Add multidocument yaml support for output.

Closes issue #54
This commit is contained in:
Jeremy Wall 2020-04-12 18:54:53 -04:00
parent 3f3368f496
commit 72de604384
5 changed files with 71 additions and 7 deletions

View File

@ -19,11 +19,9 @@ let db_conn2 = mk_db_conn(dbhost2, shared.port, dbname);
// We have two database connections in a list // We have two database connections in a list
let db_conn_list = [db_conn1, db_conn2]; let db_conn_list = [db_conn1, db_conn2];
let connstr_mapper = func (item) => { let connstr_mapper = func (item) => item.conn_string;
str = item.conn_string
};
let db_conns = map connstr_mapper.str [db_conn1, db_conn2]; let db_conns = map(connstr_mapper, [db_conn1, db_conn2]);
let cplx_list = [ let cplx_list = [
{foo = 1}, {foo = 1},
@ -41,4 +39,4 @@ let server_config = {
l = ["foo", "bar"] l = ["foo", "bar"]
}; };
out yaml server_config; out yamlmulti [server_config, server_config];

View File

@ -22,6 +22,7 @@ pub mod toml;
pub mod traits; pub mod traits;
pub mod xml; pub mod xml;
pub mod yaml; pub mod yaml;
pub mod yamlmulti;
use std::collections::HashMap; use std::collections::HashMap;
@ -50,6 +51,7 @@ impl ConverterRegistry {
registry.register("flags", Box::new(flags::FlagConverter::new())); registry.register("flags", Box::new(flags::FlagConverter::new()));
registry.register("exec", Box::new(exec::ExecConverter::new())); registry.register("exec", Box::new(exec::ExecConverter::new()));
registry.register("yaml", Box::new(yaml::YamlConverter::new())); registry.register("yaml", Box::new(yaml::YamlConverter::new()));
registry.register("yamlmulti", Box::new(yamlmulti::MultiYamlConverter::new()));
registry.register("toml", Box::new(toml::TomlConverter::new())); registry.register("toml", Box::new(toml::TomlConverter::new()));
registry.register("xml", Box::new(xml::XmlConverter {})); registry.register("xml", Box::new(xml::XmlConverter {}));
registry registry

View File

@ -130,7 +130,7 @@ impl YamlConverter {
}) })
} }
fn write(&self, v: &Val, mut w: &mut dyn Write) -> ConvertResult { pub fn write(&self, v: &Val, mut w: &mut dyn Write) -> ConvertResult {
let jsn_val = self.convert_value(v)?; let jsn_val = self.convert_value(v)?;
serde_yaml::to_writer(&mut w, &jsn_val)?; serde_yaml::to_writer(&mut w, &jsn_val)?;
writeln!(w, "")?; writeln!(w, "")?;

View File

@ -7,4 +7,8 @@ They are transformed into toml using the following rules:
- Int becomes an Int - Int becomes an Int
- Float becomes a Float - Float becomes a Float
- Strings become Strings. - Strings become Strings.
- Functions and Modules are ignored. - Functions and Modules are ignored.
If you are using the yamlmulti conversion type then a list will get transformed
into a yaml document per list item. All other types will get treated as a single
document.

60
src/convert/yamlmulti.rs Normal file
View File

@ -0,0 +1,60 @@
// Copyright 2020 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std;
use std::io::Write;
use std::rc::Rc;
use crate::convert::yaml::YamlConverter;
use crate::convert::traits::{ConvertResult, Converter};
use crate::build::Val;
pub struct MultiYamlConverter (YamlConverter);
impl MultiYamlConverter {
pub fn new() -> Self {
MultiYamlConverter (YamlConverter::new())
}
pub fn convert_list(&self, vals: &Vec<Rc<Val>>, mut w: &mut dyn Write) -> ConvertResult {
for val in vals {
self.0.write(val.as_ref(), &mut w)?;
}
Ok(())
}
}
impl Converter for MultiYamlConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut dyn Write) -> ConvertResult {
if let Val::List(ref vals) = v.as_ref() {
self.convert_list(vals, &mut w)
} else {
let list = vec![v];
self.convert_list(&list, &mut w)
}
}
fn file_ext(&self) -> String {
"yaml".to_owned()
}
fn description(&self) -> String {
"Convert ucg vals into valid multi document yaml.".to_owned()
}
#[allow(unused_must_use)]
fn help(&self) -> String {
include_str!("yaml_help.txt").to_owned()
}
}