Skip to content

Commit

Permalink
Fix YAML output issue with empty structures
Browse files Browse the repository at this point in the history
Add checks to spot empty structures such as empty map or empty list
to properly display its empty inline JSON style in the YAML output.
  • Loading branch information
HeavyWombat committed May 8, 2018
1 parent fcc09d6 commit 90281f3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
4 changes: 4 additions & 0 deletions assets/examples/styles-and-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ yaml:
lines to offer
inlinejson: { key: [A, B, C] }
emptyMap: {}
emptyList: []
emptyWhatever-1: ~
emptyWhatever-2: NULL

list:
- one
Expand Down
39 changes: 30 additions & 9 deletions pkg/neat/neat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import (
)

var (
keyColor = bunt.Coral
keyColor = bunt.IndianRed
indentLineColor = bunt.Color(0x00242424)
scalarDefaultColor = bunt.PaleGreen
boolColor = bunt.Moccasin
floatColor = bunt.Orange
intColor = bunt.MediumPurple
multiLineTextColor = bunt.Aquamarine
nullColor = bunt.DarkOrange
emptyStructures = bunt.PaleGoldenrod
)

// ToYAMLString marshals the provided object into YAML with text decorations
Expand All @@ -49,9 +51,7 @@ func ToYAMLString(obj interface{}) (string, error) {
return "", err
}

writer.WriteString("\n")
writer.Flush()

return buf.String(), nil
}

Expand Down Expand Up @@ -91,15 +91,28 @@ func neatMapSlice(out *bufio.Writer, prefix string, skipIndentOnFirstLine bool,

switch mapitem.Value.(type) {
case yaml.MapSlice:
out.WriteString("\n")
if err := neatMapSlice(out, prefix+prefixAdd(), false, mapitem.Value.(yaml.MapSlice)); err != nil {
return err
if len(mapitem.Value.(yaml.MapSlice)) == 0 {
out.WriteString(" ")
out.WriteString(bunt.Colorize("{}", emptyStructures))
out.WriteString("\n")

} else {
out.WriteString("\n")
if err := neatMapSlice(out, prefix+prefixAdd(), false, mapitem.Value.(yaml.MapSlice)); err != nil {
return err
}
}

case []interface{}:
out.WriteString("\n")
if err := neatSlice(out, prefix, false, mapitem.Value.([]interface{})); err != nil {
return err
if len(mapitem.Value.([]interface{})) == 0 {
out.WriteString(" ")
out.WriteString(bunt.Colorize("[]", emptyStructures))
out.WriteString("\n")
} else {
out.WriteString("\n")
if err := neatSlice(out, prefix, false, mapitem.Value.([]interface{})); err != nil {
return err
}
}

default:
Expand Down Expand Up @@ -138,6 +151,14 @@ func neatMapSliceSlice(out *bufio.Writer, prefix string, skipIndentOnFirstLine b
}

func neatScalar(out *bufio.Writer, prefix string, skipIndentOnFirstLine bool, obj interface{}) error {
// Process nil values immediately and return afterwards
if obj == nil {
out.WriteString(bunt.Colorize("null", nullColor))
out.WriteString("\n")
return nil
}

// Any other value: Run through Go YAML marshaller and colorize afterwards
data, err := yaml.Marshal(obj)
if err != nil {
return err
Expand Down

0 comments on commit 90281f3

Please sign in to comment.