-
Notifications
You must be signed in to change notification settings - Fork 14
/
reg_interface_fn.go
105 lines (83 loc) · 2.01 KB
/
reg_interface_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
package goql
import (
"github.com/fzerorubigd/goql/astdata"
)
type isInterfaceFn int
func (isInterfaceFn) 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.InterfaceType)
return Bool{Bool: ok}, nil
}
type interfaceFieldCountFn int
func (ifc interfaceFieldCountFn) 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.InterfaceType)
if !ok {
return Number{Null: true}, nil
}
return Number{Number: float64(len(st.Functions()))}, nil
}
type interfaceFuncDef int
func (ifc interfaceFuncDef) 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.InterfaceType)
if !ok {
return Definition{}, nil
}
switch t := args[1].Get().(type) {
case float64:
i := int(t)
fn := st.Functions()
if i > len(fn) || i < 1 {
return Definition{}, nil
}
return Definition{Definition: fn[i-1].Definition()}, nil
case string:
fn := st.Functions()
for i := range fn {
if fn[i].Name() == t {
return Definition{Definition: fn[i].Definition()}, nil
}
}
}
return Definition{}, nil
}
type interfaceFuncName int
func (ifc interfaceFuncName) 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.InterfaceType)
if !ok {
return String{Null: true}, nil
}
i := int(toNumber(args[1].Get()))
fn := st.Functions()
if i > len(fn) || i < 1 {
return String{Null: true}, nil
}
return String{String: fn[i-1].Name()}, nil
}
func registerInterfaceFunc() {
RegisterFunction("is_interface", isInterfaceFn(0))
RegisterFunction("func_count", interfaceFieldCountFn(0))
}
func init() {
registerInterfaceFunc()
}