yq/pkg/properties/decoder_properties.go

118 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-03-12 04:45:08 +00:00
package properties
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"github.com/magiconair/properties"
2024-03-12 04:45:08 +00:00
"github.com/mikefarah/yq/v4/pkg/yqlib"
)
type propertiesDecoder struct {
reader io.Reader
finished bool
2024-03-12 04:45:08 +00:00
d yqlib.DataTreeNavigator
}
2024-03-12 04:45:08 +00:00
func NewPropertiesDecoder() yqlib.Decoder {
return &propertiesDecoder{d: yqlib.NewDataTreeNavigator(), finished: false}
}
func (dec *propertiesDecoder) Init(reader io.Reader) error {
dec.reader = reader
dec.finished = false
return nil
}
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) processComment(c string) string {
if c == "" {
return ""
}
return "# " + c
}
2024-03-12 04:45:08 +00:00
func (dec *propertiesDecoder) applyPropertyComments(context yqlib.Context, path []interface{}, comments []string) error {
rhsCandidateNode := &yqlib.CandidateNode{
Tag: "!!str",
Value: fmt.Sprintf("%v", path[len(path)-1]),
HeadComment: dec.processComment(strings.Join(comments, "\n")),
2024-03-12 04:45:08 +00:00
Kind: yqlib.ScalarNode,
}
2024-03-12 04:45:08 +00:00
rhsCandidateNode.Tag = rhsCandidateNode.GuessTagFromCustomType()
2024-03-12 05:43:53 +00:00
return dec.d.DeeplyAssignKey(context, path, rhsCandidateNode)
}
2024-03-12 04:45:08 +00:00
func (dec *propertiesDecoder) applyProperty(context yqlib.Context, properties *properties.Properties, key string) error {
value, _ := properties.Get(key)
path := parsePropKey(key)
propertyComments := properties.GetComments(key)
if len(propertyComments) > 0 {
err := dec.applyPropertyComments(context, path, propertyComments)
if err != nil {
return nil
}
}
2024-03-12 05:43:53 +00:00
rhsNode := yqlib.CreateStringScalarNode(value)
2024-03-12 04:45:08 +00:00
rhsNode.Tag = rhsNode.GuessTagFromCustomType()
return dec.d.DeeplyAssign(context, path, rhsNode)
}
2024-03-12 04:45:08 +00:00
func (dec *propertiesDecoder) Decode() (*yqlib.CandidateNode, error) {
if dec.finished {
return nil, io.EOF
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(dec.reader); err != nil {
return nil, err
}
if buf.Len() == 0 {
dec.finished = true
return nil, io.EOF
}
properties, err := properties.LoadString(buf.String())
if err != nil {
return nil, err
}
properties.DisableExpansion = true
2024-03-12 04:45:08 +00:00
rootMap := &yqlib.CandidateNode{
Kind: yqlib.MappingNode,
Tag: "!!map",
}
2024-03-12 04:45:08 +00:00
context := yqlib.Context{}
context = context.SingleChildContext(rootMap)
for _, key := range properties.Keys() {
if err := dec.applyProperty(context, properties, key); err != nil {
return nil, err
}
}
dec.finished = true
return rootMap, nil
}