diff --git a/examples/small.properties b/examples/small.properties index fbb6df9f..627bee97 100644 --- a/examples/small.properties +++ b/examples/small.properties @@ -1,2 +1,6 @@ # great huh -this.is = a properties file \ No newline at end of file + +# things and stuff +this.is = a properties file +# another thing +this.another = a properties file diff --git a/pkg/yqlib/decoder_properties.go b/pkg/yqlib/decoder_properties.go index ae1b04e4..e6aa3288 100644 --- a/pkg/yqlib/decoder_properties.go +++ b/pkg/yqlib/decoder_properties.go @@ -51,11 +51,13 @@ func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, c value, _ := properties.Get(key) path := parsePropKey(key) + log.Debug("comments: %v", properties.GetComments(key)) + log.Debug("comment: %v", properties.GetComment(key)) rhsNode := &yaml.Node{ Value: value, Tag: "!!str", Kind: yaml.ScalarNode, - LineComment: dec.processComment(properties.GetComment(key)), + LineComment: dec.processComment(strings.Join(properties.GetComments(key), "\n")), } rhsNode.Tag = guessTagFromCustomType(rhsNode) diff --git a/pkg/yqlib/doc/operators/load.md b/pkg/yqlib/doc/operators/load.md index 79dc0149..ff76f442 100644 --- a/pkg/yqlib/doc/operators/load.md +++ b/pkg/yqlib/doc/operators/load.md @@ -154,7 +154,9 @@ will output cool: things more_stuff: this: - is: a properties file + is: a properties file # great huh + # things and stuff + another: a properties file # another thing ``` ## Merge from properties @@ -173,8 +175,10 @@ yq '. *= load_props("../../examples/small.properties")' sample.yml will output ```yaml this: - is: a properties file + is: a properties file # great huh + # things and stuff cool: ay + another: a properties file # another thing ``` ## Load from base64 encoded file diff --git a/pkg/yqlib/doc/usage/properties.md b/pkg/yqlib/doc/usage/properties.md index e676ca1c..d9331aa8 100644 --- a/pkg/yqlib/doc/usage/properties.md +++ b/pkg/yqlib/doc/usage/properties.md @@ -139,7 +139,8 @@ yq -p=props sample.properties will output ```yaml person: - name: Mike Wazowski # comments on values appear + name: Mike Wazowski # block comments come through + # comments on values appear pets: - cat # comments on array values appear food: @@ -164,6 +165,7 @@ yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties ``` will output ```properties +# block comments come through # comments on values appear person.name = Mike Wazowski diff --git a/pkg/yqlib/encoder_properties.go b/pkg/yqlib/encoder_properties.go index 745f1279..6b922fd9 100644 --- a/pkg/yqlib/encoder_properties.go +++ b/pkg/yqlib/encoder_properties.go @@ -75,7 +75,7 @@ func (pe *propertiesEncoder) Encode(writer io.Writer, node *yaml.Node) error { } func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *yaml.Node, path string) error { - p.SetComment(path, headAndLineComment(node)) + p.SetComment(path, strings.ReplaceAll(headAndLineComment(node), "\n", "\n# ")) switch node.Kind { case yaml.ScalarNode: var nodeValue string diff --git a/pkg/yqlib/properties_test.go b/pkg/yqlib/properties_test.go index b539270f..1bdaa457 100644 --- a/pkg/yqlib/properties_test.go +++ b/pkg/yqlib/properties_test.go @@ -36,7 +36,8 @@ person.pets.0 = cat person.food.0 = pizza ` -const expectedUpdatedProperties = `# comments on values appear +const expectedUpdatedProperties = `# block comments come through +# comments on values appear person.name = Mike Wazowski # comments on array values appear