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

Ensuring consistent indentation and preserving field order during YAML conversion. #675

Merged
Merged
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
31 changes: 29 additions & 2 deletions encoding/convert.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
package encoding

import (
"bytes"
"fmt"

"gopkg.in/yaml.v3"
)

// ToYaml converts input JSON (or YAML) data into YAML while preserving key order.
// It unmarshalls data into yaml.Node instead of map[string]interface{} because
// maps do not preserve field order.
func ToYaml(data []byte) ([]byte, error) {
var out map[string]interface{}
var out yaml.Node
err := Unmarshal(data, &out)
if err != nil {
return nil, err
}

return yaml.Marshal(out)
if len(out.Content) == 0 {
return nil, fmt.Errorf("No content found in the yaml file.")
}

// Recursively set the style of nodes to block style for readable formatting.
setBlockStyle(out.Content[0])

var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
err = enc.Encode(out.Content[0])
return buf.Bytes(), err
}

// setBlockStyle changes the node and all its children to block style.
// In simple terms, it makes the output print on multiple indented lines
// instead of one single inline line that looks like JSON.
func setBlockStyle(n *yaml.Node) {
n.Style = 0 // Reset style to default (block style).
for _, child := range n.Content {
setBlockStyle(child)
}
}
7 changes: 7 additions & 0 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (

// Unmarshal parses the JSON/YAML data and stores the result in the value pointed to by out
func Unmarshal(data []byte, out interface{}) error {
if node, ok := out.(*yaml.Node); ok {
err := unmarshalYAML(data, node)
if err != nil {
return err
}
}

err := unmarshalJSON(data, out)
if err != nil {
err = unmarshalYAML(data, out)
Expand Down
Loading