-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
181 lines (148 loc) · 3.24 KB
/
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
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
package replvar
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/KarpelesLab/pjson"
"github.com/KarpelesLab/typutil"
)
// Var is a resolvable variable
type Var interface {
Resolve(context.Context) (any, error) // resolve this variable and return its value
IsStatic() bool // if true, it means this var will not change no matter what
}
type staticVar struct {
v any
}
func (s *staticVar) Resolve(context.Context) (any, error) {
return s.v, nil
}
func (s *staticVar) IsStatic() bool {
return true
}
type varConcat []Var
func (a varConcat) Resolve(ctx context.Context) (any, error) {
res := &bytes.Buffer{}
for _, sub := range a {
v, err := sub.Resolve(ctx)
if err != nil {
return nil, err
}
str, _ := typutil.AsString(v)
res.WriteString(str)
}
return res.String(), nil
}
func (a varConcat) IsStatic() bool {
for _, sub := range a {
if !sub.IsStatic() {
return false
}
}
return true
}
type varFetchFromCtx string
func (a varFetchFromCtx) Resolve(ctx context.Context) (any, error) {
return ctx.Value(string(a)), nil
}
func (a varFetchFromCtx) IsStatic() bool {
return false
}
type varPendingToken Token
func (v varPendingToken) Resolve(ctx context.Context) (any, error) {
return nil, errors.New("this value should never happen (pending token)")
}
func (v varPendingToken) IsStatic() bool {
return true
}
type varNull struct{}
func (varNull) Resolve(ctx context.Context) (any, error) {
return nil, nil
}
func (varNull) IsStatic() bool {
return true
}
type varNot struct {
sub Var
}
func (n *varNot) Resolve(ctx context.Context) (any, error) {
sub, err := n.sub.Resolve(ctx)
if err != nil {
return nil, err
}
return !typutil.AsBool(sub), nil
}
func (n *varNot) IsStatic() bool {
return n.sub.IsStatic()
}
type varAccessOffset struct {
sub Var
offset string
}
func (a *varAccessOffset) Resolve(ctx context.Context) (any, error) {
sub, err := a.sub.Resolve(ctx)
if err != nil {
return nil, err
}
switch elem := sub.(type) {
case map[string]any:
return elem[a.offset], nil
case map[string]string:
return elem[a.offset], nil
default:
// ??
return nil, fmt.Errorf("lookup failed, offset=%s cur type=%T", a.offset, elem)
}
}
func (a *varAccessOffset) IsStatic() bool {
return a.sub.IsStatic()
}
type varMath struct {
a, b Var
op string
}
func (m *varMath) Resolve(ctx context.Context) (any, error) {
a, err := m.a.Resolve(ctx)
if err != nil {
return nil, err
}
b, err := m.b.Resolve(ctx)
if err != nil {
return nil, err
}
switch m.op {
case "&&":
// logic and
return typutil.AsBool(a) && typutil.AsBool(b), nil
case "||":
// logic or
return typutil.AsBool(a) || typutil.AsBool(b), nil
case "==":
// equal
return typutil.Equal(a, b), nil
case "!=":
// not equal
return !typutil.Equal(a, b), nil
default:
res, _ := typutil.Math(m.op, a, b)
return res, nil
}
}
func (m *varMath) IsStatic() bool {
return m.a.IsStatic() && m.b.IsStatic()
}
type varJsonMarshal struct {
obj Var
}
func (j *varJsonMarshal) Resolve(ctx context.Context) (any, error) {
res, err := j.obj.Resolve(ctx)
if err != nil {
return nil, err
}
enc, err := pjson.MarshalContext(ctx, res)
return string(enc), err
}
func (j *varJsonMarshal) IsStatic() bool {
return j.obj.IsStatic()
}