-
Notifications
You must be signed in to change notification settings - Fork 4
/
float.go
189 lines (169 loc) · 4.58 KB
/
float.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
package schema
import (
"fmt"
"reflect"
"strconv"
)
// Float holds the schema information for 64-bit floating point numbers. This dataclass only has the ability to
// hold the configuration but cannot serialize, unserialize or validate. For that functionality please use
// FloatType.
type Float interface {
TypedType[float64]
Min() *float64
Max() *float64
Units() *UnitsDefinition
}
// NewFloatSchema creates a new float schema from the specified values.
func NewFloatSchema(min *float64, max *float64, units *UnitsDefinition) *FloatSchema {
return &FloatSchema{
MinValue: min,
MaxValue: max,
UnitsValue: units,
}
}
type FloatSchema struct {
ScalarType
MinValue *float64 `json:"min"`
MaxValue *float64 `json:"max"`
UnitsValue *UnitsDefinition `json:"units"`
}
func (f FloatSchema) ReflectedType() reflect.Type {
return reflect.TypeOf(float64(0))
}
func (f FloatSchema) TypeID() TypeID {
return TypeIDFloat
}
func (f FloatSchema) Min() *float64 {
return f.MinValue
}
func (f FloatSchema) Max() *float64 {
return f.MaxValue
}
func (f FloatSchema) Units() *UnitsDefinition {
return f.UnitsValue
}
func (f FloatSchema) Unserialize(data any) (any, error) {
unserialized, err := floatInputMapper(data, f.UnitsValue)
if err != nil {
return 0, err
}
return unserialized, f.Validate(unserialized)
}
func (f FloatSchema) UnserializeType(data any) (float64, error) {
unserialized, err := f.Unserialize(data)
if err != nil {
return 0, err
}
return unserialized.(float64), nil
}
func (f FloatSchema) ValidateCompatibility(typeOrData any) error {
// Check if it's a schema.Type. If it is, verify it. If not, verify it as data.
schemaType, ok := typeOrData.(Type)
if !ok {
_, err := f.Unserialize(typeOrData)
return err
}
if schemaType.TypeID() != TypeIDFloat {
return &ConstraintError{
Message: fmt.Sprintf("unsupported data type for 'float' type: %T", schemaType),
}
}
// Verify float-specific schema values
floatSchemaType, ok := typeOrData.(*FloatSchema)
if ok {
// We are just verifying compatibility. So anything is accepted except for when they are mutually exclusive
// So that's just when the min of the tested type is greater than the max of the self type,
// or the max of the tested type is less than the min of the self type
// For more control over this, the ValidateCompatibility API would need to change to allow subset,
// superset, and exact verification levels.
if (f.MinValue != nil && floatSchemaType.MaxValue != nil && (*floatSchemaType.MinValue) > (*f.MaxValue)) ||
(f.MaxValue != nil && floatSchemaType.MinValue != nil && (*floatSchemaType.MaxValue) < (*f.MinValue)) {
return &ConstraintError{
Message: "mutually exclusive min/max values between float schemas",
}
}
}
return nil
}
func (f FloatSchema) Validate(d any) error {
_, err := f.Serialize(d)
return err
}
func (f FloatSchema) ValidateType(data float64) error {
return f.Validate(data)
}
func (f FloatSchema) Serialize(d any) (any, error) {
data, err := asFloat(d)
if err != nil {
return data, err
}
if f.MinValue != nil && data < *f.MinValue {
return data, &ConstraintError{
Message: fmt.Sprintf("Must be at least %f", *f.MinValue),
}
}
if f.MaxValue != nil && data > *f.MaxValue {
return data, &ConstraintError{
Message: fmt.Sprintf("Must be at most %f", *f.MaxValue),
}
}
return data, nil
}
func asFloat(d any) (float64, error) {
data, ok := d.(float64)
if !ok {
var i float64
intType := reflect.TypeOf(i)
dValue := reflect.ValueOf(d)
if !dValue.CanConvert(intType) {
return 0, &ConstraintError{
Message: fmt.Sprintf("%T is not a valid data type for a float schema.", d),
}
}
data = dValue.Convert(intType).Float()
}
return data, nil
}
func (f FloatSchema) SerializeType(data float64) (any, error) {
return f.Serialize(data)
}
func floatInputMapper(data any, u *UnitsDefinition) (float64, error) {
switch v := data.(type) {
case string:
if u != nil {
return (*u).ParseFloat(v)
}
return strconv.ParseFloat(v, 64)
case int64:
return float64(v), nil
case uint64:
return float64(v), nil
case int:
return float64(v), nil
case uint:
return float64(v), nil
case int32:
return float64(v), nil
case uint32:
return float64(v), nil
case int16:
return float64(v), nil
case uint16:
return float64(v), nil
case int8:
return float64(v), nil
case uint8:
return float64(v), nil
case float64:
return v, nil
case float32:
return float64(v), nil
case bool:
if v {
return float64(1), nil
}
return float64(0), nil
default:
return float64(0), fmt.Errorf("%T cannot be converted to a float64", data)
}
}