proxmox/src/api/section_config.rs: support array properties

This commit is contained in:
Dietmar Maurer 2020-05-12 08:27:23 +02:00
parent e847487b29
commit 414daabbcb

View File

@ -298,21 +298,32 @@ impl SectionConfig {
if let Some((key, value)) = (self.parse_section_content)(line) { if let Some((key, value)) = (self.parse_section_content)(line) {
//println!("CONTENT: key: {} value: {}", key, value); //println!("CONTENT: key: {} value: {}", key, value);
if let Some((_optional, prop_schema)) = plugin.properties.lookup(&key) { let schema = plugin.properties.lookup(&key);
match parse_simple_value(&value, prop_schema) { let (is_array, prop_schema) = match schema {
Ok(value) => { Some((_optional, Schema::Array(ArraySchema { items, .. }))) => (true, items),
if config[&key] == Value::Null { Some((_optional, ref prop_schema)) => (false, prop_schema),
config[key] = value; None => bail!("unknown property '{}'", key),
} else { };
bail!("duplicate property '{}'", key);
} let value = match parse_simple_value(&value, prop_schema) {
} Ok(value) => value,
Err(err) => { Err(err) => {
bail!("property '{}': {}", key, err.to_string()); bail!("property '{}': {}", key, err.to_string());
}
} }
};
if is_array {
if config[&key] == Value::Null {
config[key] = json!([value]);
} else {
config[key].as_array_mut().unwrap().push(value);
}
} else { } else {
bail!("unknown property '{}'", key) if config[&key] == Value::Null {
config[key] = value;
} else {
bail!("duplicate property '{}'", key);
}
} }
} else { } else {
bail!("syntax error (expected section properties)"); bail!("syntax error (expected section properties)");
@ -342,12 +353,21 @@ impl SectionConfig {
} }
fn default_format_section_content( fn default_format_section_content(
_type_name: &str, type_name: &str,
section_id: &str, section_id: &str,
key: &str, key: &str,
value: &Value, value: &Value,
) -> Result<String, Error> { ) -> Result<String, Error> {
if let Value::Array(array) = value {
let mut list = String::new();
for item in array {
let line = Self::default_format_section_content(type_name, section_id, key, item)?;
if !line.is_empty() { list.push_str(&line); }
}
return Ok(list);
}
let text = match value { let text = match value {
Value::Null => { return Ok(String::new()) }, // return empty string (delete) Value::Null => { return Ok(String::new()) }, // return empty string (delete)
Value::Bool(v) => v.to_string(), Value::Bool(v) => v.to_string(),