-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.go
137 lines (125 loc) · 2.8 KB
/
functions.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
package opencypher
import (
"fmt"
"time"
"github.com/cloudprivacylabs/lpg/v2"
)
// Function describes a function
type Function struct {
Name string
MinArgs int
MaxArgs int
Func func(*EvalContext, []Evaluatable) (Value, error)
ValueFunc func(*EvalContext, []Value) (Value, error)
}
type ErrInvalidFunctionCall struct {
Msg string
}
func (e ErrInvalidFunctionCall) Error() string {
return "Invalid function call: " + e.Msg
}
func RegisterGlobalFunc(fn ...Function) {
for _, f := range fn {
globalFuncs[f.Name] = f
}
}
var globalFuncs = map[string]Function{
"range": Function{
Name: "range",
MinArgs: 2,
MaxArgs: 3,
ValueFunc: rangeFunc,
},
"labels": Function{
Name: "labels",
MinArgs: 1,
MaxArgs: 1,
ValueFunc: labelsFunc,
},
"timestamp": Function{
Name: "timestamp",
MinArgs: 0,
MaxArgs: 0,
ValueFunc: timestampFunc,
},
"type": Function{
Name: "type",
MinArgs: 1,
MaxArgs: 1,
ValueFunc: typeFunc,
},
"size": Function{
Name: "size",
MinArgs: 1,
MaxArgs: 1,
ValueFunc: sizeFunc,
},
}
func rangeFunc(ctx *EvalContext, args []Value) (Value, error) {
start, err := ValueAsInt(args[0])
if err != nil {
return nil, err
}
end, err := ValueAsInt(args[1])
if err != nil {
return nil, err
}
skip := 1
if len(args) == 3 {
skip, err = ValueAsInt(args[2])
if err != nil {
return nil, err
}
}
if (end <= start && skip > 0) || (end >= start && skip < 0) || skip == 0 {
return RValue{Value: []Value{}}, nil
}
arr := make([]Value, 0)
if end > start {
for at := start; at < end; at += skip {
arr = append(arr, RValue{Value: at})
}
} else {
for at := start; at > end; at += skip {
arr = append(arr, RValue{Value: at})
}
}
return RValue{Value: arr}, nil
}
func labelsFunc(ctx *EvalContext, args []Value) (Value, error) {
if args[0].Get() == nil {
return RValue{}, nil
}
node, ok := args[0].Get().(*lpg.Node)
if !ok {
return nil, fmt.Errorf("Not a node")
}
return RValue{Value: node.GetLabels().Slice()}, nil
}
func timestampFunc(ctx *EvalContext, args []Value) (Value, error) {
return RValue{Value: int(time.Now().Unix())}, nil
}
func typeFunc(ctx *EvalContext, args []Value) (Value, error) {
edge, ok := args[0].Get().(*lpg.Edge)
if ok {
return RValue{Value: edge.GetLabel()}, nil
}
path, ok := args[0].Get().(*lpg.Path)
if !ok || path.NumEdges() != 1 {
return nil, fmt.Errorf("Cannot determine type of %T", args[0].Get())
}
return RValue{Value: path.GetEdge(0).GetLabel()}, nil
}
func sizeFunc(ctx *EvalContext, args []Value) (Value, error) {
val := args[0].Get()
if val == nil {
return RValue{}, nil
}
if arr, ok := val.([]Value); ok {
return RValue{Value: len(arr)}, nil
}
if str, ok := val.(string); ok {
return RValue{Value: len(str)}, nil
}
return RValue{}, nil
}