-
Notifications
You must be signed in to change notification settings - Fork 14
/
reg_common_fn.go
61 lines (50 loc) · 1.08 KB
/
reg_common_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
package goql
import (
"github.com/fzerorubigd/goql/astdata"
)
type embeder interface {
Embeds() astdata.Embeds
}
type embedCountFn int
func (embedCountFn) 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.(embeder)
if !ok {
return Number{Null: true}, nil
}
return Number{Number: float64(len(st.Embeds()))}, nil
}
type embedDefFn int
func (embedDefFn) Execute(args ...Getter) (Getter, error) {
if err := required(2, 2, args...); err != nil {
return nil, err
}
def := toDefinition(args[0].Get())
if def == nil {
return Definition{}, nil
}
st, ok := def.(embeder)
if !ok {
return Definition{}, nil
}
em := st.Embeds()
t := toNumber(args[1].Get())
nm := int(t)
if len(em) < nm || nm < 1 {
return Definition{}, nil
}
return Definition{Definition: em[nm-1].Definition()}, nil
}
func registerCommonFunc() {
RegisterFunction("embed_count", embedCountFn(0))
RegisterFunction("embed_def", embedDefFn(0))
}
func init() {
registerCommonFunc()
}