Skip to content

Commit

Permalink
Support YAMLv3 library flow style info in JSON output
Browse files Browse the repository at this point in the history
The Go YAML library version 3 stores flow style information, for example whether
JSON inline style (flow style) should be used.

Add helper functions to optionally omit prefix or line break depending on style.
  • Loading branch information
HeavyWombat committed Nov 4, 2020
1 parent e3400c8 commit fce4fdf
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions output_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,35 @@ func (p *OutputProcessor) neatJSON(prefix string, obj interface{}) (string, erro
}

func (p *OutputProcessor) neatJSONofNode(prefix string, node *yamlv3.Node) error {
var (
optionalLineBreak = func() string {
switch node.Style {
case yamlv3.FlowStyle:
return ""
}

return "\n"
}

optionalIndentPrefix = func() string {
switch node.Style {
case yamlv3.FlowStyle:
return ""
}

return prefix + p.prefixAdd()
}

optionalPrefixBeforeEnd = func() string {
switch node.Style {
case yamlv3.FlowStyle:
return ""
}

return prefix
}
)

switch node.Kind {
case yamlv3.DocumentNode:
return p.neatJSONofNode(prefix, node.Content[0])
Expand All @@ -186,13 +215,12 @@ func (p *OutputProcessor) neatJSONofNode(prefix string, node *yamlv3.Node) error
return nil
}

bunt.Fprint(p.out, "*{*\n")
bunt.Fprint(p.out, "*{*", optionalLineBreak())
for i := 0; i < len(node.Content); i += 2 {
k, v := followAlias(node.Content[i]), followAlias(node.Content[i+1])

fmt.Fprint(p.out,
prefix,
p.prefixAdd(),
optionalIndentPrefix(),
p.colorize(`"`+k.Value+`"`, "keyColor"), ": ",
)

Expand All @@ -207,22 +235,22 @@ func (p *OutputProcessor) neatJSONofNode(prefix string, node *yamlv3.Node) error
fmt.Fprint(p.out, ",")
}

fmt.Fprint(p.out, "\n")
fmt.Fprint(p.out, optionalLineBreak())
}
bunt.Fprint(p.out, prefix, "*}*")
bunt.Fprint(p.out, optionalPrefixBeforeEnd(), "*}*")

case yamlv3.SequenceNode:
if len(node.Content) == 0 {
fmt.Fprint(p.out, p.colorize("[]", "emptyStructures"))
return nil
}

bunt.Fprint(p.out, "*[*\n")
bunt.Fprint(p.out, "*[*", optionalLineBreak())
for i := range node.Content {
entry := followAlias(node.Content[i])

if p.isScalar(entry) {
p.neatJSON(prefix+p.prefixAdd(), entry)
p.neatJSON(optionalIndentPrefix(), entry)

} else {
fmt.Fprint(p.out, prefix, p.prefixAdd())
Expand All @@ -233,9 +261,9 @@ func (p *OutputProcessor) neatJSONofNode(prefix string, node *yamlv3.Node) error
fmt.Fprint(p.out, ",")
}

fmt.Fprint(p.out, "\n")
fmt.Fprint(p.out, optionalLineBreak())
}
bunt.Fprint(p.out, prefix, "*]*")
bunt.Fprint(p.out, optionalPrefixBeforeEnd(), "*]*")

case yamlv3.ScalarNode:
obj, err := cast(*node)
Expand All @@ -248,10 +276,12 @@ func (p *OutputProcessor) neatJSONofNode(prefix string, node *yamlv3.Node) error
return err
}

fmt.Fprint(p.out, p.colorize(
string(bytes),
p.determineColorByType(node),
))
fmt.Fprint(p.out,
prefix,
p.colorize(
string(bytes),
p.determineColorByType(node),
))
}

return nil
Expand Down

0 comments on commit fce4fdf

Please sign in to comment.