-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunctions_defs_types.go
More file actions
149 lines (144 loc) · 4.17 KB
/
functions_defs_types.go
File metadata and controls
149 lines (144 loc) · 4.17 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
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
package diecast
import (
"reflect"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/timeutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
func loadStandardFunctionsTypes(_ FuncMap, _ *Server) funcGroup {
var group = funcGroup{
Name: `Type Detection and Manipulation`,
Description: `Used to detect and convert discrete values into different data types.`,
Functions: []funcDef{
{
Name: `isBool`,
Summary: `Return whether the given *value* is a boolean type.`,
Function: stringutil.IsBoolean,
}, {
Name: `isInt`,
Summary: `Return whether the given *value* is an integer type.`,
Function: stringutil.IsInteger,
}, {
Name: `isFloat`,
Summary: `Return whether the given *value* is a floating-point type.`,
Function: stringutil.IsFloat,
}, {
Name: `isZero`,
Summary: `Return whether the given *value* is an zero-valued variable.`,
Function: typeutil.IsZero,
}, {
Name: `isEmpty`,
Summary: `Return whether the given *value* is empty.`,
Function: typeutil.IsEmpty,
}, {
Name: `isNotZero`,
Summary: `Return whether the given *value* is NOT a zero-valued variable.`,
Function: func(value any) bool {
return !typeutil.IsZero(value)
},
}, {
Name: `isNotEmpty`,
Summary: `Return whether the given *value* is NOT empty.`,
Function: func(value any) bool {
return !typeutil.IsEmpty(value)
},
}, {
Name: `isArray`,
Summary: `Return whether the given *value* is an iterable array or slice.`,
Function: typeutil.IsArray,
}, {
Name: `isMap`,
Summary: `Return whether the given *value* is a key-value map type.`,
Function: func(value any) bool {
return typeutil.IsKind(value, reflect.Map)
},
}, {
Name: `isTime`,
Summary: `Return whether the given *value* is parsable as a date/time value.`,
Function: func(value any) bool {
return !typeutil.V(value).Time().IsZero()
},
}, {
Name: `isDuration`,
Summary: `Return whether the given *value* is parsable as a duration.`,
Function: func(value any) bool {
return (typeutil.V(value).Duration() != 0)
},
}, {
Name: `autotype`,
Summary: `Attempt to automatically determine the type if *value* and return the converted output.`,
Function: stringutil.Autotype,
}, {
Name: `asStr`,
Aliases: []string{`s`},
Summary: `Return the *value* as a string.`,
Function: stringutil.ToString,
}, {
Name: `asInt`,
Aliases: []string{`i`},
Summary: `Attempt to convert the given *value* to an integer.`,
Function: func(value any) (int64, error) {
if v, err := stringutil.ConvertToInteger(value); err == nil {
return v, nil
} else {
return 0, err
}
},
}, {
Name: `asFloat`,
Aliases: []string{`f`},
Summary: `Attempt to convert the given *value* to a floating-point number.`,
Function: stringutil.ConvertToFloat,
}, {
Name: `asBool`,
Aliases: []string{`b`},
Summary: `Attempt to convert the given *value* to a boolean value.`,
Function: stringutil.ConvertToBool,
}, {
Name: `asTime`,
Aliases: []string{`t`},
Summary: `Attempt to parse the given *value* as a date/time value.`,
Function: stringutil.ConvertToTime,
}, {
Name: `asDuration`,
Aliases: []string{`d`},
Summary: `Attempt to parse the given *value* as a time duration.`,
Function: timeutil.ParseDuration,
},
},
}
group.Functions = append(group.Functions, []funcDef{
{
Name: `s`,
Alias: `asStr`,
Hidden: true,
Function: group.fn(`asStr`),
}, {
Name: `i`,
Alias: `asInt`,
Hidden: true,
Function: group.fn(`asInt`),
}, {
Name: `f`,
Alias: `asFloat`,
Hidden: true,
Function: group.fn(`asFloat`),
}, {
Name: `b`,
Alias: `asBool`,
Hidden: true,
Function: group.fn(`asBool`),
}, {
Name: `t`,
Alias: `asTime`,
Hidden: true,
Function: group.fn(`asTime`),
}, {
Name: `d`,
Alias: `asDuration`,
Hidden: true,
Function: group.fn(`asDuration`),
},
}...)
return group
}