/
json.go
316 lines (288 loc) · 9.37 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
// Package typedjson allows encoding and decoding shell syntax trees as JSON.
// The decoding process needs to know what syntax node types to decode into,
// so the "typed JSON" requires "Type" keys in some syntax tree node objects:
//
// - The root node
// - Any node represented as an interface field in the parent Go type
//
// The types of all other nodes can be inferred from context alone.
//
// For the sake of efficiency and simplicity, the "Type" key
// described above must be first in each JSON object.
package typedjson
// TODO: encoding and decoding nodes other than File is untested.
import (
"encoding/json"
"fmt"
"io"
"reflect"
"mvdan.cc/sh/v3/syntax"
)
// Encode is a shortcut for [EncodeOptions.Encode] with the default options.
func Encode(w io.Writer, node syntax.Node) error {
return EncodeOptions{}.Encode(w, node)
}
// EncodeOptions allows configuring how syntax nodes are encoded.
type EncodeOptions struct {
Indent string // e.g. "\t"
// Allows us to add options later.
}
// Encode writes node to w in its typed JSON form,
// as described in the package documentation.
func (opts EncodeOptions) Encode(w io.Writer, node syntax.Node) error {
val := reflect.ValueOf(node)
encVal, tname := encodeValue(val)
if tname == "" {
panic("node did not contain a named type?")
}
encVal.Elem().Field(0).SetString(tname)
enc := json.NewEncoder(w)
if opts.Indent != "" {
enc.SetIndent("", opts.Indent)
}
return enc.Encode(encVal.Interface())
}
func encodeValue(val reflect.Value) (reflect.Value, string) {
switch val.Kind() {
case reflect.Ptr:
if val.IsNil() {
break
}
return encodeValue(val.Elem())
case reflect.Interface:
if val.IsNil() {
break
}
enc, tname := encodeValue(val.Elem())
if tname == "" {
panic("interface did not contain a named type?")
}
enc.Elem().Field(0).SetString(tname)
return enc, ""
case reflect.Struct:
// Construct a new struct with an optional Type, Pos and End,
// and then all the visible fields which aren't positions.
typ := val.Type()
fields := []reflect.StructField{typeField, posField, endField}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
typ := anyType
if field.Type == posType {
typ = exportedPosType
}
fields = append(fields, reflect.StructField{
Name: field.Name,
Type: typ,
Tag: `json:",omitempty"`,
})
}
encTyp := reflect.StructOf(fields)
enc := reflect.New(encTyp).Elem()
// Node methods are defined on struct pointer receivers.
if node, _ := val.Addr().Interface().(syntax.Node); node != nil {
encodePos(enc.Field(1), node.Pos()) // posField
encodePos(enc.Field(2), node.End()) // endField
}
// Do the rest of the fields.
for i := 3; i < encTyp.NumField(); i++ {
ftyp := encTyp.Field(i)
fval := val.FieldByName(ftyp.Name)
if ftyp.Type == exportedPosType {
encodePos(enc.Field(i), fval.Interface().(syntax.Pos))
} else {
encElem, _ := encodeValue(fval)
if encElem.IsValid() {
enc.Field(i).Set(encElem)
}
}
}
// Addr helps prevent an allocation as we use any fields.
return enc.Addr(), typ.Name()
case reflect.Slice:
n := val.Len()
if n == 0 {
break
}
enc := reflect.MakeSlice(anySliceType, n, n)
for i := 0; i < n; i++ {
elem := val.Index(i)
encElem, _ := encodeValue(elem)
enc.Index(i).Set(encElem)
}
return enc, ""
case reflect.Bool:
if val.Bool() {
return val, ""
}
case reflect.String:
if val.String() != "" {
return val, ""
}
case reflect.Uint32:
if val.Uint() != 0 {
return val, ""
}
default:
panic(val.Kind().String())
}
return noValue, ""
}
var (
noValue reflect.Value
anyType = reflect.TypeOf((*any)(nil)).Elem() // any
anySliceType = reflect.SliceOf(anyType) // []any
posType = reflect.TypeOf((*syntax.Pos)(nil)).Elem() // syntax.Pos
exportedPosType = reflect.TypeOf((*exportedPos)(nil)) // *exportedPos
// TODO(v4): derived fields like Type, Pos, and End should have clearly
// different names to prevent confusion. For example: _type, _pos, _end.
typeField = reflect.StructField{
Name: "Type",
Type: reflect.TypeOf((*string)(nil)).Elem(),
Tag: `json:",omitempty"`,
}
posField = reflect.StructField{
Name: "Pos",
Type: exportedPosType,
Tag: `json:",omitempty"`,
}
endField = reflect.StructField{
Name: "End",
Type: exportedPosType,
Tag: `json:",omitempty"`,
}
)
type exportedPos struct {
Offset, Line, Col uint
}
func encodePos(encPtr reflect.Value, val syntax.Pos) {
if !val.IsValid() {
return
}
enc := reflect.New(exportedPosType.Elem())
encPtr.Set(enc)
enc = enc.Elem()
enc.Field(0).SetUint(uint64(val.Offset()))
enc.Field(1).SetUint(uint64(val.Line()))
enc.Field(2).SetUint(uint64(val.Col()))
}
func decodePos(val reflect.Value, enc map[string]any) {
offset := uint(enc["Offset"].(float64))
line := uint(enc["Line"].(float64))
column := uint(enc["Col"].(float64))
val.Set(reflect.ValueOf(syntax.NewPos(offset, line, column)))
}
// Decode is a shortcut for [DecodeOptions.Decode] with the default options.
func Decode(r io.Reader) (syntax.Node, error) {
return DecodeOptions{}.Decode(r)
}
// DecodeOptions allows configuring how syntax nodes are encoded.
type DecodeOptions struct {
// Empty for now; allows us to add options later.
}
// Decode writes node to w in its typed JSON form,
// as described in the package documentation.
func (opts DecodeOptions) Decode(r io.Reader) (syntax.Node, error) {
var enc any
if err := json.NewDecoder(r).Decode(&enc); err != nil {
return nil, err
}
node := new(syntax.Node)
if err := decodeValue(reflect.ValueOf(node).Elem(), enc); err != nil {
return nil, err
}
return *node, nil
}
var nodeByName = map[string]reflect.Type{
"File": reflect.TypeOf((*syntax.File)(nil)).Elem(),
"Word": reflect.TypeOf((*syntax.Word)(nil)).Elem(),
"Lit": reflect.TypeOf((*syntax.Lit)(nil)).Elem(),
"SglQuoted": reflect.TypeOf((*syntax.SglQuoted)(nil)).Elem(),
"DblQuoted": reflect.TypeOf((*syntax.DblQuoted)(nil)).Elem(),
"ParamExp": reflect.TypeOf((*syntax.ParamExp)(nil)).Elem(),
"CmdSubst": reflect.TypeOf((*syntax.CmdSubst)(nil)).Elem(),
"CallExpr": reflect.TypeOf((*syntax.CallExpr)(nil)).Elem(),
"ArithmExp": reflect.TypeOf((*syntax.ArithmExp)(nil)).Elem(),
"ProcSubst": reflect.TypeOf((*syntax.ProcSubst)(nil)).Elem(),
"ExtGlob": reflect.TypeOf((*syntax.ExtGlob)(nil)).Elem(),
"BraceExp": reflect.TypeOf((*syntax.BraceExp)(nil)).Elem(),
"ArithmCmd": reflect.TypeOf((*syntax.ArithmCmd)(nil)).Elem(),
"BinaryCmd": reflect.TypeOf((*syntax.BinaryCmd)(nil)).Elem(),
"IfClause": reflect.TypeOf((*syntax.IfClause)(nil)).Elem(),
"ForClause": reflect.TypeOf((*syntax.ForClause)(nil)).Elem(),
"WhileClause": reflect.TypeOf((*syntax.WhileClause)(nil)).Elem(),
"CaseClause": reflect.TypeOf((*syntax.CaseClause)(nil)).Elem(),
"Block": reflect.TypeOf((*syntax.Block)(nil)).Elem(),
"Subshell": reflect.TypeOf((*syntax.Subshell)(nil)).Elem(),
"FuncDecl": reflect.TypeOf((*syntax.FuncDecl)(nil)).Elem(),
"TestClause": reflect.TypeOf((*syntax.TestClause)(nil)).Elem(),
"DeclClause": reflect.TypeOf((*syntax.DeclClause)(nil)).Elem(),
"LetClause": reflect.TypeOf((*syntax.LetClause)(nil)).Elem(),
"TimeClause": reflect.TypeOf((*syntax.TimeClause)(nil)).Elem(),
"CoprocClause": reflect.TypeOf((*syntax.CoprocClause)(nil)).Elem(),
"TestDecl": reflect.TypeOf((*syntax.TestDecl)(nil)).Elem(),
"UnaryArithm": reflect.TypeOf((*syntax.UnaryArithm)(nil)).Elem(),
"BinaryArithm": reflect.TypeOf((*syntax.BinaryArithm)(nil)).Elem(),
"ParenArithm": reflect.TypeOf((*syntax.ParenArithm)(nil)).Elem(),
"UnaryTest": reflect.TypeOf((*syntax.UnaryTest)(nil)).Elem(),
"BinaryTest": reflect.TypeOf((*syntax.BinaryTest)(nil)).Elem(),
"ParenTest": reflect.TypeOf((*syntax.ParenTest)(nil)).Elem(),
"WordIter": reflect.TypeOf((*syntax.WordIter)(nil)).Elem(),
"CStyleLoop": reflect.TypeOf((*syntax.CStyleLoop)(nil)).Elem(),
}
func decodeValue(val reflect.Value, enc any) error {
switch enc := enc.(type) {
case map[string]any:
if val.Kind() == reflect.Ptr && val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
if typeName, _ := enc["Type"].(string); typeName != "" {
typ := nodeByName[typeName]
if typ == nil {
return fmt.Errorf("unknown type: %q", typeName)
}
val.Set(reflect.New(typ))
}
for val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
val = val.Elem()
}
for name, fv := range enc {
fval := val.FieldByName(name)
switch name {
case "Type", "Pos", "End":
// Type is already used above. Pos and End came from method calls.
continue
}
if !fval.IsValid() {
return fmt.Errorf("unknown field for %s: %q", val.Type(), name)
}
if fval.Type() == posType {
// TODO: don't panic on bad input
decodePos(fval, fv.(map[string]any))
continue
}
if err := decodeValue(fval, fv); err != nil {
return err
}
}
case []any:
for _, encElem := range enc {
elem := reflect.New(val.Type().Elem()).Elem()
if err := decodeValue(elem, encElem); err != nil {
return err
}
val.Set(reflect.Append(val, elem))
}
case float64:
// Tokens and thus operators are uint32, but encoding/json defaults to float64.
// TODO: reject invalid operators.
u := uint64(enc)
val.SetUint(u)
default:
if enc != nil {
val.Set(reflect.ValueOf(enc))
}
}
return nil
}