2022-02-10 01:02:53 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-10-28 03:16:46 +00:00
|
|
|
"fmt"
|
2022-02-10 01:02:53 +00:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/magiconair/properties"
|
|
|
|
)
|
|
|
|
|
|
|
|
type propertiesDecoder struct {
|
|
|
|
reader io.Reader
|
|
|
|
finished bool
|
|
|
|
d DataTreeNavigator
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPropertiesDecoder() Decoder {
|
|
|
|
return &propertiesDecoder{d: NewDataTreeNavigator(), finished: false}
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (dec *propertiesDecoder) Init(reader io.Reader) error {
|
2022-02-10 01:02:53 +00:00
|
|
|
dec.reader = reader
|
|
|
|
dec.finished = false
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (dec *propertiesDecoder) applyPropertyComments(context Context, path []interface{}, comments []string) error {
|
|
|
|
assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}}
|
|
|
|
|
|
|
|
rhsCandidateNode := &CandidateNode{
|
2023-10-18 01:11:53 +00:00
|
|
|
Tag: "!!str",
|
|
|
|
Value: fmt.Sprintf("%v", path[len(path)-1]),
|
|
|
|
HeadComment: dec.processComment(strings.Join(comments, "\n")),
|
|
|
|
Kind: ScalarNode,
|
2022-10-28 03:16:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
rhsCandidateNode.Tag = rhsCandidateNode.guessTagFromCustomType()
|
2022-10-28 03:16:46 +00:00
|
|
|
|
2023-01-12 04:11:45 +00:00
|
|
|
rhsOp := &Operation{OperationType: referenceOpType, CandidateNode: rhsCandidateNode}
|
2022-10-28 03:16:46 +00:00
|
|
|
|
|
|
|
assignmentOpNode := &ExpressionNode{
|
|
|
|
Operation: assignmentOp,
|
|
|
|
LHS: createTraversalTree(path, traversePreferences{}, true),
|
|
|
|
RHS: &ExpressionNode{Operation: rhsOp},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := dec.d.GetMatchingNodes(context, assignmentOpNode)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dec *propertiesDecoder) applyProperty(context Context, properties *properties.Properties, key string) error {
|
2022-02-10 01:02:53 +00:00
|
|
|
value, _ := properties.Get(key)
|
|
|
|
path := parsePropKey(key)
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
propertyComments := properties.GetComments(key)
|
|
|
|
if len(propertyComments) > 0 {
|
|
|
|
err := dec.applyPropertyComments(context, path, propertyComments)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
rhsNode := createStringScalarNode(value)
|
|
|
|
rhsNode.Tag = rhsNode.guessTagFromCustomType()
|
2022-02-10 01:02:53 +00:00
|
|
|
|
2023-03-25 23:59:15 +00:00
|
|
|
return dec.d.DeeplyAssign(context, path, rhsNode)
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (dec *propertiesDecoder) Decode() (*CandidateNode, error) {
|
2022-02-10 01:02:53 +00:00
|
|
|
if dec.finished {
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil, io.EOF
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if _, err := buf.ReadFrom(dec.reader); err != nil {
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil, err
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
if buf.Len() == 0 {
|
|
|
|
dec.finished = true
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil, io.EOF
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
properties, err := properties.LoadString(buf.String())
|
|
|
|
if err != nil {
|
2022-10-28 03:16:46 +00:00
|
|
|
return nil, err
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
2022-03-01 00:29:11 +00:00
|
|
|
properties.DisableExpansion = true
|
2022-02-10 01:02:53 +00:00
|
|
|
|
|
|
|
rootMap := &CandidateNode{
|
2023-10-18 01:11:53 +00:00
|
|
|
Kind: MappingNode,
|
|
|
|
Tag: "!!map",
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
context := Context{}
|
|
|
|
context = context.SingleChildContext(rootMap)
|
|
|
|
|
|
|
|
for _, key := range properties.Keys() {
|
2022-10-28 03:16:46 +00:00
|
|
|
if err := dec.applyProperty(context, properties, key); err != nil {
|
|
|
|
return nil, err
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
dec.finished = true
|
2022-10-28 03:16:46 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
return rootMap, nil
|
2022-02-10 01:02:53 +00:00
|
|
|
|
|
|
|
}
|