From 7eccbe5902e6eb8b5364e5da43c5c7cc53aeeae5 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 26 Oct 2022 13:24:36 +1100 Subject: [PATCH] put comments on key node instead --- examples/small.properties | 6 ++++- pkg/yqlib/decoder_properties.go | 48 ++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/examples/small.properties b/examples/small.properties index 627bee97..c73af7a6 100644 --- a/examples/small.properties +++ b/examples/small.properties @@ -2,5 +2,9 @@ # things and stuff this.is = a properties file + + +this.cat.0 = free +# this one is important # another thing -this.another = a properties file +this.cat.1 = meow diff --git a/pkg/yqlib/decoder_properties.go b/pkg/yqlib/decoder_properties.go index 1c7eee14..1ec9b069 100644 --- a/pkg/yqlib/decoder_properties.go +++ b/pkg/yqlib/decoder_properties.go @@ -2,6 +2,7 @@ package yqlib import ( "bytes" + "fmt" "io" "strconv" "strings" @@ -47,17 +48,50 @@ func (dec *propertiesDecoder) processComment(c string) string { return "# " + c } -func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, context Context, key string) error { +func (dec *propertiesDecoder) applyPropertyComments(context Context, path []interface{}, comments []string) error { + assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}} + + rhsCandidateNode := &CandidateNode{ + Path: path, + Node: &yaml.Node{ + Tag: "!!str", + Value: fmt.Sprintf("%v", path[len(path)-1]), + HeadComment: dec.processComment(strings.Join(comments, "\n")), + Kind: yaml.ScalarNode, + }, + } + + rhsCandidateNode.Node.Tag = guessTagFromCustomType(rhsCandidateNode.Node) + + rhsOp := &Operation{OperationType: valueOpType, CandidateNode: rhsCandidateNode} + + assignmentOpNode := &ExpressionNode{ + Operation: assignmentOp, + LHS: createTraversalTree(path, traversePreferences{}, true), + RHS: &ExpressionNode{Operation: rhsOp}, + } + + _, err := dec.d.GetMatchingNodes(context, assignmentOpNode) + return err +} + +// TODO: test comment on array +func (dec *propertiesDecoder) applyProperty(context Context, properties *properties.Properties, key string) error { value, _ := properties.Get(key) path := parsePropKey(key) - IMPROVEMENT - target the key node with the comment, set as a header comment instead. + propertyComments := properties.GetComments(key) + if len(propertyComments) > 0 { + err := dec.applyPropertyComments(context, path, propertyComments) + if err != nil { + return nil + } + } rhsNode := &yaml.Node{ - Value: value, - Tag: "!!str", - Kind: yaml.ScalarNode, - LineComment: dec.processComment(strings.Join(properties.GetComments(key), "\n")), + Value: value, + Tag: "!!str", + Kind: yaml.ScalarNode, } rhsNode.Tag = guessTagFromCustomType(rhsNode) @@ -111,7 +145,7 @@ func (dec *propertiesDecoder) Decode() (*CandidateNode, error) { context = context.SingleChildContext(rootMap) for _, key := range properties.Keys() { - if err := dec.applyProperty(properties, context, key); err != nil { + if err := dec.applyProperty(context, properties, key); err != nil { return nil, err }