mirror of
https://github.com/mikefarah/yq.git
synced 2025-02-03 20:26:43 +00:00
Properties encoder wip
This commit is contained in:
parent
9fd467590f
commit
b64982a487
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ require (
|
|||||||
github.com/fatih/color v1.10.0
|
github.com/fatih/color v1.10.0
|
||||||
github.com/goccy/go-yaml v1.8.9
|
github.com/goccy/go-yaml v1.8.9
|
||||||
github.com/jinzhu/copier v0.2.8
|
github.com/jinzhu/copier v0.2.8
|
||||||
|
github.com/magiconair/properties v1.8.5
|
||||||
github.com/spf13/cobra v1.1.3
|
github.com/spf13/cobra v1.1.3
|
||||||
github.com/timtadh/data-structures v0.5.3 // indirect
|
github.com/timtadh/data-structures v0.5.3 // indirect
|
||||||
github.com/timtadh/lexmachine v0.2.2
|
github.com/timtadh/lexmachine v0.2.2
|
||||||
|
2
go.sum
2
go.sum
@ -119,6 +119,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
|||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||||
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||||
|
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
|
||||||
|
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
||||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||||
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
|
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
|
||||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||||
|
79
pkg/yqlib/encoder_properties.go
Normal file
79
pkg/yqlib/encoder_properties.go
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package yqlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/magiconair/properties"
|
||||||
|
yaml "gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type propertiesEncoder struct {
|
||||||
|
destination io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPropertiesEncoder(destination io.Writer) Encoder {
|
||||||
|
return &propertiesEncoder{destination}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pe *propertiesEncoder) Encode(node *yaml.Node) error {
|
||||||
|
mapKeysToStrings(node)
|
||||||
|
p := properties.NewProperties()
|
||||||
|
// p.SetComment("a", "hi")
|
||||||
|
// p.Set("a", "hi")
|
||||||
|
err := pe.doEncode(p, node, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = p.WriteComment(pe.destination, "#", properties.UTF8)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *yaml.Node, path string) error {
|
||||||
|
p.SetComment(path, node.HeadComment+node.LineComment)
|
||||||
|
switch node.Kind {
|
||||||
|
case yaml.ScalarNode:
|
||||||
|
p.Set(path, node.Value)
|
||||||
|
return nil
|
||||||
|
case yaml.DocumentNode:
|
||||||
|
return pe.doEncode(p, node.Content[0], path)
|
||||||
|
case yaml.SequenceNode:
|
||||||
|
return pe.encodeArray(p, node.Content, path)
|
||||||
|
case yaml.MappingNode:
|
||||||
|
return pe.encodeMap(p, node.Content, path)
|
||||||
|
case yaml.AliasNode:
|
||||||
|
return pe.doEncode(p, node.Alias, path)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Unsupported node %v", node.Tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pe *propertiesEncoder) appendPath(path string, key interface{}) string {
|
||||||
|
if path == "" {
|
||||||
|
return fmt.Sprintf("%v", key)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%v.%v", path, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pe *propertiesEncoder) encodeArray(p *properties.Properties, kids []*yaml.Node, path string) error {
|
||||||
|
for index, child := range kids {
|
||||||
|
err := pe.doEncode(p, child, pe.appendPath(path, index))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pe *propertiesEncoder) encodeMap(p *properties.Properties, kids []*yaml.Node, path string) error {
|
||||||
|
for index := 0; index < len(kids); index = index + 2 {
|
||||||
|
key := kids[index]
|
||||||
|
value := kids[index+1]
|
||||||
|
err := pe.doEncode(p, value, pe.appendPath(path, key.Value))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user