-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (63 loc) · 1.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"sort"
"github.com/olekukonko/tablewriter"
)
func has(m []string, name string) bool {
for _, s := range m {
if s == name {
return true
}
}
return false
}
func main() {
var v interface{}
dec := json.NewDecoder(os.Stdin)
dec.UseNumber()
err := dec.Decode(&v)
if err != nil {
log.Fatal(err)
}
arr, ok := v.([]interface{})
if !ok {
log.Fatal("input stream should be array")
}
keys := []string{}
for _, ai := range arr {
elem, ok := ai.(map[string]interface{})
if !ok {
log.Fatal("input stream should be array of objects")
}
for k := range elem {
if !has(keys, k) {
keys = append(keys, k)
}
}
}
sort.Strings(keys)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(keys)
table.SetAutoFormatHeaders(false)
for _, ai := range arr {
items := make([]string, len(keys))
elem, _ := ai.(map[string]interface{})
for i, k := range keys {
if vv, ok := elem[k]; ok {
if _, ok := vv.(map[string]interface{}); ok {
items[i] = "..."
} else if _, ok := vv.([]interface{}); ok {
items[i] = "..."
} else {
items[i] = fmt.Sprint(vv)
}
}
}
table.Append(items)
}
table.Render()
}