Fixed property decode for github actions

This commit is contained in:
Mike Farah 2022-02-10 11:34:33 +11:00
parent e0d6b45651
commit 3ac531ce74
7 changed files with 77 additions and 27 deletions

View File

@ -23,6 +23,23 @@ EOM
assertEquals "$expected" "$X" assertEquals "$expected" "$X"
} }
testInputPropertiesGitHubAction() {
cat >test.properties <<EOL
mike.things = hello
EOL
read -r -d '' expected << EOM
mike:
things: hello
EOM
X=$(cat /dev/null | ./yq e -p=props test.properties)
assertEquals "$expected" "$X"
X=$(cat /dev/null | ./yq ea -p=props test.properties)
assertEquals "$expected" "$X"
}
testInputXml() { testInputXml() {
cat >test.yml <<EOL cat >test.yml <<EOL
<cat legs="4">BiBi</cat> <cat legs="4">BiBi</cat>

34
pkg/yqlib/decoder.go Normal file
View File

@ -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)
}
}

View File

@ -83,9 +83,14 @@ func (dec *propertiesDecoder) Decode(rootYamlNode *yaml.Node) error {
return io.EOF return io.EOF
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(dec.reader); err != nil { if _, err := buf.ReadFrom(dec.reader); err != nil {
return err return err
} }
if buf.Len() == 0 {
dec.finished = true
return io.EOF
}
properties, err := properties.LoadString(buf.String()) properties, err := properties.LoadString(buf.String())
if err != nil { if err != nil {
return err return err

View File

@ -2,7 +2,6 @@ package yqlib
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
"io" "io"
"strings" "strings"
"unicode" "unicode"
@ -11,27 +10,6 @@ import (
yaml "gopkg.in/yaml.v3" 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 { type xmlDecoder struct {
reader io.Reader reader io.Reader
attributePrefix string attributePrefix string
@ -156,6 +134,7 @@ func (dec *xmlDecoder) Decode(rootYamlNode *yaml.Node) error {
if err != nil { if err != nil {
return err return err
} else if firstNode.Tag == "!!null" { } else if firstNode.Tag == "!!null" {
dec.finished = true
return io.EOF return io.EOF
} }
rootYamlNode.Kind = yaml.DocumentNode rootYamlNode.Kind = yaml.DocumentNode

View File

@ -6,11 +6,6 @@ import (
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
type Decoder interface {
Init(reader io.Reader)
Decode(node *yaml.Node) error
}
type yamlDecoder struct { type yamlDecoder struct {
decoder yaml.Decoder decoder yaml.Decoder
} }

View File

@ -144,3 +144,16 @@ person.pets.0 = dog
person.food.0 = pizza person.food.0 = pizza
``` ```
## Empty doc
Given a sample.properties file of:
```properties
```
then
```bash
yq -p=props sample.properties
```
will output
```yaml
```

View File

@ -90,6 +90,13 @@ var propertyScenarios = []formatScenario{
expected: expectedUpdatedProperties, expected: expectedUpdatedProperties,
scenarioType: "roundtrip", scenarioType: "roundtrip",
}, },
{
description: "Empty doc",
skipDoc: true,
input: "",
expected: "",
scenarioType: "decode",
},
} }
func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) { func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {