forked from CloudyKit/jet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
176 lines (157 loc) · 5.71 KB
/
func.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2016 José Santos <henrique_1609@me.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jet
import (
"fmt"
"reflect"
"time"
)
// Arguments holds the arguments passed to jet.Func.
type Arguments struct {
runtime *Runtime
args CallArgs
pipedVal *reflect.Value
}
// IsSet checks whether an argument is set or not. It behaves like the build-in isset function.
func (a *Arguments) IsSet(argumentIndex int) bool {
if argumentIndex < len(a.args.Exprs) {
if a.args.Exprs[argumentIndex].Type() == NodeUnderscore {
return a.pipedVal != nil
}
return a.runtime.isSet(a.args.Exprs[argumentIndex])
}
if len(a.args.Exprs) == 0 && argumentIndex == 0 {
return a.pipedVal != nil
}
return false
}
// Get gets an argument by index.
func (a *Arguments) Get(argumentIndex int) reflect.Value {
if argumentIndex < len(a.args.Exprs) {
if a.args.Exprs[argumentIndex].Type() == NodeUnderscore {
return *a.pipedVal
}
return a.runtime.evalPrimaryExpressionGroup(a.args.Exprs[argumentIndex])
}
if len(a.args.Exprs) == 0 && argumentIndex == 0 {
return *a.pipedVal
}
return reflect.Value{}
}
// Panicf panics with formatted error message.
func (a *Arguments) Panicf(format string, v ...interface{}) {
panic(fmt.Errorf(format, v...))
}
// RequireNumOfArguments panics if the number of arguments is not in the range specified by min and max.
// In case there is no minimum pass -1, in case there is no maximum pass -1 respectively.
func (a *Arguments) RequireNumOfArguments(funcname string, min, max int) {
num := a.NumOfArguments()
if min >= 0 && num < min {
a.Panicf("unexpected number of arguments in a call to %s", funcname)
} else if max >= 0 && num > max {
a.Panicf("unexpected number of arguments in a call to %s", funcname)
}
}
// NumOfArguments returns the number of arguments
func (a *Arguments) NumOfArguments() int {
num := len(a.args.Exprs)
if a.pipedVal != nil && !a.args.HasPipeSlot {
return num + 1
}
return num
}
// Runtime get the Runtime context
func (a *Arguments) Runtime() *Runtime {
return a.runtime
}
// ParseInto parses the arguments into the provided pointers. It returns an error if the number of pointers passed in does not
// equal the number of arguments, if any argument's value is invalid according to Go's reflect package, if an argument can't
// be used as the value the pointer passed in at the corresponding position points to, or if an unhandled pointer type is encountered.
// Allowed pointer types are pointers to interface{}, int, int64, float64, bool, string, time.Time, reflect.Value, []interface{},
// map[string]interface{}. If a pointer to a reflect.Value is passed in, the argument be assigned as-is to the value pointed to. For
// pointers to int or float types, type conversion is performed automatically if necessary.
func (a *Arguments) ParseInto(ptrs ...interface{}) error {
if len(ptrs) < a.NumOfArguments() {
return fmt.Errorf("have %d arguments, but only %d pointers to parse into", a.NumOfArguments(), len(ptrs))
}
for i := 0; i < a.NumOfArguments(); i++ {
arg, ptr := indirectEface(a.Get(i)), ptrs[i]
ok := false
if !arg.IsValid() {
return fmt.Errorf("argument at position %d is not a valid value", i)
}
switch p := ptr.(type) {
case *reflect.Value:
*p, ok = arg, true
case *int:
switch arg.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
*p, ok = int(arg.Int()), true
case reflect.Float32, reflect.Float64:
*p, ok = int(arg.Float()), true
default:
return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
}
case *int64:
switch arg.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
*p, ok = arg.Int(), true
case reflect.Float32, reflect.Float64:
*p, ok = int64(arg.Float()), true
default:
return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
}
case *float64:
switch arg.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
*p, ok = float64(arg.Int()), true
case reflect.Float32, reflect.Float64:
*p, ok = arg.Float(), true
default:
return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
}
}
if ok {
continue
}
if !arg.CanInterface() {
return fmt.Errorf("argument at position %d can't be accessed via Interface()", i)
}
val := arg.Interface()
switch p := ptr.(type) {
case *interface{}:
*p, ok = val, true
case *bool:
*p, ok = val.(bool)
case *string:
*p, ok = val.(string)
case *time.Time:
*p, ok = val.(time.Time)
case *[]interface{}:
*p, ok = val.([]interface{})
case *map[string]interface{}:
*p, ok = val.(map[string]interface{})
default:
return fmt.Errorf("trying to parse %v into %v: unhandled value type %T", arg, p, val)
}
if !ok {
return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
}
}
return nil
}
// Func function implementing this type is called directly, which is faster than calling through reflect.
// If a function is being called many times in the execution of a template, you may consider implementing
// a wrapper for that function implementing a Func.
type Func func(Arguments) reflect.Value