forked from hairyhenderson/gomplate
/
duration_gen.go
60 lines (55 loc) · 1.41 KB
/
duration_gen.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
package strings
import (
"time"
"github.com/flanksource/gomplate/v3/conv"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)
func age(value ref.Val) ref.Val {
switch v := value.Value().(type) {
case int, int32, int64, float32, float64:
return types.Duration{Duration: time.Duration(conv.ToInt64(v))}
case string:
return types.Duration{Duration: Age(v)}
}
return types.NewErr("cannot convert %v into duration", value.Type())
}
var DurationAgeGen = cel.Function("Age",
cel.Overload("duration.Age",
[]*cel.Type{cel.AnyType},
cel.DurationType,
cel.UnaryBinding(age),
),
)
var DurationAgeGen2 = cel.Function("age",
cel.Overload("duration.age",
[]*cel.Type{cel.AnyType},
cel.DurationType,
cel.UnaryBinding(age),
),
)
var Durations = []cel.EnvOption{
cel.Function("duration",
cel.Overload("double.duration",
[]*cel.Type{cel.DoubleType},
cel.DurationType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
return types.Duration{Duration: time.Duration(conv.ToInt64(value.Value()))}
}),
),
),
cel.Function("Duration",
cel.Overload("duration.Duration",
[]*cel.Type{cel.StringType},
cel.DurationType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
a, err := ParseDuration(value.Value().(string))
if err != nil || a == nil {
return types.Duration{}
}
return types.Duration{Duration: *a}
}),
),
),
}