use Arc pointer to properties
This commit is contained in:
parent
e471d69946
commit
00c908df85
|
@ -4,6 +4,7 @@ use serde_json::{json, Value};
|
||||||
use url::form_urlencoded;
|
use url::form_urlencoded;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub type PropertyMap = HashMap<&'static str, Schema>;
|
pub type PropertyMap = HashMap<&'static str, Schema>;
|
||||||
|
|
||||||
|
@ -67,7 +68,7 @@ pub struct StringSchema {
|
||||||
pub struct ArraySchema {
|
pub struct ArraySchema {
|
||||||
pub description: &'static str,
|
pub description: &'static str,
|
||||||
pub optional: bool,
|
pub optional: bool,
|
||||||
pub items: Box<Schema>,
|
pub items: Arc<Schema>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -75,7 +76,7 @@ pub struct ObjectSchema {
|
||||||
pub description: &'static str,
|
pub description: &'static str,
|
||||||
pub optional: bool,
|
pub optional: bool,
|
||||||
pub additional_properties: bool,
|
pub additional_properties: bool,
|
||||||
pub properties: HashMap<&'static str, Schema>,
|
pub properties: HashMap<&'static str, Arc<Schema>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -130,7 +131,7 @@ pub enum ApiStringFormat {
|
||||||
None,
|
None,
|
||||||
Enum(Vec<String>),
|
Enum(Vec<String>),
|
||||||
Pattern(Box<Regex>),
|
Pattern(Box<Regex>),
|
||||||
Complex(Box<Schema>),
|
Complex(Arc<Schema>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
@ -148,7 +149,7 @@ macro_rules! parameter {
|
||||||
description: "",
|
description: "",
|
||||||
optional: false,
|
optional: false,
|
||||||
additional_properties: false,
|
additional_properties: false,
|
||||||
properties: HashMap::<&'static str, Schema>::new(),
|
properties: HashMap::<&'static str, Arc<Schema>>::new(),
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
($($name:ident => $e:expr),*) => {{
|
($($name:ident => $e:expr),*) => {{
|
||||||
|
@ -157,7 +158,7 @@ macro_rules! parameter {
|
||||||
optional: false,
|
optional: false,
|
||||||
additional_properties: false,
|
additional_properties: false,
|
||||||
properties: {
|
properties: {
|
||||||
let mut map = HashMap::<&'static str, Schema>::new();
|
let mut map = HashMap::<&'static str, Arc<Schema>>::new();
|
||||||
$(
|
$(
|
||||||
map.insert(stringify!($name), $e);
|
map.insert(stringify!($name), $e);
|
||||||
)*
|
)*
|
||||||
|
@ -255,7 +256,7 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &ObjectSche
|
||||||
|
|
||||||
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.as_ref() {
|
||||||
Schema::Array(array_schema) => {
|
Schema::Array(array_schema) => {
|
||||||
if params[key] == Value::Null {
|
if params[key] == Value::Null {
|
||||||
params[key] = json!([]);
|
params[key] = json!([]);
|
||||||
|
@ -306,7 +307,7 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &ObjectSche
|
||||||
|
|
||||||
if test_required && errors.len() == 0 {
|
if test_required && errors.len() == 0 {
|
||||||
for (name, prop_schema) in properties {
|
for (name, prop_schema) in properties {
|
||||||
let optional = match prop_schema {
|
let optional = match prop_schema.as_ref() {
|
||||||
Schema::Boolean(boolean_schema) => boolean_schema.optional,
|
Schema::Boolean(boolean_schema) => boolean_schema.optional,
|
||||||
Schema::Integer(integer_schema) => integer_schema.optional,
|
Schema::Integer(integer_schema) => integer_schema.optional,
|
||||||
Schema::String(string_schema) => string_schema.optional,
|
Schema::String(string_schema) => string_schema.optional,
|
||||||
|
@ -354,24 +355,24 @@ fn test_schema1() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_string() {
|
fn test_query_string() {
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{ optional => false }};
|
let schema = parameter!{name => Arc::new(ApiString!{ optional => false })};
|
||||||
|
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{ optional => true }};
|
let schema = parameter!{name => Arc::new(ApiString!{ optional => true })};
|
||||||
|
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
|
||||||
// TEST min_length and max_length
|
// TEST min_length and max_length
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{
|
let schema = parameter!{name => Arc::new(ApiString!{
|
||||||
optional => false,
|
optional => false,
|
||||||
min_length => Some(5),
|
min_length => Some(5),
|
||||||
max_length => Some(10)
|
max_length => Some(10)
|
||||||
|
|
||||||
}};
|
})};
|
||||||
|
|
||||||
let res = parse_query_string("name=abcd", &schema, true);
|
let res = parse_query_string("name=abcd", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
@ -387,10 +388,10 @@ fn test_query_string() {
|
||||||
|
|
||||||
// TEST regex pattern
|
// TEST regex pattern
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{
|
let schema = parameter!{name => Arc::new(ApiString!{
|
||||||
optional => false,
|
optional => false,
|
||||||
format => ApiStringFormat::Pattern(Box::new(Regex::new("test").unwrap()))
|
format => ApiStringFormat::Pattern(Box::new(Regex::new("test").unwrap()))
|
||||||
}};
|
})};
|
||||||
|
|
||||||
let res = parse_query_string("name=abcd", &schema, true);
|
let res = parse_query_string("name=abcd", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
@ -398,10 +399,10 @@ fn test_query_string() {
|
||||||
let res = parse_query_string("name=ateststring", &schema, true);
|
let res = parse_query_string("name=ateststring", &schema, true);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{
|
let schema = parameter!{name => Arc::new(ApiString!{
|
||||||
optional => false,
|
optional => false,
|
||||||
format => ApiStringFormat::Pattern(Box::new(Regex::new("^test$").unwrap()))
|
format => ApiStringFormat::Pattern(Box::new(Regex::new("^test$").unwrap()))
|
||||||
}};
|
})};
|
||||||
|
|
||||||
let res = parse_query_string("name=ateststring", &schema, true);
|
let res = parse_query_string("name=ateststring", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
@ -411,10 +412,10 @@ fn test_query_string() {
|
||||||
|
|
||||||
// TEST string enums
|
// TEST string enums
|
||||||
|
|
||||||
let schema = parameter!{name => ApiString!{
|
let schema = parameter!{name => Arc::new(ApiString!{
|
||||||
optional => false,
|
optional => false,
|
||||||
format => ApiStringFormat::Enum(vec!["ev1".into(), "ev2".into()])
|
format => ApiStringFormat::Enum(vec!["ev1".into(), "ev2".into()])
|
||||||
}};
|
})};
|
||||||
|
|
||||||
let res = parse_query_string("name=noenum", &schema, true);
|
let res = parse_query_string("name=noenum", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
@ -433,16 +434,16 @@ fn test_query_string() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_integer() {
|
fn test_query_integer() {
|
||||||
|
|
||||||
let schema = parameter!{count => Integer!{ optional => false }};
|
let schema = parameter!{count => Arc::new(Integer!{ optional => false })};
|
||||||
|
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
|
||||||
let schema = parameter!{count => Integer!{
|
let schema = parameter!{count => Arc::new(Integer!{
|
||||||
optional => true,
|
optional => true,
|
||||||
minimum => Some(-3),
|
minimum => Some(-3),
|
||||||
maximum => Some(50)
|
maximum => Some(50)
|
||||||
}};
|
})};
|
||||||
|
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
@ -472,12 +473,12 @@ fn test_query_integer() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_query_boolean() {
|
fn test_query_boolean() {
|
||||||
|
|
||||||
let schema = parameter!{force => Boolean!{ optional => false }};
|
let schema = parameter!{force => Arc::new(Boolean!{ optional => false })};
|
||||||
|
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
|
|
||||||
let schema = parameter!{force => Boolean!{ optional => true }};
|
let schema = parameter!{force => Arc::new(Boolean!{ optional => true })};
|
||||||
|
|
||||||
let res = parse_query_string("", &schema, true);
|
let res = parse_query_string("", &schema, true);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use failure::*;
|
use failure::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
||||||
use crate::api::schema::*;
|
use crate::api::schema::*;
|
||||||
|
@ -44,10 +45,10 @@ pub fn router() -> Router {
|
||||||
handler: test_sync_api_handler,
|
handler: test_sync_api_handler,
|
||||||
description: "This is a simple test.",
|
description: "This is a simple test.",
|
||||||
parameters: parameter!{
|
parameters: parameter!{
|
||||||
force => Boolean!{
|
force => Arc::new(Boolean!{
|
||||||
optional => true,
|
optional => true,
|
||||||
description => "Test for boolean options."
|
description => "Test for boolean options."
|
||||||
}
|
})
|
||||||
},
|
},
|
||||||
returns: Schema::Null,
|
returns: Schema::Null,
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,6 +2,8 @@ use crate::api::schema::*;
|
||||||
|
|
||||||
use failure::*;
|
use failure::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -71,16 +73,18 @@ pub fn parse_arguments(
|
||||||
RawArgument::Option { name, value } => {
|
RawArgument::Option { name, value } => {
|
||||||
match value {
|
match value {
|
||||||
None => {
|
None => {
|
||||||
let param_schema = properties.get::<str>(&name);
|
let mut want_bool = false;
|
||||||
let (want_bool, can_default) = match param_schema {
|
let mut can_default = false;
|
||||||
Some(Schema::Boolean(boolean_schema)) => {
|
if let Some(param_schema) = properties.get::<str>(&name) {
|
||||||
|
if let Schema::Boolean(boolean_schema) = param_schema.as_ref() {
|
||||||
|
want_bool = true;
|
||||||
if let Some(default) = boolean_schema.default {
|
if let Some(default) = boolean_schema.default {
|
||||||
if default == true { (true, false); }
|
if default == false { can_default = true; }
|
||||||
|
} else {
|
||||||
|
can_default = true;
|
||||||
}
|
}
|
||||||
(true, true)
|
|
||||||
}
|
}
|
||||||
_ => (false, false),
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let mut next_is_argument = false;
|
let mut next_is_argument = false;
|
||||||
let mut next_is_bool = false;
|
let mut next_is_bool = false;
|
||||||
|
@ -152,7 +156,7 @@ pub fn parse_arguments(
|
||||||
#[test]
|
#[test]
|
||||||
fn test_boolean_arg() {
|
fn test_boolean_arg() {
|
||||||
|
|
||||||
let schema = parameter!{enable => Boolean!{ optional => false }};
|
let schema = parameter!{enable => Arc::new(Boolean!{ optional => false })};
|
||||||
|
|
||||||
let mut variants: Vec<(Vec<&str>, bool)> = vec![];
|
let mut variants: Vec<(Vec<&str>, bool)> = vec![];
|
||||||
variants.push((vec!["-enable"], true));
|
variants.push((vec!["-enable"], true));
|
||||||
|
@ -183,8 +187,8 @@ fn test_boolean_arg() {
|
||||||
fn test_argument_paramenter() {
|
fn test_argument_paramenter() {
|
||||||
|
|
||||||
let schema = parameter!{
|
let schema = parameter!{
|
||||||
enable => Boolean!{ optional => false },
|
enable => Arc::new(Boolean!{ optional => false }),
|
||||||
storage => ApiString!{ optional => false }
|
storage => Arc::new(ApiString!{ optional => false })
|
||||||
};
|
};
|
||||||
|
|
||||||
let args = vec!["-enable", "local"];
|
let args = vec!["-enable", "local"];
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
extern crate apitest;
|
extern crate apitest;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use apitest::api::schema::*;
|
use apitest::api::schema::*;
|
||||||
use apitest::api::router::*;
|
use apitest::api::router::*;
|
||||||
|
@ -20,7 +21,7 @@ fn main() {
|
||||||
println!("Proxmox REST Server example.");
|
println!("Proxmox REST Server example.");
|
||||||
|
|
||||||
let schema = parameter!{
|
let schema = parameter!{
|
||||||
name => ApiString!{ optional => true }
|
name => Arc::new(ApiString!{ optional => true })
|
||||||
};
|
};
|
||||||
|
|
||||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||||
|
|
Loading…
Reference in New Issue