Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add variables #30

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- [Request DSL](./reference_guide/request_dsl.md)
- [Body](./reference_guide/request_dsl/body.md)
- [Input_parameters]()
- [Placeholders and variables]()
- [Placeholders](./reference_guide/request_dsl/placeholders.md)
- [Command Line Interface](./reference_guide/command_line_interface.md)
- [run](reference_guide/command_line_interface/run.md)
- [example](reference_guide/command_line_interface/example.md)
Expand Down
13 changes: 13 additions & 0 deletions book/src/reference_guide/request_dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ _warning: some common leaders like `Host` and `Content-Type` will be autogenerat
option to disable this autogeneration_

## [query_params]

This table is **free**. There's no predefined keys but the values can't be of type datetime
or tables. Arrays are supported and will be sent in the request as a comma separated list of
the values.
Expand Down Expand Up @@ -95,6 +96,18 @@ name = "Feldespato"
location = "unknown"
```

## [variables]
This table is **free**. There's no predefined keys but the values can't be of type datetime
or tables. Arrays are supported and will be sent in the request as a comma separated list of
the values.

Variables serve to set a value to replace **placeholders** ([see](./request_dsl/placeholders.md)).

```toml
host = "192.0.0.1"
name = "Vin"
```

## [metadata]

This table is **free** but the values must be one of the primitive values (_string_, _integer_,
Expand Down
12 changes: 12 additions & 0 deletions book/src/reference_guide/request_dsl/placeholders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Placeholders

Placeholders are strings wrapped in double braces, like `{{this}}`. They can be the whole value
of a string or can be a substring, for example: `{{host}}/api/{{version}}/hello`.

Placeholders can be given a value through different resolvers. These resolvers have a defined
order and the first providing a value will be the one that is used. If no resolver has a value
for the placeholder then the user will be prompted to resolve it.

The order of resolution is:
1. [Variables](#variables), these are defined in a [standard table](../request_dsl.md#variables)
similar to query params or headers, but this one is only aimed to provide values for placeholders.
6 changes: 3 additions & 3 deletions docs/schema/schema_v0.1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ _key = "string"
[query_params]
_key = ["string", "integer", "float", "boolean", "array"]

####### The path params table defines the value for segment paths enclosed in `{{name}}`, overrides env values
####### The variables table allows to define values for placeholders

[path_params]
_key = ["string", "integer", "float", "boolean"]
[variables]
_key = ["string", "integer", "float", "boolean", "array"]

####### Defines the body that will be sent with the request

Expand Down
15 changes: 9 additions & 6 deletions parser/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct Request {
pub metadata: HashMap<String, String>,
pub headers: HeaderMap,
pub query_params: Vec<(String, String)>,
pub path_params: HashMap<String, String>,
pub body: Body,
pub variables: HashMap<String, String>,
}

impl TryFrom<Schema> for Request {
Expand All @@ -32,7 +32,7 @@ impl TryFrom<Schema> for Request {
metadata: schema.metadata.into_map(),
headers: schema.headers,
query_params: schema.query_params.into_pairs(),
path_params: schema.path_params.into_map(),
variables: schema.variables.into_map(),
body: schema.body.into(),
})
}
Expand Down Expand Up @@ -65,8 +65,11 @@ mod test {
PrimitiveArray::Multiple(vec![Primitive::Str("s".to_string()), Primitive::Int(1)]),
);

let mut path_params = HashMap::new();
path_params.insert("pp".to_string(), Primitive::Str("value".to_string()));
let mut variables = HashMap::new();
variables.insert(
"pp".to_string(),
PrimitiveArray::Single(Primitive::Str("value".to_string())),
);

let body = schema::Body::Binary("path".to_string());

Expand All @@ -79,7 +82,7 @@ mod test {
headers,
metadata: Table::new(metadata),
query_params: Table::new(query_params),
path_params: Table::new(path_params),
variables: Table::new(variables),
body,
};

Expand All @@ -96,7 +99,7 @@ mod test {
("qp".to_string(), "1".to_string())
]
);
assert_eq!(request.path_params["pp"], "value");
assert_eq!(request.variables["pp"], "value");
assert_eq!(
request.body,
Body::Binary {
Expand Down
40 changes: 27 additions & 13 deletions parser/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;
pub(crate) use body::Body;

use crate::error::Error;
use crate::schema::table::{PrimitiveArrTable, PrimitiveTable};
use crate::schema::table::PrimitiveTable;

mod body;
pub(crate) mod table;
Expand All @@ -18,15 +18,15 @@ pub(crate) mod types;
pub(crate) struct Schema {
pub http: Http,
#[serde(default)]
pub metadata: PrimitiveArrTable,
pub metadata: PrimitiveTable,
#[serde(with = "http_serde::header_map", default)]
pub headers: HeaderMap,
#[serde(alias = "queryparams", alias = "query-params", default)]
pub query_params: PrimitiveArrTable,
#[serde(alias = "pathparams", alias = "path-params", default)]
pub path_params: PrimitiveTable,
pub query_params: PrimitiveTable,
#[serde(default)]
pub body: Body,
#[serde(default)]
pub variables: PrimitiveTable,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -75,11 +75,12 @@ mod test {
array = [ "first", "second" ]
boolean = true

[path-params]
[variables]
string = "string"
integer = 5
float = 1.2
boolean = false
array = [ 1, "2" ]

[body]
raw = """
Expand Down Expand Up @@ -133,14 +134,27 @@ mod test {
Primitive::Str("second".into()),
])
);
assert_eq!(schema.path_params.0.len(), 4);
assert_eq!(schema.variables.0.len(), 5);
assert_eq!(
schema.variables.0["string"],
PrimitiveArray::Single(Primitive::Str("string".to_string()))
);
assert_eq!(
schema.variables.0["integer"],
PrimitiveArray::Single(Primitive::Int(5))
);
assert_eq!(
schema.variables.0["float"],
PrimitiveArray::Single(Primitive::Float(1.2))
);
assert_eq!(
schema.variables.0["boolean"],
PrimitiveArray::Single(Primitive::Bool(false))
);
assert_eq!(
schema.path_params.0["string"],
Primitive::Str("string".to_string())
schema.variables.0["array"],
PrimitiveArray::Multiple(vec![Primitive::Int(1), Primitive::Str("2".into()),])
);
assert_eq!(schema.path_params.0["integer"], Primitive::Int(5));
assert_eq!(schema.path_params.0["float"], Primitive::Float(1.2));
assert_eq!(schema.path_params.0["boolean"], Primitive::Bool(false));
let body: Body = schema.body.into();
assert!(matches!(body, Body::Raw(content) if content.contains(r#""key": "value""#)));
}
Expand Down Expand Up @@ -168,7 +182,7 @@ mod test {
assert!(schema.metadata.0.is_empty());
assert!(schema.headers.is_empty());
assert!(schema.query_params.0.is_empty());
assert!(schema.path_params.0.is_empty());
assert!(schema.variables.0.is_empty());
assert_eq!(schema.body, Body::None);
}
}
4 changes: 2 additions & 2 deletions parser/src/schema/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::body::Body as PublicBody;
use crate::schema::table::{FormDataTable, PrimitiveArrTable, Transform};
use crate::schema::table::{FormDataTable, PrimitiveTable, Transform};
use crate::schema::types::PrimitiveArray;
use serde::Deserialize;

Expand All @@ -24,7 +24,7 @@ pub(crate) enum Body {
alias = "form_urlencoded",
alias = "form-urlencoded"
)]
XFormUrlEncoded(PrimitiveArrTable),
XFormUrlEncoded(PrimitiveTable),
}

#[derive(Debug, Deserialize, PartialEq)]
Expand Down
9 changes: 4 additions & 5 deletions parser/src/schema/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use std::collections::HashMap;
use std::ops::Index;

use crate::schema::body::FormDataValue;
use crate::schema::types::{Primitive, PrimitiveArray};
use crate::schema::types::PrimitiveArray;
use serde::Deserialize;

/// Newtype implementation to wrap TOML tables where the set of keys can be free
#[derive(Debug, Deserialize, PartialEq)]
pub(crate) struct Table<V>(pub(crate) HashMap<String, V>);

pub type PrimitiveTable = Table<Primitive>;
pub type PrimitiveArrTable = Table<PrimitiveArray>;
pub type PrimitiveTable = Table<PrimitiveArray>;
pub type FormDataTable = Table<FormDataValue>;

impl<V> Index<&str> for Table<V> {
Expand Down Expand Up @@ -59,7 +58,7 @@ where
}
}

impl PrimitiveArrTable {
impl PrimitiveTable {
pub(crate) fn into_pairs(self) -> Vec<(String, String)> {
let mut vec = Vec::new();
for (key, val) in self {
Expand Down Expand Up @@ -109,7 +108,7 @@ mod test {
assert_eq!(map["array"], "one,2");
}

fn new_test_table() -> PrimitiveArrTable {
fn new_test_table() -> PrimitiveTable {
let string = r#"
string = "value"
integer = 10
Expand Down
Loading