diff --git a/integration_tests/include_example.json b/integration_tests/include_example.json new file mode 100644 index 0000000..bf1b6db --- /dev/null +++ b/integration_tests/include_example.json @@ -0,0 +1,11 @@ +{ + "foo": "bar", + "one": 1, + "inner": { + "list": [ + 1, + true, + null + ] + } +} \ No newline at end of file diff --git a/integration_tests/include_example.yaml b/integration_tests/include_example.yaml new file mode 100644 index 0000000..c938bf0 --- /dev/null +++ b/integration_tests/include_example.yaml @@ -0,0 +1,5 @@ +--- +foo: "bar" +one: 1 +inner: + list: [1, true, null] \ No newline at end of file diff --git a/integration_tests/include_test.ucg b/integration_tests/include_test.ucg index 605f5c3..2028bde 100644 --- a/integration_tests/include_test.ucg +++ b/integration_tests/include_test.ucg @@ -1,3 +1,5 @@ +let t = import "std/testing.ucg".asserts{}; + let script = include str "./include_example.sh"; assert { ok = script == "#!/usr/bin/env bash @@ -14,4 +16,42 @@ let base64safe = include b64urlsafe "./include_example.sh"; assert { ok = base64safe == expected, desc = "base64safe == expected", -}; \ No newline at end of file +}; + +let json_conf = include json "./include_example.json"; + +assert t.equal{ + left = json_conf.foo, + right = "bar", + }; + +assert t.equal{ + left = json_conf.one, + right = 1, + }; + +assert t.equal{ + left = json_conf.inner, + right = { + list = [1, true, NULL], + }, + }; + +let yaml_conf = include yaml "./include_example.yaml"; + +assert t.equal{ + left = yaml_conf.foo, + right = "bar", + }; + +assert t.equal{ + left = yaml_conf.one, + right = 1, + }; + +assert t.equal{ + left = yaml_conf.inner, + right = { + list = [1, true, NULL], + }, + }; \ No newline at end of file diff --git a/src/convert/yaml.rs b/src/convert/yaml.rs index 54c74a8..03a8ad8 100644 --- a/src/convert/yaml.rs +++ b/src/convert/yaml.rs @@ -100,13 +100,21 @@ impl YamlConverter { serde_yaml::Value::Mapping(m) => { let mut fs = Vec::with_capacity(m.len()); for (key, value) in m { + // This is a little gross but since yaml allows maps to be keyed + // by more than just a string it's necessary. + let key = match key { + serde_yaml::Value::Bool(b) => b.to_string(), + serde_yaml::Value::Null => "null".to_string(), + serde_yaml::Value::Number(n) => n.to_string(), + serde_yaml::Value::String(s) => s.clone(), + serde_yaml::Value::Sequence(_) | serde_yaml::Value::Mapping(_) => { + eprintln!("Unsupported key type in yaml import skipping"); + continue; + } + }; + eprintln!("yaml key is: {}", key); fs.push(( - ast::PositionedItem::new( - // This is a little gross but since yaml allows maps to be keyed - // by more than just a string it's necessary. - serde_yaml::to_string(key)?, - ast::Position::new(0, 0, 0), - ), + ast::PositionedItem::new(key, ast::Position::new(0, 0, 0)), Rc::new(self.convert_json_val(value)?), )); }