talis-fb / TReq

A CLI tool for effortless HTTP requests
GNU General Public License v3.0
50 stars 3 forks source link

Add operator to add non-string JSON fields using `key:=value` operator #8

Closed talis-fb closed 6 months ago

talis-fb commented 7 months ago

The objective is to replicate this feature in HTTPie (https://httpie.io/docs/cli/non-string-json-fields). The proposal suggests introducing a new operator in Request items to add JSON field. Instead of the traditional key=value syntax, users can utilize key:=value. This new separator is designed to automatically infer the type of the field when it's a non-string.

  age:=29                           # Raw JSON — Number
  married:=false                    # Raw JSON — Boolean
  hobbies:='["http", "pies"]'       # Raw JSON — Array
  favorite:='{"tool": "HTTPie"}'    # Raw JSON — Object

Expected Behavior

Implement type inference for the following JSON types

Additional Considerations:

SummerGram commented 7 months ago

@talis-fb

What kind of actions related to this feature?

talis-fb commented 7 months ago

This task if for add a new operator in request items. This is almost same thing of = body declaration, It is defined HERE and used HERE

Some caveats: All the view core and the flow for parse the Input of user to ViewCommandChoices tries to be purely functional (from functional programing). All these operator are pure functions, that receive ONLY the input of request item, match them and if yes, return the new PartialRequestData with modification, or None otherwise.

To do this task is necessary

  1. Create a new function to this operator parsers_request_items
  2. Use it HERE . Important: Ensure it is defined before parsers_request_items::body_value in fold() , as the values in from it can also match its regex.
  3. Create a Regex for this operator HERE
  4. Create the function for this operator almost like the body_value. The difference live HERE. In body value it get the value and put as a static string, you'll need to use serde_json::from_str<Value> to parse it, and then insert in the map.
  5. Really important: create test for it HERE. Cover all the possible cases this can match (numbers, bool, list, object and even normal strings)
  6. Create also snapshots tests HERE for view. You'll need to download the Insta.rs, to accept the new snapshots your test will create (This is really easier than tests in 5.)
talis-fb commented 7 months ago

@SummerGram

talis-fb commented 7 months ago

I believe these are the steps. Maybe the parse from Value::from_str can be different in some parser of number values. But if it does, you can tell me.