From a9c3617b4fdd9b9f774974a31e5dbac70a1e178c Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Feb 2022 12:02:53 +1100 Subject: [PATCH] Decoder Properties (#1099) * Decoder Properties * Added properties round trip test * Fixed property decode for github actions * Refactored XML test to use common functions * Switched formatScenario parameter order for more readablity --- acceptance_tests/basic.sh | 4 +- acceptance_tests/inputs-format.sh | 38 ++++++++- cmd/root.go | 2 +- cmd/utils.go | 3 + examples/example.properties | 6 ++ pkg/yqlib/decoder.go | 34 ++++++++ pkg/yqlib/decoder_properties.go | 121 +++++++++++++++++++++++++++ pkg/yqlib/decoder_test.go | 60 ++++++++++++++ pkg/yqlib/decoder_xml.go | 20 +---- pkg/yqlib/decoder_yaml.go | 5 -- pkg/yqlib/doc/usage/properties.md | 69 +++++++++++++++- pkg/yqlib/json_test.go | 45 +---------- pkg/yqlib/properties_test.go | 118 ++++++++++++++++++++++++--- pkg/yqlib/xml_test.go | 130 +++++------------------------- 14 files changed, 464 insertions(+), 191 deletions(-) create mode 100644 examples/example.properties create mode 100644 pkg/yqlib/decoder.go create mode 100644 pkg/yqlib/decoder_properties.go create mode 100644 pkg/yqlib/decoder_test.go diff --git a/acceptance_tests/basic.sh b/acceptance_tests/basic.sh index be9d8f6e..fbbd52cd 100755 --- a/acceptance_tests/basic.sh +++ b/acceptance_tests/basic.sh @@ -1,8 +1,8 @@ #!/bin/bash setUp() { - rm test*.yml || true - rm .xyz -f || true + rm test*.yml 2>/dev/null || true + rm .xyz 2>/dev/null || true } testBasicEvalRoundTrip() { diff --git a/acceptance_tests/inputs-format.sh b/acceptance_tests/inputs-format.sh index 4c76cd2e..021b58a5 100755 --- a/acceptance_tests/inputs-format.sh +++ b/acceptance_tests/inputs-format.sh @@ -1,7 +1,43 @@ #!/bin/bash setUp() { - rm test*.yml || true + rm test*.yml 2>/dev/null || true + rm test*.properties 2>/dev/null || true + rm test*.xml 2>/dev/null || true +} + +testInputProperties() { + cat >test.properties <test.properties < @@ -121,7 +45,7 @@ for x --> ` -var expectedDecodeYamlWithSubChild = `D0, P[], (doc)::# before cat +var expectedDecodeYamlWithSubChild = `# before cat cat: # in cat before x: "3" # multi @@ -161,7 +85,7 @@ for x --> ` -var expectedDecodeYamlWithArray = `D0, P[], (doc)::# before cat +var expectedDecodeYamlWithArray = `# before cat cat: # in cat before x: "3" # multi @@ -185,7 +109,7 @@ cat: # after cat ` -var expectedDecodeYamlWithComments = `D0, P[], (doc)::# before cat +var expectedDecodeYamlWithComments = `# before cat cat: # in cat before x: "3" # multi @@ -234,32 +158,32 @@ var xmlScenarios = []formatScenario{ description: "Parse xml: simple", subdescription: "Notice how all the values are strings, see the next example on how you can fix that.", input: "\n\n meow\n 4\n true\n", - expected: "D0, P[], (doc)::cat:\n says: meow\n legs: \"4\"\n cute: \"true\"\n", + expected: "cat:\n says: meow\n legs: \"4\"\n cute: \"true\"\n", }, { description: "Parse xml: number", subdescription: "All values are assumed to be strings when parsing XML, but you can use the `from_yaml` operator on all the strings values to autoparse into the correct type.", input: "\n\n meow\n 4\n true\n", expression: " (.. | select(tag == \"!!str\")) |= from_yaml", - expected: "D0, P[], ()::cat:\n says: meow\n legs: 4\n cute: true\n", + expected: "cat:\n says: meow\n legs: 4\n cute: true\n", }, { description: "Parse xml: array", subdescription: "Consecutive nodes with identical xml names are assumed to be arrays.", input: "\ncat\ngoat", - expected: "D0, P[], (doc)::animal:\n - cat\n - goat\n", + expected: "animal:\n - cat\n - goat\n", }, { description: "Parse xml: attributes", subdescription: "Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.", input: "\n\n 7\n", - expected: "D0, P[], (doc)::cat:\n +legs: \"4\"\n legs: \"7\"\n", + expected: "cat:\n +legs: \"4\"\n legs: \"7\"\n", }, { description: "Parse xml: attributes with content", subdescription: "Content is added as a field, using the default content name of `+content`. Use `--xml-content-name` to set your own.", input: "\nmeow", - expected: "D0, P[], (doc)::cat:\n +content: meow\n +legs: \"4\"\n", + expected: "cat:\n +content: meow\n +legs: \"4\"\n", }, { description: "Parse xml: with comments", @@ -272,28 +196,28 @@ var xmlScenarios = []formatScenario{ description: "Empty doc", skipDoc: true, input: "", - expected: "D0, P[], ()::null\n", + expected: "", scenarioType: "decode", }, { description: "Empty single node", skipDoc: true, input: "", - expected: "D0, P[], (doc)::a:\n", + expected: "a:\n", scenarioType: "decode", }, { description: "Empty close node", skipDoc: true, input: "", - expected: "D0, P[], (doc)::a:\n", + expected: "a:\n", scenarioType: "decode", }, { description: "Nested empty", skipDoc: true, input: "", - expected: "D0, P[], (doc)::a:\n b:\n", + expected: "a:\n b:\n", scenarioType: "decode", }, { @@ -359,11 +283,12 @@ var xmlScenarios = []formatScenario{ } func testXMLScenario(t *testing.T, s formatScenario) { - if s.scenarioType == "encode" || s.scenarioType == "roundtrip" { - test.AssertResultWithContext(t, s.expected, processXMLScenario(s), s.description) + if s.scenarioType == "encode" { + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")), s.description) + } else if s.scenarioType == "roundtrip" { + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content"), NewXMLEncoder(2, "+", "+content")), s.description) } else { - var actual = resultToString(t, decodeXML(t, s)) - test.AssertResultWithContext(t, s.expected, actual, s.description) + test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content"), NewYamlEncoder(4, false, true, true)), s.description) } } @@ -378,12 +303,12 @@ func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) { } else if s.scenarioType == "roundtrip" { documentXMLRoundTripScenario(w, s) } else { - documentXMLDecodeScenario(t, w, s) + documentXMLDecodeScenario(w, s) } } -func documentXMLDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) { +func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) if s.subdescription != "" { @@ -402,18 +327,7 @@ func documentXMLDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=xml '%v' sample.xml\n```\n", expression)) writeOrPanic(w, "will output\n") - var output bytes.Buffer - printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true) - - node := decodeXML(t, s) - - err := printer.PrintResults(node.AsList()) - if err != nil { - t.Error(err) - return - } - - writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String())) + writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content"), NewYamlEncoder(2, false, true, true)))) } func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) { @@ -431,7 +345,7 @@ func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "```bash\nyq -o=xml '.' sample.yml\n```\n") writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processXMLScenario(s))) + writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")))) } func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) { @@ -449,7 +363,7 @@ func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, "```bash\nyq -p=xml -o=xml '.' sample.xml\n```\n") writeOrPanic(w, "will output\n") - writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processXMLScenario(s))) + writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processFormatScenario(s, NewXMLDecoder("+", "+content"), NewXMLEncoder(2, "+", "+content")))) } func TestXMLScenarios(t *testing.T) {