forked from itchyny/gojq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term_type.go
151 lines (144 loc) · 3 KB
/
term_type.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
package gojq
import (
"encoding/json"
"fmt"
)
// TermType represents the type of [Term].
type TermType int
// TermType list.
const (
TermTypeIdentity TermType = iota + 1
TermTypeRecurse
TermTypeNull
TermTypeTrue
TermTypeFalse
TermTypeIndex
TermTypeFunc
TermTypeObject
TermTypeArray
TermTypeNumber
TermTypeUnary
TermTypeFormat
TermTypeString
TermTypeIf
TermTypeTry
TermTypeReduce
TermTypeForeach
TermTypeLabel
TermTypeBreak
TermTypeQuery
)
// GoString implements [fmt.GoStringer].
func TermTypeFromString(s string) TermType {
switch s {
case "TermTypeIdentity":
return TermTypeIdentity
case "TermTypeRecurse":
return TermTypeRecurse
case "TermTypeNull":
return TermTypeNull
case "TermTypeTrue":
return TermTypeTrue
case "TermTypeFalse":
return TermTypeFalse
case "TermTypeIndex":
return TermTypeIndex
case "TermTypeFunc":
return TermTypeFunc
case "TermTypeObject":
return TermTypeObject
case "TermTypeArray":
return TermTypeArray
case "TermTypeNumber":
return TermTypeNumber
case "TermTypeUnary":
return TermTypeUnary
case "TermTypeFormat":
return TermTypeFormat
case "TermTypeString":
return TermTypeString
case "TermTypeIf":
return TermTypeIf
case "TermTypeTry":
return TermTypeTry
case "TermTypeReduce":
return TermTypeReduce
case "TermTypeForeach":
return TermTypeForeach
case "TermTypeLabel":
return TermTypeLabel
case "TermTypeBreak":
return TermTypeBreak
case "TermTypeQuery":
return TermTypeQuery
default:
return 0
}
}
// GoString implements [fmt.GoStringer].
func (termType TermType) GoString() (str string) {
defer func() { str = "gojq." + str }()
switch termType {
case TermTypeIdentity:
return "TermTypeIdentity"
case TermTypeRecurse:
return "TermTypeRecurse"
case TermTypeNull:
return "TermTypeNull"
case TermTypeTrue:
return "TermTypeTrue"
case TermTypeFalse:
return "TermTypeFalse"
case TermTypeIndex:
return "TermTypeIndex"
case TermTypeFunc:
return "TermTypeFunc"
case TermTypeObject:
return "TermTypeObject"
case TermTypeArray:
return "TermTypeArray"
case TermTypeNumber:
return "TermTypeNumber"
case TermTypeUnary:
return "TermTypeUnary"
case TermTypeFormat:
return "TermTypeFormat"
case TermTypeString:
return "TermTypeString"
case TermTypeIf:
return "TermTypeIf"
case TermTypeTry:
return "TermTypeTry"
case TermTypeReduce:
return "TermTypeReduce"
case TermTypeForeach:
return "TermTypeForeach"
case TermTypeLabel:
return "TermTypeLabel"
case TermTypeBreak:
return "TermTypeBreak"
case TermTypeQuery:
return "TermTypeQuery"
default:
panic(termType)
}
}
func (termType TermType) MarshalJSON() ([]byte, error) {
if termType == 0 {
return json.Marshal(nil)
}
// TODO: gojq. skips prefix
return json.Marshal(termType.GoString()[5:])
}
func (termType *TermType) UnmarshalJSON(text []byte) error {
var s string
err := json.Unmarshal(text, &s)
if err != nil {
return err
}
*termType = TermTypeFromString(s)
if *termType == 0 {
return fmt.Errorf("unknown term %v", s)
}
return nil
}