use ObjectSchema for parameters

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2018-11-18 08:46:26 +01:00
parent 5d9f0eae6b
commit a653882dd9
3 changed files with 64 additions and 78 deletions

View File

@ -6,7 +6,7 @@ use std::collections::HashMap;
pub struct ApiMethod { pub struct ApiMethod {
pub description: &'static str, pub description: &'static str,
pub parameters: Schema, pub parameters: ObjectSchema,
pub returns: Schema, pub returns: Schema,
pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>, pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>,
} }

View File

@ -144,17 +144,15 @@ macro_rules! ApiString {
#[macro_export] #[macro_export]
macro_rules! parameter { macro_rules! parameter {
() => {{ () => {{
let inner = ObjectSchema { ObjectSchema {
description: "", description: "",
optional: false, optional: false,
additional_properties: false, additional_properties: false,
properties: HashMap::<&'static str, Schema>::new(), properties: HashMap::<&'static str, Schema>::new(),
}; }
Schema::Object(inner)
}}; }};
($($name:ident => $e:expr),*) => {{ ($($name:ident => $e:expr),*) => {{
let inner = ObjectSchema { ObjectSchema {
description: "", description: "",
optional: false, optional: false,
additional_properties: false, additional_properties: false,
@ -165,9 +163,7 @@ macro_rules! parameter {
)* )*
map map
} }
}; }
Schema::Object(inner)
}}; }};
} }
@ -246,7 +242,7 @@ fn parse_simple_value(value_str: &str, schema: &Schema) -> Result<Value, Error>
Ok(value) Ok(value)
} }
pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Schema, test_required: bool) -> Result<Value, ParameterError> { pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &ObjectSchema, test_required: bool) -> Result<Value, ParameterError> {
println!("QUERY Strings {:?}", data); println!("QUERY Strings {:?}", data);
@ -254,8 +250,9 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Schema, te
let mut errors = ParameterError::new(); let mut errors = ParameterError::new();
match schema { let properties = &schema.properties;
Schema::Object(ObjectSchema { properties, additional_properties, .. }) => { let additional_properties = schema.additional_properties;
for (key, value) in data { for (key, value) in data {
if let Some(prop_schema) = properties.get::<str>(key) { if let Some(prop_schema) = properties.get::<str>(key) {
match prop_schema { match prop_schema {
@ -285,10 +282,9 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Schema, te
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)), Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
} }
} }
} }
} else { } else {
if *additional_properties { if additional_properties {
match params[key] { match params[key] {
Value::Null => { Value::Null => {
params[key] = Value::String(value.to_owned()); params[key] = Value::String(value.to_owned());
@ -323,10 +319,6 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Schema, te
} }
} }
} }
}
_ => errors.push(format_err!("Got unexpected schema type in parse_parameter_strings.")),
}
if errors.len() > 0 { if errors.len() > 0 {
Err(errors) Err(errors)
@ -335,7 +327,7 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Schema, te
} }
} }
pub fn parse_query_string(query: &str, schema: &Schema, test_required: bool) -> Result<Value, ParameterError> { pub fn parse_query_string(query: &str, schema: &ObjectSchema, test_required: bool) -> Result<Value, ParameterError> {
let param_list: Vec<(String, String)> = let param_list: Vec<(String, String)> =
form_urlencoded::parse(query.as_bytes()).into_owned().collect(); form_urlencoded::parse(query.as_bytes()).into_owned().collect();

View File

@ -42,18 +42,12 @@ fn parse_argument(arg: &str) -> RawArgument {
pub fn parse_arguments( pub fn parse_arguments(
args: &Vec<String>, args: &Vec<String>,
schema: &Schema, schema: &ObjectSchema,
) -> Result<(Value,Vec<String>), ParameterError> { ) -> Result<(Value,Vec<String>), ParameterError> {
let mut errors = ParameterError::new(); let mut errors = ParameterError::new();
let properties = match schema { let properties = &schema.properties;
Schema::Object(ObjectSchema { properties, .. }) => properties,
_ => {
errors.push(format_err!("parse arguments failed - got strange parameters (expected object schema)."));
return Err(errors);
},
};
let mut data: Vec<(String, String)> = vec![]; let mut data: Vec<(String, String)> = vec![];
let mut rest: Vec<String> = vec![]; let mut rest: Vec<String> = vec![];