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,78 +250,74 @@ 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 {
if let Some(prop_schema) = properties.get::<str>(key) { for (key, value) in data {
match prop_schema { if let Some(prop_schema) = properties.get::<str>(key) {
Schema::Array(array_schema) => { match prop_schema {
if params[key] == Value::Null { Schema::Array(array_schema) => {
params[key] = json!([]); if params[key] == Value::Null {
} params[key] = json!([]);
match params[key] { }
Value::Array(ref mut array) => { match params[key] {
match parse_simple_value(value, &array_schema.items) { Value::Array(ref mut array) => {
Ok(res) => array.push(res), match parse_simple_value(value, &array_schema.items) {
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)), Ok(res) => array.push(res),
}
}
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
}
}
_ => {
match parse_simple_value(value, prop_schema) {
Ok(res) => {
if params[key] == Value::Null {
params[key] = res;
} else {
errors.push(format_err!("parameter '{}': duplicate parameter.", key));
}
},
Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)), Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
} }
} }
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
} }
} else { }
if *additional_properties { _ => {
match params[key] { match parse_simple_value(value, prop_schema) {
Value::Null => { Ok(res) => {
params[key] = Value::String(value.to_owned()); if params[key] == Value::Null {
}, params[key] = res;
Value::String(ref old) => { } else {
params[key] = Value::Array( errors.push(format_err!("parameter '{}': duplicate parameter.", key));
vec![Value::String(old.to_owned()), Value::String(value.to_owned())]);
} }
Value::Array(ref mut array) => { },
array.push(Value::String(value.to_string())); Err(err) => errors.push(format_err!("parameter '{}': {}", key, err)),
}
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
}
} else {
errors.push(format_err!("parameter '{}': schema does not allow additional properties.", key));
} }
} }
} }
} else {
if test_required && errors.len() == 0 { if additional_properties {
for (name, prop_schema) in properties { match params[key] {
let optional = match prop_schema { Value::Null => {
Schema::Boolean(boolean_schema) => boolean_schema.optional, params[key] = Value::String(value.to_owned());
Schema::Integer(integer_schema) => integer_schema.optional, },
Schema::String(string_schema) => string_schema.optional, Value::String(ref old) => {
Schema::Array(array_schema) => array_schema.optional, params[key] = Value::Array(
Schema::Object(object_schema) => object_schema.optional, vec![Value::String(old.to_owned()), Value::String(value.to_owned())]);
Schema::Null => true,
};
if optional == false && params[name] == Value::Null {
errors.push(format_err!("parameter '{}': parameter is missing and it is not optional.", name));
} }
Value::Array(ref mut array) => {
array.push(Value::String(value.to_string()));
}
_ => errors.push(format_err!("parameter '{}': expected array - type missmatch", key)),
} }
} else {
errors.push(format_err!("parameter '{}': schema does not allow additional properties.", key));
} }
} }
_ => errors.push(format_err!("Got unexpected schema type in parse_parameter_strings.")), }
if test_required && errors.len() == 0 {
for (name, prop_schema) in properties {
let optional = match prop_schema {
Schema::Boolean(boolean_schema) => boolean_schema.optional,
Schema::Integer(integer_schema) => integer_schema.optional,
Schema::String(string_schema) => string_schema.optional,
Schema::Array(array_schema) => array_schema.optional,
Schema::Object(object_schema) => object_schema.optional,
Schema::Null => true,
};
if optional == false && params[name] == Value::Null {
errors.push(format_err!("parameter '{}': parameter is missing and it is not optional.", name));
}
}
} }
if errors.len() > 0 { if errors.len() > 0 {
@ -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![];