/
types.go
63 lines (58 loc) · 1.32 KB
/
types.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
package validate
import (
"fmt"
"strconv"
"strings"
)
func isNumeric(args ...interface{}) (interface{}, error) {
if m, _ := isMissing(args[0]); m.(bool) {
return (bool)(true), nil
}
switch args[0].(type) {
case float64, float32, int:
return (bool)(true), nil
}
return (bool)(false), nil
}
func isInt(args ...interface{}) (interface{}, error) {
if m, _ := isMissing(args[0]); m.(bool) {
return (bool)(true), nil
}
conv, ok := args[0].(float64)
if ok {
if conv == float64(int64(conv)) {
return (bool)(true), nil
}
}
if _, err := strconv.Atoi(fmt.Sprintf("%s", args[0])); err == nil {
return (bool)(true), nil
}
return (bool)(false), nil
}
func isBool(args ...interface{}) (interface{}, error) {
if m, _ := isMissing(args[0]); m.(bool) {
return (bool)(true), nil
}
val := fmt.Sprintf("%v", args[0])
BooleanValues := []string{"true", "false"}
for _, x := range BooleanValues {
if strings.ToLower(val) == x {
return (bool)(true), nil
}
}
return (bool)(false), nil
}
func isString(args ...interface{}) (interface{}, error) {
if m, _ := isMissing(args[0]); m.(bool) {
return (bool)(true), nil
}
tNum, _ := isNumeric(args[0])
tInt, _ := isInt(args[0])
tBool, _ := isBool(args[0])
if tNum == false &&
tInt == false &&
tBool == false {
return (bool)(true), nil
}
return (bool)(false), nil
}