-
Notifications
You must be signed in to change notification settings - Fork 14
/
reg_struct_fn.go
165 lines (145 loc) · 3.32 KB
/
reg_struct_fn.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
package goql
import (
"reflect"
"github.com/fzerorubigd/goql/astdata"
)
type isStructFn int
func (isStructFn) Execute(args ...Getter) (Getter, error) {
def, err := getSingleDef(args...)
if err != nil {
return nil, err
}
if def == nil {
return Bool{Null: true}, nil
}
_, ok := def.(*astdata.StructType)
return Bool{Bool: ok}, nil
}
type structFieldCountFn int
func (sfc structFieldCountFn) Execute(args ...Getter) (Getter, error) {
def, err := getSingleDef(args...)
if err != nil {
return nil, err
}
if def == nil {
return Number{Null: true}, nil
}
st, ok := def.(*astdata.StructType)
if !ok {
return Number{Null: true}, nil
}
return Number{Number: float64(len(st.Fields()))}, nil
}
type structFieldDefFn int
func (structFieldDefFn) Execute(args ...Getter) (Getter, error) {
if err := required(2, 2, args...); err != nil {
return nil, err
}
def := toDefinition(args[0].Get())
st, ok := def.(*astdata.StructType)
if !ok {
return Definition{}, nil
}
var f astdata.Definition
fl := st.Fields()
bigSwitch:
switch t := args[1].Get().(type) {
case float64:
nm := int(t)
if len(fl) < nm || nm < 1 {
return Definition{}, nil
}
f = fl[nm-1].Definition()
case string:
for i := range fl {
if fl[i].Name() == t {
f = fl[i].Definition()
break bigSwitch
}
}
return Definition{}, nil
default:
return Definition{}, nil
}
return Definition{Definition: f}, nil
}
type structFieldNameFn int
func (structFieldNameFn) Execute(args ...Getter) (Getter, error) {
if err := required(2, 2, args...); err != nil {
return nil, err
}
def := toDefinition(args[0].Get())
nm := int(toNumber(args[1].Get()))
st, ok := def.(*astdata.StructType)
if !ok {
return String{Null: true}, nil
}
fl := st.Fields()
if len(fl) < nm || nm < 1 {
return String{Null: true}, nil
}
return String{String: fl[nm-1].Name()}, nil
}
type structFieldTagFn int
func (sft structFieldTagFn) Execute(args ...Getter) (Getter, error) {
if err := required(2, 3, args...); err != nil {
return nil, err
}
def := toDefinition(args[0].Get())
st, ok := def.(*astdata.StructType)
if !ok {
return String{Null: true}, nil
}
var f reflect.StructTag
if sft == 0 {
fl := st.Fields()
bigSwitch:
switch t := args[1].Get().(type) {
case float64:
nm := int(t)
if len(fl) < nm || nm < 1 {
return String{Null: true}, nil
}
f = fl[nm-1].Tags()
case string:
for i := range fl {
if fl[i].Name() == t {
f = fl[i].Tags()
break bigSwitch
}
}
return String{Null: true}, nil
default:
return String{Null: true}, nil
}
} else {
fl := st.Embeds()
switch t := args[1].Get().(type) {
case float64:
nm := int(t)
if len(fl) < nm || nm < 1 {
return String{Null: true}, nil
}
f = fl[nm-1].Tags()
default:
return String{Null: true}, nil
}
}
if len(args) == 3 {
tag := toString(args[2].Get())
return String{String: f.Get(tag)}, nil
}
return String{String: string(f)}, nil
}
func registerStructFunc() {
// Struct funcs
RegisterFunction("is_struct", isStructFn(0))
RegisterFunction("field_def", structFieldDefFn(0))
RegisterFunction("field_name", structFieldNameFn(0))
RegisterFunction("field_count", structFieldCountFn(0))
RegisterFunction("field_tag", structFieldTagFn(0))
RegisterFunction("embed_tag", structFieldTagFn(1))
}
func init() {
registerStructFunc()
}