This commit is contained in:
Mike Farah
2024-03-12 16:43:53 +11:00
parent 836d50ac9f
commit 1c9f001171
40 changed files with 321 additions and 309 deletions
+2 -15
View File
@@ -49,27 +49,14 @@ func (dec *propertiesDecoder) processComment(c string) string {
}
func (dec *propertiesDecoder) applyPropertyComments(context yqlib.Context, path []interface{}, comments []string) error {
assignmentOp := &yqlib.Operation{OperationType: assignOpType, Preferences: assignPreferences{}}
rhsCandidateNode := &yqlib.CandidateNode{
Tag: "!!str",
Value: fmt.Sprintf("%v", path[len(path)-1]),
HeadComment: dec.processComment(strings.Join(comments, "\n")),
Kind: yqlib.ScalarNode,
}
rhsCandidateNode.Tag = rhsCandidateNode.GuessTagFromCustomType()
rhsOp := &yqlib.Operation{OperationType: referenceOpType, CandidateNode: rhsCandidateNode}
assignmentOpNode := &yqlib.ExpressionNode{
Operation: assignmentOp,
LHS: createTraversalTree(path, traversePreferences{}, true),
RHS: &yqlib.ExpressionNode{Operation: rhsOp},
}
_, err := dec.d.GetMatchingNodes(context, assignmentOpNode)
return err
return dec.d.DeeplyAssignKey(context, path, rhsCandidateNode)
}
func (dec *propertiesDecoder) applyProperty(context yqlib.Context, properties *properties.Properties, key string) error {
@@ -84,7 +71,7 @@ func (dec *propertiesDecoder) applyProperty(context yqlib.Context, properties *p
}
}
rhsNode := createStringScalarNode(value)
rhsNode := yqlib.CreateStringScalarNode(value)
rhsNode.Tag = rhsNode.GuessTagFromCustomType()
return dec.d.DeeplyAssign(context, path, rhsNode)
+7 -7
View File
@@ -44,7 +44,7 @@ func (pe *propertiesEncoder) PrintLeadingContent(writer io.Writer, content strin
}
} else {
if err := writeString(writer, readline); err != nil {
if err := yqlib.WriteString(writer, readline); err != nil {
return err
}
}
@@ -52,7 +52,7 @@ func (pe *propertiesEncoder) PrintLeadingContent(writer io.Writer, content strin
if errors.Is(errReading, io.EOF) {
if readline != "" {
// the last comment we read didn't have a newline, put one in
if err := writeString(writer, "\n"); err != nil {
if err := yqlib.WriteString(writer, "\n"); err != nil {
return err
}
}
@@ -65,10 +65,10 @@ func (pe *propertiesEncoder) PrintLeadingContent(writer io.Writer, content strin
func (pe *propertiesEncoder) Encode(writer io.Writer, node *yqlib.CandidateNode) error {
if node.Kind == yqlib.ScalarNode {
return writeString(writer, node.Value+"\n")
return yqlib.WriteString(writer, node.Value+"\n")
}
mapKeysToStrings(node)
node.ConvertKeysToStrings()
p := properties.NewProperties()
p.WriteSeparator = pe.prefs.KeyValueSeparator
err := pe.doEncode(p, node, "", nil)
@@ -80,14 +80,14 @@ func (pe *propertiesEncoder) Encode(writer io.Writer, node *yqlib.CandidateNode)
return err
}
func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *yqlib.CandidateNode, path string, keyNode *CandidateNode) error {
func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *yqlib.CandidateNode, path string, keyNode *yqlib.CandidateNode) error {
comments := ""
if keyNode != nil {
// include the key node comments if present
comments = headAndLineComment(keyNode)
comments = keyNode.CleanHeadAndLineComment()
}
comments = comments + headAndLineComment(node)
comments = comments + node.CleanHeadAndLineComment()
commentsWithSpaces := strings.ReplaceAll(comments, "\n", "\n ")
p.SetComments(path, strings.Split(commentsWithSpaces, "\n"))
+1 -1
View File
@@ -1,4 +1,4 @@
package yqlib
package properties
import (
"bufio"
+5 -4
View File
@@ -24,15 +24,16 @@ func (p *PropertiesPreferences) Copy() PropertiesPreferences {
}
}
var PropertiesFormat = &yqlib.Format{"props", []string{"p", "properties"},
var PropertiesFormat = &yqlib.Format{"props", []string{"p", "properties"}, "properties",
func() yqlib.Encoder { return NewPropertiesEncoder(ConfiguredPropertiesPreferences) },
func() yqlib.Decoder { return NewPropertiesDecoder() },
nil,
}
var propertyYqRules = []*yqlib.ParticipleYqRule{
{"PropertiesDecode", `from_?props|@propsd`, decodeOp(PropertiesFormat), 0},
{"PropsEncode", `to_?props|@props`, encodeWithIndent(PropertiesFormat, 2), 0},
{"LoadProperties", `load_?props`, loadOp(NewPropertiesDecoder(), false), 0},
{"PropertiesDecode", `from_?props|@propsd`, yqlib.CreateDecodeOpYqAction(PropertiesFormat), 0},
{"PropsEncode", `to_?props|@props`, yqlib.CreateEncodeOpYqAction(PropertiesFormat, 2), 0},
{"LoadProperties", `load_?props`, yqlib.CreateLoadOpYqAction(NewPropertiesDecoder()), 0},
}
func RegisterPropertiesFormat() {