Skip to content

Commit 3ea2bbf

Browse files
committed
Fix JSON output for dyff
With the new version of `dyff`, the Go YAML v3 `Node` based input was not created properly. Update JSON marshalling code to properly create JSON output using the new Go YAML v3 `Node` input.
1 parent 0f09670 commit 3ea2bbf

File tree

2 files changed

+133
-14
lines changed

2 files changed

+133
-14
lines changed

output.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/gonvenience/bunt"
2929
colorful "github.com/lucasb-eyer/go-colorful"
3030
yamlv2 "gopkg.in/yaml.v2"
31+
yamlv3 "gopkg.in/yaml.v3"
3132
)
3233

3334
// DefaultColorSchema is a prepared usable color schema for the neat output
@@ -92,6 +93,23 @@ func (p *OutputProcessor) determineColorByType(obj interface{}) string {
9293
color := "scalarDefaultColor"
9394

9495
switch t := obj.(type) {
96+
case *yamlv3.Node:
97+
switch t.Tag {
98+
case "!!str":
99+
if len(strings.Split(strings.TrimSpace(t.Value), "\n")) > 1 {
100+
color = "multiLineTextColor"
101+
}
102+
103+
case "!!int":
104+
color = "intColor"
105+
106+
case "!!float":
107+
color = "floatColor"
108+
109+
case "!!bool":
110+
color = "boolColor"
111+
}
112+
95113
case bool:
96114
color = "boolColor"
97115

@@ -111,7 +129,10 @@ func (p *OutputProcessor) determineColorByType(obj interface{}) string {
111129
}
112130

113131
func (p *OutputProcessor) isScalar(obj interface{}) bool {
114-
switch obj.(type) {
132+
switch tobj := obj.(type) {
133+
case *yamlv3.Node:
134+
return tobj.Kind == yamlv3.ScalarNode
135+
115136
case yamlv2.MapSlice, []interface{}, []yamlv2.MapSlice:
116137
return false
117138

@@ -136,3 +157,11 @@ func (p *OutputProcessor) prefixAdd() string {
136157

137158
return p.colorize(" ", "indentLineColor")
138159
}
160+
161+
func followAlias(node *yamlv3.Node) *yamlv3.Node {
162+
if node != nil && node.Alias != nil {
163+
return followAlias(node.Alias)
164+
}
165+
166+
return node
167+
}

output_json.go

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,120 @@ func (p *OutputProcessor) ToCompactJSON(obj interface{}) (string, error) {
148148
}
149149

150150
func (p *OutputProcessor) neatJSON(prefix string, obj interface{}) (string, error) {
151+
var err error
152+
151153
switch t := obj.(type) {
154+
case *yamlv3.Node:
155+
err = p.neatJSONofNode(prefix, t)
156+
152157
case yamlv2.MapSlice:
153-
if err := p.neatJSONofYAMLMapSlice(prefix, t); err != nil {
154-
return "", err
155-
}
158+
err = p.neatJSONofYAMLMapSlice(prefix, t)
156159

157160
case []interface{}:
158-
if err := p.neatJSONofSlice(prefix, t); err != nil {
159-
return "", err
161+
err = p.neatJSONofSlice(prefix, t)
162+
163+
default:
164+
err = p.neatJSONofScalar(prefix, obj)
165+
}
166+
167+
if err != nil {
168+
return "", err
169+
}
170+
171+
p.out.Flush()
172+
return p.data.String(), nil
173+
}
174+
175+
func (p *OutputProcessor) neatJSONofNode(prefix string, node *yamlv3.Node) error {
176+
switch node.Kind {
177+
case yamlv3.DocumentNode:
178+
return p.neatJSONofNode(prefix, node.Content[0])
179+
180+
case yamlv3.MappingNode:
181+
if len(node.Content) == 0 {
182+
fmt.Fprint(p.out, p.colorize("{}", "emptyStructures"))
183+
return nil
160184
}
161185

162-
case []yamlv2.MapSlice:
163-
if err := p.neatJSONofSlice(prefix, p.simplify(t)); err != nil {
164-
return "", err
186+
bunt.Fprint(p.out, "*{*\n")
187+
for i := 0; i < len(node.Content); i += 2 {
188+
k, v := followAlias(node.Content[i]), followAlias(node.Content[i+1])
189+
190+
fmt.Fprint(p.out,
191+
prefix,
192+
p.prefixAdd(),
193+
p.colorize(`"`+k.Value+`"`, "keyColor"), ": ",
194+
)
195+
196+
if p.isScalar(v) {
197+
p.neatJSON("", v)
198+
199+
} else {
200+
p.neatJSON(prefix+p.prefixAdd(), v)
201+
}
202+
203+
if i < len(node.Content)-2 {
204+
fmt.Fprint(p.out, ",")
205+
}
206+
207+
fmt.Fprint(p.out, "\n")
165208
}
209+
bunt.Fprint(p.out, prefix, "*}*")
166210

167-
default:
168-
if err := p.neatJSONofScalar(prefix, obj); err != nil {
169-
return "", nil
211+
case yamlv3.SequenceNode:
212+
if len(node.Content) == 0 {
213+
fmt.Fprint(p.out, p.colorize("[]", "emptyStructures"))
214+
return nil
215+
}
216+
217+
bunt.Fprint(p.out, "*[*\n")
218+
for i := range node.Content {
219+
entry := followAlias(node.Content[i])
220+
221+
if p.isScalar(entry) {
222+
p.neatJSON("", entry)
223+
224+
} else {
225+
fmt.Fprint(p.out, prefix, p.prefixAdd())
226+
p.neatJSON(prefix+p.prefixAdd(), entry)
227+
}
228+
229+
if i < len(node.Content)-1 {
230+
fmt.Fprint(p.out, ",")
231+
}
232+
233+
fmt.Fprint(p.out, "\n")
234+
}
235+
bunt.Fprint(p.out, prefix, "*]*")
236+
237+
case yamlv3.ScalarNode:
238+
if node.Tag == "!!null" {
239+
fmt.Fprint(p.out, p.colorize("null", "nullColor"))
240+
return nil
170241
}
242+
243+
color := p.determineColorByType(node)
244+
quotes := func() string {
245+
if node.Tag == "!!str" {
246+
return p.colorize(`"`, color)
247+
}
248+
249+
return ""
250+
}
251+
252+
fmt.Fprint(p.out, prefix, quotes())
253+
parts := strings.Split(node.Value, "\n")
254+
for idx, part := range parts {
255+
fmt.Fprint(p.out, p.colorize(part, color))
256+
257+
if idx < len(parts)-1 {
258+
fmt.Fprint(p.out, p.colorize("\\n", "emptyStructures"))
259+
}
260+
}
261+
fmt.Fprint(p.out, quotes())
171262
}
172263

173-
p.out.Flush()
174-
return p.data.String(), nil
264+
return nil
175265
}
176266

177267
func (p *OutputProcessor) neatJSONofYAMLMapSlice(prefix string, mapslice yamlv2.MapSlice) error {

0 commit comments

Comments
 (0)