-
Notifications
You must be signed in to change notification settings - Fork 0
/
manip.go
66 lines (52 loc) · 1.04 KB
/
manip.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
package pl
import (
"fmt"
)
func NewPl(fs ...*Fn) *Pl {
return &Pl{Funcs: fs}
}
func NewFn(name string, args ...interface{}) (*Fn, error) {
if len(args) == 0 {
return &Fn{Name: name}, nil
}
args_, err := NewArgs(args...)
if err != nil {
return nil, err
}
return &Fn{Name: name, Args: args_}, nil
}
func NewArgs(args ...interface{}) ([]*Arg, error) {
rst := make([]*Arg, len(args))
for i, arg := range args {
rst[i] = &Arg{}
switch v := arg.(type) {
case string:
rst[i].String = &v
case float64:
rst[i].Float = &v
case int:
rst[i].Int = &v
case Ref:
rst[i].Ref = v
case *Pl:
rst[i].Nested = v
default:
return nil, fmt.Errorf("invalid type of argument at %d", i)
}
}
return rst, nil
}
func NewRef(entries ...interface{}) (Ref, error) {
rst := make(Ref, len(entries))
for i, entry := range entries {
switch v := entry.(type) {
case string:
rst[i].Name = &v
case int:
rst[i].Index = &v
default:
return nil, fmt.Errorf("invalid type of argument at %d", i)
}
}
return rst, nil
}