fixing property comment handling

This commit is contained in:
Mike Farah 2022-10-25 15:52:13 +11:00
parent 2e588a11a0
commit 23b2aa5668
6 changed files with 20 additions and 7 deletions

View File

@ -1,2 +1,6 @@
# great huh # great huh
# things and stuff
this.is = a properties file this.is = a properties file
# another thing
this.another = a properties file

View File

@ -51,11 +51,13 @@ func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, c
value, _ := properties.Get(key) value, _ := properties.Get(key)
path := parsePropKey(key) path := parsePropKey(key)
log.Debug("comments: %v", properties.GetComments(key))
log.Debug("comment: %v", properties.GetComment(key))
rhsNode := &yaml.Node{ rhsNode := &yaml.Node{
Value: value, Value: value,
Tag: "!!str", Tag: "!!str",
Kind: yaml.ScalarNode, Kind: yaml.ScalarNode,
LineComment: dec.processComment(properties.GetComment(key)), LineComment: dec.processComment(strings.Join(properties.GetComments(key), "\n")),
} }
rhsNode.Tag = guessTagFromCustomType(rhsNode) rhsNode.Tag = guessTagFromCustomType(rhsNode)

View File

@ -154,7 +154,9 @@ will output
cool: things cool: things
more_stuff: more_stuff:
this: this:
is: a properties file is: a properties file # great huh
# things and stuff
another: a properties file # another thing
``` ```
## Merge from properties ## Merge from properties
@ -173,8 +175,10 @@ yq '. *= load_props("../../examples/small.properties")' sample.yml
will output will output
```yaml ```yaml
this: this:
is: a properties file is: a properties file # great huh
# things and stuff
cool: ay cool: ay
another: a properties file # another thing
``` ```
## Load from base64 encoded file ## Load from base64 encoded file

View File

@ -139,7 +139,8 @@ yq -p=props sample.properties
will output will output
```yaml ```yaml
person: person:
name: Mike Wazowski # comments on values appear name: Mike Wazowski # block comments come through
# comments on values appear
pets: pets:
- cat # comments on array values appear - cat # comments on array values appear
food: food:
@ -164,6 +165,7 @@ yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
``` ```
will output will output
```properties ```properties
# block comments come through
# comments on values appear # comments on values appear
person.name = Mike Wazowski person.name = Mike Wazowski

View File

@ -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 { 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 { switch node.Kind {
case yaml.ScalarNode: case yaml.ScalarNode:
var nodeValue string var nodeValue string

View File

@ -36,7 +36,8 @@ person.pets.0 = cat
person.food.0 = pizza person.food.0 = pizza
` `
const expectedUpdatedProperties = `# comments on values appear const expectedUpdatedProperties = `# block comments come through
# comments on values appear
person.name = Mike Wazowski person.name = Mike Wazowski
# comments on array values appear # comments on array values appear