Skip to content

Commit 7dd783f

Browse files
committed
Adding support for json-like output
1 parent abc9516 commit 7dd783f

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed

config.go

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type output struct {
2929
Indent string `long:"indent" description:"indent string" default:"\t"`
3030
ShowTypes bool `long:"show-types" short:"t" description:"show types"`
3131
Colorized bool
32+
JSON bool `long:"json" description:"json-style output"`
3233
}
3334

3435
func readConfig() config {
@@ -47,6 +48,15 @@ func readConfig() config {
4748
os.Exit(statusUsage)
4849
}
4950

51+
if c.JSON && c.ShowTypes {
52+
fmt.Fprintf(os.Stderr, "Incompatible options --json and --show-types\n")
53+
os.Exit(statusUsage)
54+
}
55+
if c.JSON && c.OutputReport {
56+
fmt.Fprintf(os.Stderr, "Incompatible options --json and --report\n")
57+
os.Exit(statusUsage)
58+
}
59+
5060
c.output.Colorized = terminal.IsTerminal(int(os.Stdout.Fd()))
5161

5262
return c

diff/map.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ func (m mapDiff) StringIndent(keyprefix, prefix string, conf Output) string {
147147
case Identical:
148148
return " " + prefix + keyprefix + conf.white(m.lhs)
149149
case TypesDiffer:
150-
return "-" + prefix + keyprefix + conf.red(m.lhs) + "\n" +
150+
return "-" + prefix + keyprefix + conf.red(m.lhs) + newLineSeparatorString(conf) +
151151
"+" + prefix + keyprefix + conf.green(m.rhs)
152152
case ContentDiffer:
153-
var ss = []string{" " + prefix + keyprefix + conf.typ(m.lhs) + "map["}
153+
var ss = []string{}
154154
var keys []interface{}
155155

156156
for key := range m.diffs {
@@ -164,19 +164,45 @@ func (m mapDiff) StringIndent(keyprefix, prefix string, conf Output) string {
164164
for _, key := range keys {
165165
d := m.diffs[key]
166166

167-
keyStr := fmt.Sprintf("%v: ", key)
167+
keyStr := m.mapKeyString(key, conf)
168168
s := d.StringIndent(keyStr, prefix+conf.Indent, conf)
169169
if s != "" {
170170
ss = append(ss, s)
171171
}
172172
}
173173

174-
return strings.Join(append(ss, " "+prefix+"]"), "\n")
174+
return strings.Join([]string{
175+
m.openString(keyprefix, prefix, conf),
176+
strings.Join(ss, newLineSeparatorString(conf)),
177+
m.closeString(prefix, conf),
178+
}, "\n")
175179
}
176180

177181
return ""
178182
}
179183

184+
func (m mapDiff) openString(keyprefix, prefix string, conf Output) string {
185+
if conf.JSON {
186+
return " " + prefix + keyprefix + "{"
187+
}
188+
return " " + prefix + keyprefix + conf.typ(m.lhs) + "map["
189+
}
190+
191+
func (m mapDiff) closeString(prefix string, conf Output) string {
192+
if conf.JSON {
193+
return " " + prefix + "}"
194+
}
195+
return " " + prefix + "]"
196+
}
197+
198+
func (m mapDiff) mapKeyString(key interface{}, conf Output) string {
199+
if conf.JSON {
200+
return fmt.Sprintf("%q: ", key)
201+
}
202+
203+
return fmt.Sprintf("%v: ", key)
204+
}
205+
180206
func (m mapDiff) Walk(path string, fn WalkFn) error {
181207
var keys []interface{}
182208

diff/output.go

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package diff
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/fatih/color"
@@ -11,13 +12,16 @@ type Output struct {
1112
Indent string
1213
ShowTypes bool
1314
Colorized bool
15+
JSON bool
1416
}
1517

1618
func (o Output) red(v interface{}) string {
1719
var s string
1820

1921
if o.ShowTypes {
2022
s = fmt.Sprintf("%T %v", v, v)
23+
} else if o.JSON {
24+
s = jsonString(v)
2125
} else {
2226
s = fmt.Sprintf("%v", v)
2327
}
@@ -34,6 +38,8 @@ func (o Output) green(v interface{}) string {
3438

3539
if o.ShowTypes {
3640
s = fmt.Sprintf("%T %v", v, v)
41+
} else if o.JSON {
42+
s = jsonString(v)
3743
} else {
3844
s = fmt.Sprintf("%v", v)
3945
}
@@ -50,6 +56,8 @@ func (o Output) white(v interface{}) string {
5056

5157
if o.ShowTypes {
5258
s = fmt.Sprintf("%T %v", v, v)
59+
} else if o.JSON {
60+
s = jsonString(v)
5361
} else {
5462
s = fmt.Sprintf("%v", v)
5563
}
@@ -64,3 +72,20 @@ func (o Output) typ(v interface{}) string {
6472

6573
return ""
6674
}
75+
76+
func newLineSeparatorString(conf Output) string {
77+
if conf.JSON {
78+
return ",\n"
79+
}
80+
81+
return "\n"
82+
}
83+
84+
func jsonString(v interface{}) string {
85+
b, err := json.Marshal(v)
86+
if err != nil {
87+
panic(fmt.Errorf("unexpected error marshaling value: %s", err))
88+
}
89+
90+
return string(b)
91+
}

diff/scalar.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s scalar) StringIndent(key, prefix string, conf Output) string {
4242
return " " + prefix + key + conf.white(s.lhs)
4343
}
4444

45-
return "-" + prefix + key + conf.red(s.lhs) + "\n" +
45+
return "-" + prefix + key + conf.red(s.lhs) + newLineSeparatorString(conf) +
4646
"+" + prefix + key + conf.green(s.rhs)
4747
}
4848

diff/slice.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ func (s slice) StringIndent(key, prefix string, conf Output) string {
213213
case Identical:
214214
return " " + prefix + key + conf.white(s.lhs)
215215
case TypesDiffer:
216-
return "-" + prefix + key + conf.red(s.lhs) + "\n" +
216+
return "-" + prefix + key + conf.red(s.lhs) + newLineSeparatorString(conf) +
217217
"+" + prefix + key + conf.green(s.rhs)
218218
case ContentDiffer:
219-
var ss = []string{" " + prefix + key + conf.typ(s.lhs) + "["}
219+
var ss = []string{}
220220

221221
for _, d := range s.diffs {
222222
s := d.StringIndent("", prefix+conf.Indent, conf)
@@ -225,12 +225,26 @@ func (s slice) StringIndent(key, prefix string, conf Output) string {
225225
}
226226
}
227227

228-
return strings.Join(append(ss, " "+prefix+"]"), "\n")
228+
return strings.Join(
229+
[]string{
230+
s.openString(key, prefix, conf),
231+
strings.Join(ss, newLineSeparatorString(conf)),
232+
" " + prefix + "]",
233+
}, "\n",
234+
)
229235
}
230236

231237
return ""
232238
}
233239

240+
func (s slice) openString(key, prefix string, conf Output) string {
241+
if conf.JSON {
242+
return " " + prefix + key + "["
243+
}
244+
245+
return " " + prefix + key + conf.typ(s.lhs) + "["
246+
}
247+
234248
func (s slice) Walk(path string, fn WalkFn) error {
235249
for i, diff := range s.diffs {
236250
d, err := walk(s, diff, path+"[]", fn)

diff/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (t types) Strings() []string {
2121
}
2222

2323
func (t types) StringIndent(key, prefix string, conf Output) string {
24-
return "-" + prefix + key + conf.red(t.lhs) + "\n" +
24+
return "-" + prefix + key + conf.red(t.lhs) + newLineSeparatorString(conf) +
2525
"+" + prefix + key + conf.green(t.rhs)
2626
}
2727

0 commit comments

Comments
 (0)