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
this.is = a properties file
# things and stuff
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)
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)

View File

@ -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

View File

@ -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

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 {
p.SetComment(path, headAndLineComment(node))
p.SetComment(path, strings.ReplaceAll(headAndLineComment(node), "\n", "\n# "))
switch node.Kind {
case yaml.ScalarNode:
var nodeValue string

View File

@ -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