Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle formatting of empty but not nil structs #65

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/yam/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func Test_formatSingleFile(t *testing.T) {
{
fixture: "testdata/format/whitespace_issues.yaml",
},
{
fixture: "testdata/format/update.yaml",
},
}

for _, tt := range cases {
Expand Down
69 changes: 35 additions & 34 deletions pkg/yam/formatted/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ func (enc Encoder) marshal(node *yaml.Node, nodePath path.Path) ([]byte, error)
return bytes, nil

case yaml.MappingNode:
// Handle empty mappings explicitly
//if len(node.Content) == 0 {
// return []byte("{}"), nil
//}

// Marshal non-empty mappings
Comment on lines +202 to +207
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed but will close this PR anyways in favour of #66

return enc.marshalMapping(node, nodePath)

case yaml.SequenceNode:
Expand All @@ -212,7 +218,6 @@ func (enc Encoder) marshal(node *yaml.Node, nodePath path.Path) ([]byte, error)

default:
return yaml.Marshal(node)

}
}

Expand All @@ -221,49 +226,49 @@ func (enc Encoder) marshalMapping(node *yaml.Node, nodePath path.Path) ([]byte,

var result []byte
var latestKey string
for i, item := range node.Content {
if isMapKeyIndex(i) {
rawKeyBytes, err := enc.marshal(item, nodePath)
if err != nil {
return nil, err
}

// assume the key can be a string (this isn't always true in YAML, but we'll see how far this gets us)
key := bytes.TrimSuffix(rawKeyBytes, newline)
latestKey = string(key)

keyBytes := bytes.Join([][]byte{
key,
colon,
}, nil)
for i := 0; i < len(node.Content); i += 2 {
keyNode := node.Content[i]
valueNode := node.Content[i+1]

if nextItem := node.Content[i+1]; nextItem.Kind == yaml.ScalarNode && nextItem.Tag != "!!null" { // TODO: check that there is a value node for this key node
// render in same line
keyBytes = append(keyBytes, space...)
} else {
keyBytes = append(keyBytes, newline...)
}
rawKeyBytes, err := enc.marshal(keyNode, nodePath)
if err != nil {
return nil, err
}

result = append(result, keyBytes...)
continue
key := bytes.TrimSuffix(rawKeyBytes, newline)
latestKey = string(key)

keyBytes := bytes.Join([][]byte{
key,
colon,
}, nil)

// Check for empty mapping node and handle it
if valueNode.Kind == yaml.MappingNode && len(valueNode.Content) == 0 {
keyBytes = append(keyBytes, space...)
keyBytes = append(keyBytes, []byte("{}")...)
} else if valueNode.Kind == yaml.ScalarNode && valueNode.Tag != "!!null" {
// Render scalar values in the same line
keyBytes = append(keyBytes, space...)
} else {
keyBytes = append(keyBytes, newline...)
}

nodePathForValue := nodePath.AppendMapPart(latestKey)
result = append(result, keyBytes...)

valueBytes, err := enc.marshal(item, nodePathForValue)
valueBytes, err := enc.marshal(valueNode, nodePath.AppendMapPart(latestKey))
if err != nil {
return nil, err
}

isFinalMapValue := i == len(node.Content)-1
isFinalMapValue := (i + 1) == len(node.Content)-1

// This was the key's value node, so add a gap if configured to do so.
// We shouldn't add a newline after the final map value, though.
// Avoid adding a newline after the final map value
if enc.matchesAnyGapPath(nodePath) && !isFinalMapValue {
valueBytes = append(valueBytes, newline...)
}

if item.Kind == yaml.MappingNode || item.Kind == yaml.SequenceNode {
if valueNode.Kind == yaml.MappingNode || valueNode.Kind == yaml.SequenceNode {
valueBytes = enc.applyIndent(valueBytes)
} else {
valueBytes = enc.handleMultilineStringIndentation(valueBytes)
Expand All @@ -275,10 +280,6 @@ func (enc Encoder) marshalMapping(node *yaml.Node, nodePath path.Path) ([]byte,
return result, nil
}

func isMapKeyIndex(i int) bool {
return i%2 == 0
}

func (enc Encoder) marshalSequence(node *yaml.Node, nodePath path.Path) ([]byte, error) {
var lines [][]byte

Expand Down
6 changes: 6 additions & 0 deletions pkg/yam/testdata/format/update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
update:
enabled: true
git: {}
schedule:
daily: true
reason: upstream does not maintain tags or releases, it uses a branch
6 changes: 6 additions & 0 deletions pkg/yam/testdata/format/update_expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
update:
enabled: true
git: {}
schedule:
daily: true
reason: upstream does not maintain tags or releases, it uses a branch