-
Notifications
You must be signed in to change notification settings - Fork 9
/
types.go
236 lines (201 loc) · 6.55 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package vitotrol
import (
"errors"
"fmt"
"strconv"
"strings"
)
// Singletons matching Vitodata™ types.
var (
TypeDouble = (*VitodataDouble)(nil)
TypeInteger = (*VitodataInteger)(nil)
TypeDate = (*VitodataDate)(nil)
TypeString = (*VitodataString)(nil)
TypeOnOffEnum = NewEnum([]string{ // 0 -> 1
"off",
"on",
})
TypeEnabledEnum = NewEnum([]string{ // 0 -> 1
"disabled",
"enabled",
})
TypeNames = map[string]VitodataType{
TypeDouble.Type(): TypeDouble,
TypeInteger.Type(): TypeInteger,
TypeDate.Type(): TypeDate,
TypeString.Type(): TypeString,
}
)
// ErrEnumInvalidValue is returned when trying to convert to an enum a
// value that cannot match any value of this enum.
var ErrEnumInvalidValue = errors.New("Invalid Enum value")
// VitodataType is the interface implemented by each Vitodata™ type.
type VitodataType interface {
Type() string
Human2VitodataValue(string) (string, error)
Vitodata2HumanValue(string) (string, error)
Vitodata2NativeValue(string) (interface{}, error)
}
// A VitodataDouble represent the Vitodata™ Double type.
type VitodataDouble struct{}
// Type returns the "human" name of the type.
func (v *VitodataDouble) Type() string {
return "Double"
}
// Human2VitodataValue checks that the value is a float number and
// returns it after reformatting.
func (v *VitodataDouble) Human2VitodataValue(value string) (string, error) {
num, err := strconv.ParseFloat(value, 64)
if err != nil {
return "", err
}
return strings.Replace(strconv.FormatFloat(num, 'f', -1, 64), ".", ",", 1), nil
}
// Vitodata2HumanValue checks that the value is a float number and
// returns it after reformatting.
func (v *VitodataDouble) Vitodata2HumanValue(value string) (string, error) {
num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64)
if err != nil {
return "", err
}
return strconv.FormatFloat(num, 'f', -1, 64), nil
}
// Vitodata2NativeValue extract the number from the passed string and
// returns it as a float64.
func (v *VitodataDouble) Vitodata2NativeValue(value string) (interface{}, error) {
num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64)
if err != nil {
return nil, err
}
return num, nil
}
// A VitodataInteger represent the Vitodata™ Integer type.
type VitodataInteger struct{}
// Type returns the "human" name of the type.
func (v *VitodataInteger) Type() string {
return "Integer"
}
// Human2VitodataValue checks that the value is an integer and returns
// it after reformatting.
func (v *VitodataInteger) Human2VitodataValue(value string) (string, error) {
num, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return "", err
}
return strconv.FormatInt(num, 10), nil
}
// Vitodata2HumanValue checks that the value is an integer and returns
// it after reformatting.
func (v *VitodataInteger) Vitodata2HumanValue(value string) (string, error) {
return v.Human2VitodataValue(value)
}
// Vitodata2NativeValue extract the number from the passed string and
// returns it as an int64.
func (v *VitodataInteger) Vitodata2NativeValue(value string) (interface{}, error) {
num, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, err
}
return num, nil
}
// A VitodataDate represent the Vitodata™ Date type.
type VitodataDate struct{}
// Type returns the "human" name of the type.
func (v *VitodataDate) Type() string {
return "Date"
}
// Human2VitodataValue checks that the value is Vitodata™ formatted date and
// returns it after reformatting.
func (v *VitodataDate) Human2VitodataValue(value string) (string, error) {
tm, err := ParseVitotrolTime(value)
if err != nil {
return "", err
}
return tm.String(), nil
}
// Vitodata2HumanValue checks that the value is Vitodata™ formatted date and
// returns it after reformatting.
func (v *VitodataDate) Vitodata2HumanValue(value string) (string, error) {
return v.Human2VitodataValue(value)
}
// Vitodata2NativeValue extract the Vitodata™ date from
// the passed string and returns it as a vitotrol.Time.
func (v *VitodataDate) Vitodata2NativeValue(value string) (interface{}, error) {
tm, err := ParseVitotrolTime(value)
if err != nil {
return nil, err
}
return tm, nil
}
// A VitodataString represent the Vitodata™ String type.
type VitodataString struct{}
// Type returns the "human" name of the type.
func (v *VitodataString) Type() string {
return "String"
}
// Human2VitodataValue is a no-op here, returning its argument.
func (v *VitodataString) Human2VitodataValue(value string) (string, error) {
return value, nil
}
// Vitodata2HumanValue is a no-op here, returning its argument.
func (v *VitodataString) Vitodata2HumanValue(value string) (string, error) {
return value, nil
}
// Vitodata2NativeValue is a no-op here, returning its argument.
func (v *VitodataString) Vitodata2NativeValue(value string) (interface{}, error) {
return value, nil
}
// VitodataEnum represents any Vitodata™ Enum type. See NewEnum to
// specialize it.
type VitodataEnum struct {
values map[string]uint16
revValues []string
}
// NewEnum specializes an enum to a set of values and returns it.
func NewEnum(values []string) *VitodataEnum {
pEnum := &VitodataEnum{
values: make(map[string]uint16, len(values)),
revValues: make([]string, len(values)),
}
for idx, value := range values {
pEnum.values[value] = uint16(idx)
pEnum.revValues[idx] = value
}
return pEnum
}
// Type returns the "human" name of the type.
func (v *VitodataEnum) Type() string {
return fmt.Sprintf("Enum%d", len(v.revValues))
}
// Human2VitodataValue checks that the value is a Vitodata™ enum value
// and returns its numeric counterpart.
func (v *VitodataEnum) Human2VitodataValue(value string) (string, error) {
// String version ?
if num, ok := v.values[value]; ok {
return strconv.FormatUint(uint64(num), 10), nil
}
// Numeric one ?
num, err := v.Vitodata2NativeValue(value)
if err != nil {
return "", err
}
return strconv.FormatUint(num.(uint64), 10), nil
}
// Vitodata2HumanValue check that the (numeric) value is a Vitodata™
// enum value and returns its string counterpart.
func (v *VitodataEnum) Vitodata2HumanValue(value string) (string, error) {
num, err := v.Vitodata2NativeValue(value)
if err != nil {
return "", err
}
return v.revValues[num.(uint64)], nil
}
// Vitodata2NativeValue extract the numeric Vitodata™ enum value from
// the passed string and returns it as a uint64.
func (v *VitodataEnum) Vitodata2NativeValue(value string) (interface{}, error) {
num, err := strconv.ParseUint(value, 10, 64)
if err != nil || num >= uint64(len(v.revValues)) {
return nil, ErrEnumInvalidValue
}
return num, nil
}