-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
85 lines (74 loc) · 1.99 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package god
type typeAssertFunc func(interface{}) (interface{}, bool)
// String for string type
func String(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "string", func(val interface{}) (interface{}, bool) {
if out, ok := val.(string); ok {
return out, ok
}
if out, ok := val.(*string); ok {
return out, ok
}
return nil, false
})
}
}
// Float32 for float32 type
func Float32(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "float", func(val interface{}) (interface{}, bool) {
if out, ok := ConvertToNumeric(value); ok {
return out.ToFloat32(), ok
}
return nil, false
})
}
}
// Float64 for float64 type
func Float64(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "float64", func(val interface{}) (interface{}, bool) {
if out, ok := ConvertToNumeric(value); ok {
return out.ToFloat64(), ok
}
return nil, false
})
}
}
// Int for int32 type
func Int(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "int", func(val interface{}) (interface{}, bool) {
if out, ok := ConvertToNumeric(value); ok {
return out.ToInt32(), ok
}
return nil, false
})
}
}
// Int64 for int64 type
func Int64(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "int64", func(val interface{}) (interface{}, bool) {
if out, ok := ConvertToNumeric(value); ok {
return out.ToInt64(), ok
}
return nil, false
})
}
}
// Bool for bool type
func Bool(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "bool", func(val interface{}) (interface{}, bool) {
if out, ok := val.(bool); ok {
return out, ok
}
if out, ok := val.(*bool); ok {
return out, ok
}
return nil, false
})
}
}