-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_method.go
More file actions
47 lines (37 loc) · 1.19 KB
/
Copy pathobject_method.go
File metadata and controls
47 lines (37 loc) · 1.19 KB
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
package gal
import (
"fmt"
)
// ObjectMethod is a Tree entry that holds a reference of a user-defined object by name and the method to call on it.
// It is used to call a method on a user-defined object.
// It is a "cousin" of Function, but for a method of a user-defined object.
type ObjectMethod struct {
ObjectName string
MethodName string
Args []Tree
}
func NewObjectMethod(objectName, propertyName string, args ...Tree) ObjectMethod {
return ObjectMethod{
ObjectName: objectName,
MethodName: propertyName,
Args: args,
}
}
//nolint:errcheck // life's too short to check for type assertion success here
func (om ObjectMethod) Calculate(val entry, op Operator, cfg *treeConfig) entry {
// attempt to get body of a user-provided object's method.
bodyFn := cfg.ObjectMethod(om)
fn := NewFunction(om.MethodName, bodyFn, om.Args...)
rhsVal := fn.Eval(WithFunctions(cfg.functions), WithVariables(cfg.variables), WithObjects(cfg.objects))
if u, ok := rhsVal.(Undefined); ok {
return u
}
if val == nil {
return rhsVal
}
val = calculate(val.(Value), op, rhsVal)
return val
}
func (om ObjectMethod) String() string {
return fmt.Sprintf("%s.%s", om.ObjectName, om.MethodName)
}