-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaluetype.go
124 lines (101 loc) · 2.27 KB
/
valuetype.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
package ruleenginecore
import (
"encoding/json"
"fmt"
"strings"
)
// 'ValueType' defines supported value types for rule engine
type ValueType uint8
const (
unknownValueType ValueType = iota
// 'Boolean' is boolean true or false
Boolean
// 'String' is UTF-8 based string
String
// 'Integer' is 64 bit signed integer
Integer
// 'Float' is 64 bit signed float
Float
)
var (
valueType_Name = map[ValueType]string{
1: "Boolean",
2: "String",
3: "Integer",
4: "Float",
}
valueType_Value = map[string]ValueType{
"bool": 1,
"Bool": 1,
"boolean": 1,
"Boolean": 1,
"string": 2,
"String": 2,
"int": 3,
"Int": 3,
"integer": 3,
"Integer": 3,
"float": 4,
"Float": 4,
}
)
// 'isValid' check for valid ValueType
func (valueType ValueType) isValid() bool {
_, ok := valueType_Name[valueType]
return ok
}
// 'String' gives string representation of ValueType
func (valueType ValueType) String() string {
val, ok := valueType_Name[valueType]
if !ok {
return fmt.Sprintf("ValueType(%v)", uint8(valueType))
}
return val
}
func parseValueType(s string) (ValueType, error) {
s = strings.TrimSpace(strings.ToLower(s))
value, ok := valueType_Value[s]
if !ok {
return unknownValueType, fmt.Errorf("invalid ValueType(%v)", s)
}
return ValueType(value), nil
}
func (valueType ValueType) MarshalJSON() ([]byte, error) {
if valueType.isValid() {
return json.Marshal(valueType.String())
}
return nil, fmt.Errorf("invalid ValueType(%v)", valueType)
}
func (valueType *ValueType) UnmarshalJSON(data []byte) error {
var val string
if err := json.Unmarshal(data, &val); err != nil {
return err
}
result, err := parseValueType(val)
if err != nil {
return err
}
*valueType = result
return nil
}
func (valueType ValueType) isBoolean() bool {
return valueType == Boolean
}
func (valueType ValueType) isInteger() bool {
return valueType == Integer
}
func (valueType ValueType) isFloat() bool {
return valueType == Float
}
func (valueType ValueType) isString() bool {
return valueType == String
}
// comma separated value type list
var valueTypeList string
func init() {
validTypes := []string{}
for _, valueType := range valueType_Name {
validTypes = append(validTypes, valueType)
}
valueTypeList = strings.Join(validTypes[:], ", ")
}