-
Notifications
You must be signed in to change notification settings - Fork 78
/
json.go
149 lines (131 loc) · 3.27 KB
/
json.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package spruce
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"github.com/starkandwayne/goutils/ansi"
"github.com/geofffranks/simpleyaml"
. "github.com/geofffranks/spruce/log"
)
func jsonifyData(data []byte, strict bool) (string, error) {
y, err := simpleyaml.NewYaml(data)
if err != nil {
return "", err
}
doc, err := y.Map()
if err != nil {
return "", ansi.Errorf("@R{Root of YAML document is not a hash/map}: %s\n", err.Error())
}
doc_, err := deinterface(doc, strict)
if err != nil {
return "", err
}
b, err := json.Marshal(doc_)
if err != nil {
return "", err
}
return string(b), nil
}
func JSONifyIO(in io.Reader, strict bool) (string, error) {
data, err := io.ReadAll(in)
if err != nil {
return "", ansi.Errorf("@R{Error reading input}: %s", err)
}
return jsonifyData(data, strict)
}
func JSONifyFiles(paths []string, strict bool) ([]string, error) {
l := []string{}
var err error
for _, path := range paths {
data := []byte{}
if path == "-" {
DEBUG("Processing STDIN")
stat, err := os.Stdin.Stat()
if err != nil {
return nil, ansi.Errorf("@R{Error statting STDIN} - Bailing out: %s\n", err.Error())
}
if stat.Mode()&os.ModeCharDevice == 0 {
data, err = io.ReadAll(os.Stdin)
if err != nil {
return nil, ansi.Errorf("@R{Error reading STDIN}: %s\n", err.Error())
}
}
} else {
DEBUG("Processing file '%s'", path)
data, err = os.ReadFile(path)
if err != nil {
return nil, ansi.Errorf("@R{Error reading file} @m{%s}: %s", path, err)
}
}
docs := bytes.Split(data, []byte("\n---\n"))
// strip off empty document created if the first three bytes of the file are the doc separator
// keeps the indexing correct for when used with error messages
if len(docs[0]) == 0 {
docs = docs[1:]
}
for i, doc := range docs {
jsonData, err := jsonifyData(doc, strict)
if err != nil {
return nil, ansi.Errorf("%s[%d]: %s", path, i, err)
}
l = append(l, jsonData)
}
}
return l, nil
}
func deinterface(o interface{}, strict bool) (interface{}, error) {
switch o.(type) {
case map[interface{}]interface{}:
return deinterfaceMap(o.(map[interface{}]interface{}), strict)
case []interface{}:
return deinterfaceList(o.([]interface{}), strict)
default:
return o, nil
}
}
func addKeyToMap(m map[string]interface{}, k interface{}, v interface{}, strict bool) error {
vs := fmt.Sprintf("%v", k)
_, exists := m[vs]
if exists {
NewWarningError(eContextAll, "@Y{Duplicate key detected: %s}", vs).Warn()
return nil
}
dv, err := deinterface(v, strict)
if err != nil {
return err
}
m[vs] = dv
return nil
}
func deinterfaceMap(o map[interface{}]interface{}, strict bool) (map[string]interface{}, error) {
m := map[string]interface{}{}
for k, v := range o {
switch k.(type) {
case string:
err := addKeyToMap(m, k, v, strict)
if err != nil {
return nil, err
}
default:
if strict {
return nil, fmt.Errorf("Non-string keys found during strict JSON conversion")
} else {
addKeyToMap(m, k, v, strict)
}
}
}
return m, nil
}
func deinterfaceList(o []interface{}, strict bool) ([]interface{}, error) {
l := make([]interface{}, len(o))
for i, v := range o {
v_, err := deinterface(v, strict)
if err != nil {
return nil, err
}
l[i] = v_
}
return l, nil
}