forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocgen.go
256 lines (205 loc) · 8.05 KB
/
docgen.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
package docgen
import (
"reflect"
"regexp"
"strings"
"github.com/expr-lang/expr/conf"
"github.com/expr-lang/expr/internal/deref"
)
// Kind can be any of array, map, struct, func, string, int, float, bool or any.
type Kind string
// Identifier represents variable names and field names.
type Identifier string
// TypeName is a name of type in types map.
type TypeName string
type Context struct {
Variables map[Identifier]*Type `json:"variables"`
Types map[TypeName]*Type `json:"types"`
PkgPath string
}
type Type struct {
Name TypeName `json:"name,omitempty"`
Kind Kind `json:"kind,omitempty"`
Type *Type `json:"type,omitempty"`
Key *Type `json:"key_type,omitempty"`
Fields map[Identifier]*Type `json:"fields,omitempty"`
Arguments []*Type `json:"arguments,omitempty"`
Return *Type `json:"return,omitempty"`
}
var (
Operators = []string{"matches", "contains", "startsWith", "endsWith"}
Builtins = map[Identifier]*Type{
"true": {Kind: "bool"},
"false": {Kind: "bool"},
"len": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "int"}},
"all": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"none": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"any": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"one": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"filter": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},
"map": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},
"count": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "int"}},
"trim": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "cutstr", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"trimPrefix": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "cutstr", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"trimSuffix": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "cutstr", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"upper": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"lower": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"repeat": {Kind: "func", Arguments: []*Type{{Name: "n", Kind: "int"}}, Return: &Type{Name: "string", Kind: "string"}},
"join": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Name: "glue", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"indexOf": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "substr", Kind: "string"}}, Return: &Type{Name: "index", Kind: "int"}},
"lastIndexOf": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "substr", Kind: "string"}}, Return: &Type{Name: "index", Kind: "int"}},
"hasPrefix": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "prefix", Kind: "string"}}, Return: &Type{Kind: "bool"}},
"hasSuffix": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "prefix", Kind: "string"}}, Return: &Type{Kind: "bool"}},
"toJSON": {Kind: "func", Arguments: []*Type{{Kind: "any"}}, Return: &Type{Kind: "string"}},
"fromJSON": {Kind: "func", Arguments: []*Type{{Kind: "string"}}, Return: &Type{Kind: "any"}},
"toBase64": {Kind: "func", Arguments: []*Type{{Kind: "string"}}, Return: &Type{Kind: "string"}},
"fromBase64": {Kind: "func", Arguments: []*Type{{Kind: "string"}}, Return: &Type{Kind: "string"}},
"first": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "any"}},
"last": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "any"}},
"now": {Kind: "func", Return: &Type{Name: "time.Time", Kind: "struct"}},
"duration": {Kind: "func", Arguments: []*Type{{Kind: "string"}}, Return: &Type{Kind: "time.Duration"}},
}
)
func CreateDoc(i any) *Context {
c := &Context{
Variables: make(map[Identifier]*Type),
Types: make(map[TypeName]*Type),
PkgPath: deref.Type(reflect.TypeOf(i)).PkgPath(),
}
for name, t := range conf.CreateTypesTable(i) {
if t.Ambiguous {
continue
}
c.Variables[Identifier(name)] = c.use(t.Type, fromMethod(t.Method))
}
for _, op := range Operators {
c.Variables[Identifier(op)] = &Type{
Kind: "operator",
}
}
for builtin, t := range Builtins {
c.Variables[builtin] = t
}
return c
}
type config struct {
method bool
}
type option func(c *config)
func fromMethod(b bool) option {
return func(c *config) {
c.method = b
}
}
func (c *Context) use(t reflect.Type, ops ...option) *Type {
config := &config{}
for _, op := range ops {
op(config)
}
methods := make([]reflect.Method, 0)
// Methods of struct should be gathered from original struct with pointer,
// as methods maybe declared on pointer receiver. Also this method retrieves
// all embedded structs methods as well, no need to recursion.
for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i)
if isPrivate(m.Name) || isProtobuf(m.Name) {
continue
}
methods = append(methods, m)
}
t = deref.Type(t)
// Only named types will have methods defined on them.
// It maybe not even struct, but we gonna call then
// structs in appendix anyway.
if len(methods) > 0 {
goto appendix
}
// This switch only for "simple" types.
switch t.Kind() {
case reflect.Bool:
return &Type{Kind: "bool"}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fallthrough
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return &Type{Kind: "int"}
case reflect.Float32, reflect.Float64:
return &Type{Kind: "float"}
case reflect.String:
return &Type{Kind: "string"}
case reflect.Interface:
return &Type{Kind: "any"}
case reflect.Array, reflect.Slice:
return &Type{
Kind: "array",
Type: c.use(t.Elem()),
}
case reflect.Map:
return &Type{
Kind: "map",
Key: c.use(t.Key()),
Type: c.use(t.Elem()),
}
case reflect.Struct:
goto appendix
case reflect.Func:
arguments := make([]*Type, 0)
start := 0
if config.method {
start = 1
}
for i := start; i < t.NumIn(); i++ {
arguments = append(arguments, c.use(t.In(i)))
}
f := &Type{
Kind: "func",
Arguments: arguments,
}
if t.NumOut() > 0 {
f.Return = c.use(t.Out(0))
}
return f
}
appendix:
name := TypeName(t.String())
if c.PkgPath == t.PkgPath() {
name = TypeName(t.Name())
}
anonymous := t.Name() == ""
a, ok := c.Types[name]
if !ok {
a = &Type{
Kind: "struct",
Fields: make(map[Identifier]*Type),
}
// baseNode a should be saved before starting recursion, or it will never end.
if !anonymous {
c.Types[name] = a
}
for name, field := range conf.FieldsFromStruct(t) {
if isPrivate(name) || isProtobuf(name) || field.Ambiguous {
continue
}
a.Fields[Identifier(name)] = c.use(field.Type)
}
for _, m := range methods {
if isPrivate(m.Name) || isProtobuf(m.Name) {
continue
}
a.Fields[Identifier(m.Name)] = c.use(m.Type, fromMethod(true))
}
}
if anonymous {
return a
}
return &Type{
Kind: "struct",
Name: name,
}
}
var isCapital = regexp.MustCompile("^[A-Z]")
func isPrivate(s string) bool {
return !isCapital.Match([]byte(s))
}
func isProtobuf(s string) bool {
return strings.HasPrefix(s, "XXX_")
}