From 3ac531ce747e9ea4041adb17aa8a4a0771b22203 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Feb 2022 11:34:33 +1100 Subject: [PATCH] Fixed property decode for github actions --- acceptance_tests/inputs-format.sh | 17 ++++++++++++++++ pkg/yqlib/decoder.go | 34 +++++++++++++++++++++++++++++++ pkg/yqlib/decoder_properties.go | 5 +++++ pkg/yqlib/decoder_xml.go | 23 +-------------------- pkg/yqlib/decoder_yaml.go | 5 ----- pkg/yqlib/doc/usage/properties.md | 13 ++++++++++++ pkg/yqlib/properties_test.go | 7 +++++++ 7 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 pkg/yqlib/decoder.go diff --git a/acceptance_tests/inputs-format.sh b/acceptance_tests/inputs-format.sh index 467d2114..021b58a5 100755 --- a/acceptance_tests/inputs-format.sh +++ b/acceptance_tests/inputs-format.sh @@ -23,6 +23,23 @@ EOM assertEquals "$expected" "$X" } +testInputPropertiesGitHubAction() { + cat >test.properties <test.yml <BiBi diff --git a/pkg/yqlib/decoder.go b/pkg/yqlib/decoder.go new file mode 100644 index 00000000..f8797607 --- /dev/null +++ b/pkg/yqlib/decoder.go @@ -0,0 +1,34 @@ +package yqlib + +import ( + "fmt" + "io" + + yaml "gopkg.in/yaml.v3" +) + +type InputFormat uint + +const ( + YamlInputFormat = 1 << iota + XMLInputFormat + PropertiesInputFormat +) + +type Decoder interface { + Init(reader io.Reader) + Decode(node *yaml.Node) error +} + +func InputFormatFromString(format string) (InputFormat, error) { + switch format { + case "yaml", "y": + return YamlInputFormat, nil + case "xml", "x": + return XMLInputFormat, nil + case "props", "p": + return PropertiesInputFormat, nil + default: + return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml|props]", format) + } +} diff --git a/pkg/yqlib/decoder_properties.go b/pkg/yqlib/decoder_properties.go index 6cc312e6..85e0349d 100644 --- a/pkg/yqlib/decoder_properties.go +++ b/pkg/yqlib/decoder_properties.go @@ -83,9 +83,14 @@ func (dec *propertiesDecoder) Decode(rootYamlNode *yaml.Node) error { return io.EOF } buf := new(bytes.Buffer) + if _, err := buf.ReadFrom(dec.reader); err != nil { return err } + if buf.Len() == 0 { + dec.finished = true + return io.EOF + } properties, err := properties.LoadString(buf.String()) if err != nil { return err diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index 23bbff00..7fa7ea27 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -2,7 +2,6 @@ package yqlib import ( "encoding/xml" - "fmt" "io" "strings" "unicode" @@ -11,27 +10,6 @@ import ( yaml "gopkg.in/yaml.v3" ) -type InputFormat uint - -const ( - YamlInputFormat = 1 << iota - XMLInputFormat - PropertiesInputFormat -) - -func InputFormatFromString(format string) (InputFormat, error) { - switch format { - case "yaml", "y": - return YamlInputFormat, nil - case "xml", "x": - return XMLInputFormat, nil - case "props", "p": - return PropertiesInputFormat, nil - default: - return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml]", format) - } -} - type xmlDecoder struct { reader io.Reader attributePrefix string @@ -156,6 +134,7 @@ func (dec *xmlDecoder) Decode(rootYamlNode *yaml.Node) error { if err != nil { return err } else if firstNode.Tag == "!!null" { + dec.finished = true return io.EOF } rootYamlNode.Kind = yaml.DocumentNode diff --git a/pkg/yqlib/decoder_yaml.go b/pkg/yqlib/decoder_yaml.go index 58f1c520..37c4ad31 100644 --- a/pkg/yqlib/decoder_yaml.go +++ b/pkg/yqlib/decoder_yaml.go @@ -6,11 +6,6 @@ import ( yaml "gopkg.in/yaml.v3" ) -type Decoder interface { - Init(reader io.Reader) - Decode(node *yaml.Node) error -} - type yamlDecoder struct { decoder yaml.Decoder } diff --git a/pkg/yqlib/doc/usage/properties.md b/pkg/yqlib/doc/usage/properties.md index 97d05760..d5477434 100644 --- a/pkg/yqlib/doc/usage/properties.md +++ b/pkg/yqlib/doc/usage/properties.md @@ -144,3 +144,16 @@ person.pets.0 = dog person.food.0 = pizza ``` +## Empty doc +Given a sample.properties file of: +```properties + +``` +then +```bash +yq -p=props sample.properties +``` +will output +```yaml +``` + diff --git a/pkg/yqlib/properties_test.go b/pkg/yqlib/properties_test.go index bc553839..9716d98f 100644 --- a/pkg/yqlib/properties_test.go +++ b/pkg/yqlib/properties_test.go @@ -90,6 +90,13 @@ var propertyScenarios = []formatScenario{ expected: expectedUpdatedProperties, scenarioType: "roundtrip", }, + { + description: "Empty doc", + skipDoc: true, + input: "", + expected: "", + scenarioType: "decode", + }, } func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {