Skip to content

Commit 0f09670

Browse files
committed
Add JSON output support for Go YAML v3 Nodes
Add Go YAML v3 Node type specific code in `ToCompactJSON` to enable the output processor to create valid JSON from this type. Fix missing AliasNode implementation in YAML output.
1 parent b102cb6 commit 0f09670

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

output_json.go

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/gonvenience/bunt"
2929
yamlv2 "gopkg.in/yaml.v2"
30+
yamlv3 "gopkg.in/yaml.v3"
3031
)
3132

3233
// ToJSONString marshals the provided object into JSON with text decorations
@@ -49,14 +50,60 @@ func (p *OutputProcessor) ToJSON(obj interface{}) (string, error) {
4950
return out, nil
5051
}
5152

52-
// ToCompactJSON processed the provided input object and tries to create a as compact
53-
// as possible output.
53+
// ToCompactJSON processed the provided input object and tries to create a as
54+
// compact as possible output
5455
func (p *OutputProcessor) ToCompactJSON(obj interface{}) (string, error) {
55-
switch v := obj.(type) {
56+
switch tobj := obj.(type) {
57+
case *yamlv3.Node:
58+
switch tobj.Kind {
59+
case yamlv3.DocumentNode:
60+
return p.ToCompactJSON(tobj.Content[0])
61+
62+
case yamlv3.MappingNode:
63+
tmp := []string{}
64+
for i := 0; i < len(tobj.Content); i += 2 {
65+
k, v := tobj.Content[i], tobj.Content[i+1]
66+
67+
key, err := p.ToCompactJSON(k)
68+
if err != nil {
69+
return "", err
70+
}
71+
72+
value, err := p.ToCompactJSON(v)
73+
if err != nil {
74+
return "", err
75+
}
76+
77+
tmp = append(tmp, fmt.Sprintf("%s: %s", key, value))
78+
}
79+
80+
return fmt.Sprintf("{%s}", strings.Join(tmp, ", ")), nil
81+
82+
case yamlv3.SequenceNode:
83+
tmp := []string{}
84+
for _, e := range tobj.Content {
85+
entry, err := p.ToCompactJSON(e)
86+
if err != nil {
87+
return "", err
88+
}
89+
90+
tmp = append(tmp, entry)
91+
}
92+
93+
return fmt.Sprintf("[%s]", strings.Join(tmp, ", ")), nil
94+
95+
case yamlv3.ScalarNode:
96+
switch tobj.Tag {
97+
case "!!str":
98+
return fmt.Sprintf("\"%s\"", tobj.Value), nil
99+
}
100+
101+
return tobj.Value, nil
102+
}
56103

57104
case []interface{}:
58105
result := make([]string, 0)
59-
for _, i := range v {
106+
for _, i := range tobj {
60107
value, err := p.ToCompactJSON(i)
61108
if err != nil {
62109
return "", err
@@ -68,7 +115,7 @@ func (p *OutputProcessor) ToCompactJSON(obj interface{}) (string, error) {
68115

69116
case yamlv2.MapSlice:
70117
result := make([]string, 0)
71-
for _, i := range v {
118+
for _, i := range tobj {
72119
value, err := p.ToCompactJSON(i)
73120
if err != nil {
74121
return "", err
@@ -79,26 +126,25 @@ func (p *OutputProcessor) ToCompactJSON(obj interface{}) (string, error) {
79126
return fmt.Sprintf("{%s}", strings.Join(result, ", ")), nil
80127

81128
case yamlv2.MapItem:
82-
key, keyError := p.ToCompactJSON(v.Key)
129+
key, keyError := p.ToCompactJSON(tobj.Key)
83130
if keyError != nil {
84131
return "", keyError
85132
}
86133

87-
value, valueError := p.ToCompactJSON(v.Value)
134+
value, valueError := p.ToCompactJSON(tobj.Value)
88135
if valueError != nil {
89136
return "", valueError
90137
}
91138

92139
return fmt.Sprintf("%s: %s", key, value), nil
140+
}
93141

94-
default:
95-
bytes, err := json.Marshal(v)
96-
if err != nil {
97-
return "", err
98-
}
99-
100-
return string(bytes), nil
142+
bytes, err := json.Marshal(obj)
143+
if err != nil {
144+
return "", err
101145
}
146+
147+
return string(bytes), nil
102148
}
103149

104150
func (p *OutputProcessor) neatJSON(prefix string, obj interface{}) (string, error) {

output_yaml.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ func (p *OutputProcessor) neatYAMLofNode(prefix string, skipIndentOnFirstLine bo
305305
}
306306

307307
case yamlv3.AliasNode:
308-
return fmt.Errorf("kind AliasNode is not implemented yet")
308+
if err := p.neatYAMLofNode(prefix, skipIndentOnFirstLine, node.Alias); err != nil {
309+
return err
310+
}
309311
}
310312

311313
return nil

0 commit comments

Comments
 (0)