mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-27 00:47:56 +00:00
Decoder Properties
This commit is contained in:
parent
4d4bd5114d
commit
a5ab9a8a0a
@ -54,7 +54,10 @@ func configureDecoder() (yqlib.Decoder, error) {
|
||||
switch yqlibInputFormat {
|
||||
case yqlib.XMLInputFormat:
|
||||
return yqlib.NewXMLDecoder(xmlAttributePrefix, xmlContentName), nil
|
||||
case yqlib.PropertiesInputFormat:
|
||||
return yqlib.NewPropertiesDecoder(), nil
|
||||
}
|
||||
|
||||
return yqlib.NewYamlDecoder(), nil
|
||||
}
|
||||
|
||||
|
6
examples/example.properties
Normal file
6
examples/example.properties
Normal file
@ -0,0 +1,6 @@
|
||||
# comments on values appear
|
||||
person.name = Mike
|
||||
|
||||
# comments on array values appear
|
||||
person.pets.0 = cat
|
||||
person.food.0 = pizza
|
109
pkg/yqlib/decoder_properties.go
Normal file
109
pkg/yqlib/decoder_properties.go
Normal file
@ -0,0 +1,109 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/magiconair/properties"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type propertiesDecoder struct {
|
||||
reader io.Reader
|
||||
finished bool
|
||||
d DataTreeNavigator
|
||||
}
|
||||
|
||||
func NewPropertiesDecoder() Decoder {
|
||||
return &propertiesDecoder{d: NewDataTreeNavigator(), finished: false}
|
||||
}
|
||||
|
||||
func (dec *propertiesDecoder) Init(reader io.Reader) {
|
||||
dec.reader = reader
|
||||
dec.finished = false
|
||||
}
|
||||
|
||||
func parsePropKey(key string) []interface{} {
|
||||
pathStrArray := strings.Split(key, ".")
|
||||
path := make([]interface{}, len(pathStrArray))
|
||||
for i, pathStr := range pathStrArray {
|
||||
num, err := strconv.ParseInt(pathStr, 10, 32)
|
||||
if err == nil {
|
||||
path[i] = num
|
||||
} else {
|
||||
path[i] = pathStr
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, context Context, key string) error {
|
||||
value, _ := properties.Get(key)
|
||||
path := parsePropKey(key)
|
||||
|
||||
rhsNode := &yaml.Node{
|
||||
Value: value,
|
||||
Tag: "!!str",
|
||||
Kind: yaml.ScalarNode,
|
||||
LineComment: properties.GetComment(key),
|
||||
}
|
||||
|
||||
rhsNode.Tag = guessTagFromCustomType(rhsNode)
|
||||
|
||||
rhsCandidateNode := &CandidateNode{
|
||||
Path: path,
|
||||
Node: rhsNode,
|
||||
}
|
||||
|
||||
assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}}
|
||||
|
||||
rhsOp := &Operation{OperationType: valueOpType, CandidateNode: rhsCandidateNode}
|
||||
|
||||
assignmentOpNode := &ExpressionNode{
|
||||
Operation: assignmentOp,
|
||||
LHS: createTraversalTree(path, traversePreferences{}, false),
|
||||
RHS: &ExpressionNode{Operation: rhsOp},
|
||||
}
|
||||
|
||||
_, err := dec.d.GetMatchingNodes(context, assignmentOpNode)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dec *propertiesDecoder) Decode(rootYamlNode *yaml.Node) error {
|
||||
if dec.finished {
|
||||
return io.EOF
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if _, err := buf.ReadFrom(dec.reader); err != nil {
|
||||
return err
|
||||
}
|
||||
properties, err := properties.LoadString(buf.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rootMap := &CandidateNode{
|
||||
Node: &yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Tag: "!!map",
|
||||
},
|
||||
}
|
||||
|
||||
context := Context{}
|
||||
context = context.SingleChildContext(rootMap)
|
||||
|
||||
for _, key := range properties.Keys() {
|
||||
if err := dec.applyProperty(properties, context, key); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
rootYamlNode.Kind = yaml.DocumentNode
|
||||
rootYamlNode.Content = []*yaml.Node{rootMap.Node}
|
||||
dec.finished = true
|
||||
return nil
|
||||
|
||||
}
|
@ -16,6 +16,7 @@ type InputFormat uint
|
||||
const (
|
||||
YamlInputFormat = 1 << iota
|
||||
XMLInputFormat
|
||||
PropertiesInputFormat
|
||||
)
|
||||
|
||||
func InputFormatFromString(format string) (InputFormat, error) {
|
||||
@ -24,6 +25,8 @@ func InputFormatFromString(format string) (InputFormat, error) {
|
||||
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)
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ emptyMap: []
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq -o=props -I=0 '.' sample.yml
|
||||
yq -o=props sample.yml
|
||||
```
|
||||
will output
|
||||
```properties
|
||||
@ -54,7 +54,7 @@ emptyMap: []
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq -o=props -I=0 '... comments = ""' sample.yml
|
||||
yq -o=props '... comments = ""' sample.yml
|
||||
```
|
||||
will output
|
||||
```properties
|
||||
@ -80,7 +80,7 @@ emptyMap: []
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq -o=props -I=0 '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""' sample.yml
|
||||
yq -o=props '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) = ""' sample.yml
|
||||
```
|
||||
will output
|
||||
```properties
|
||||
@ -94,3 +94,28 @@ emptyArray =
|
||||
emptyMap =
|
||||
```
|
||||
|
||||
## Decode properties
|
||||
Given a sample.properties file of:
|
||||
```properties
|
||||
# comments on values appear
|
||||
person.name = Mike
|
||||
|
||||
# comments on array values appear
|
||||
person.pets.0 = cat
|
||||
person.food.0 = pizza
|
||||
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yq -p=props sample.properties
|
||||
```
|
||||
will output
|
||||
```yaml
|
||||
person:
|
||||
name: Mike # comments on values appear
|
||||
pets:
|
||||
- cat # comments on array values appear
|
||||
food:
|
||||
- pizza
|
||||
```
|
||||
|
||||
|
@ -98,19 +98,21 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
|
||||
|
||||
func testJSONScenario(t *testing.T, s formatScenario) {
|
||||
if s.scenarioType == "encode" || s.scenarioType == "roundtrip" {
|
||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJONEncoder(s.indent)), s.description)
|
||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJONEncoder(s.indent), NewYamlDecoder()), s.description)
|
||||
} else {
|
||||
var actual = resultToString(t, decodeJSON(t, s.input))
|
||||
test.AssertResultWithContext(t, s.expected, actual, s.description)
|
||||
}
|
||||
}
|
||||
|
||||
func processFormatScenario(s formatScenario, encoder Encoder) string {
|
||||
func processFormatScenario(s formatScenario, encoder Encoder, decoder Decoder) string {
|
||||
|
||||
var output bytes.Buffer
|
||||
writer := bufio.NewWriter(&output)
|
||||
|
||||
var decoder = NewYamlDecoder()
|
||||
if decoder == nil {
|
||||
decoder = NewYamlDecoder()
|
||||
}
|
||||
|
||||
inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder)
|
||||
if err != nil {
|
||||
@ -212,7 +214,7 @@ func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
|
||||
}
|
||||
writeOrPanic(w, "will output\n")
|
||||
|
||||
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewJONEncoder(s.indent))))
|
||||
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewJONEncoder(s.indent), NewYamlDecoder())))
|
||||
}
|
||||
|
||||
func TestJSONScenarios(t *testing.T) {
|
||||
|
@ -26,6 +26,14 @@ person.pets.0 = cat
|
||||
person.food.0 = pizza
|
||||
`
|
||||
|
||||
var expectedDecodedYaml = `person:
|
||||
name: Mike # comments on values appear
|
||||
pets:
|
||||
- cat # comments on array values appear
|
||||
food:
|
||||
- pizza
|
||||
`
|
||||
|
||||
var expectedPropertiesNoComments = `person.name = Mike
|
||||
person.pets.0 = cat
|
||||
person.food.0 = pizza
|
||||
@ -61,10 +69,16 @@ var propertyScenarios = []formatScenario{
|
||||
input: samplePropertiesYaml,
|
||||
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Decode properties",
|
||||
input: expectedProperties,
|
||||
expected: expectedDecodedYaml,
|
||||
scenarioType: "decode",
|
||||
},
|
||||
}
|
||||
|
||||
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||
s := i.(formatScenario)
|
||||
func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||
|
||||
if s.subdescription != "" {
|
||||
@ -78,23 +92,59 @@ func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||
writeOrPanic(w, "then\n")
|
||||
|
||||
expression := s.expression
|
||||
if expression == "" {
|
||||
expression = "."
|
||||
}
|
||||
|
||||
if s.indent == 2 {
|
||||
if expression != "" {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props '%v' sample.yml\n```\n", expression))
|
||||
} else {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props -I=%v '%v' sample.yml\n```\n", s.indent, expression))
|
||||
writeOrPanic(w, "```bash\nyq -o=props sample.yml\n```\n")
|
||||
}
|
||||
writeOrPanic(w, "will output\n")
|
||||
|
||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesEncoder())))
|
||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder())))
|
||||
}
|
||||
|
||||
func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
||||
|
||||
if s.subdescription != "" {
|
||||
writeOrPanic(w, s.subdescription)
|
||||
writeOrPanic(w, "\n\n")
|
||||
}
|
||||
|
||||
writeOrPanic(w, "Given a sample.properties file of:\n")
|
||||
writeOrPanic(w, fmt.Sprintf("```properties\n%v\n```\n", s.input))
|
||||
|
||||
writeOrPanic(w, "then\n")
|
||||
|
||||
expression := s.expression
|
||||
if expression != "" {
|
||||
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=props '%v' sample.properties\n```\n", expression))
|
||||
} else {
|
||||
writeOrPanic(w, "```bash\nyq -p=props sample.properties\n```\n")
|
||||
}
|
||||
|
||||
writeOrPanic(w, "will output\n")
|
||||
|
||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewYamlEncoder(s.indent, false, true, true), NewPropertiesDecoder())))
|
||||
}
|
||||
|
||||
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||
s := i.(formatScenario)
|
||||
if s.scenarioType == "decode" {
|
||||
documentDecodePropertyScenario(w, s)
|
||||
} else {
|
||||
documentEncodePropertyScenario(w, s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPropertyScenarios(t *testing.T) {
|
||||
for _, s := range propertyScenarios {
|
||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder()), s.description)
|
||||
if s.scenarioType == "decode" {
|
||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlEncoder(2, false, true, true), NewPropertiesDecoder()), s.description)
|
||||
} else {
|
||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder()), s.description)
|
||||
}
|
||||
}
|
||||
genericScenarios := make([]interface{}, len(propertyScenarios))
|
||||
for i, s := range propertyScenarios {
|
||||
|
Loading…
Reference in New Issue
Block a user