-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfloat_spec.go
41 lines (38 loc) · 1.46 KB
/
float_spec.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
package vjson
// FloatRangeSpec is a type for parsing a float field range
type FloatRangeSpec struct {
Start float64 `mapstructure:"start" json:"start"`
End float64 `mapstructure:"end" json:"end"`
}
// FloatFieldSpec is a type used for parsing an FloatField
type FloatFieldSpec struct {
Name string `mapstructure:"name" json:"name"`
Type fieldType `json:"type"`
Required bool `mapstructure:"required" json:"required,omitempty"`
Min float64 `mapstructure:"min" json:"min,omitempty"`
Max float64 `mapstructure:"max" json:"max,omitempty"`
Positive bool `mapstructure:"positive" json:"positive,omitempty"`
Ranges []FloatRangeSpec `mapstructure:"ranges" json:"ranges,omitempty"`
}
// NewFloat receives an FloatFieldSpec and returns and FloatField
func NewFloat(spec FloatFieldSpec, minValidation, maxValidation, signValidation, rangeValidation bool) *FloatField {
ranges := make([]floatRange, 0, len(spec.Ranges))
for _, rangeSpec := range spec.Ranges {
ranges = append(ranges, floatRange{
start: rangeSpec.Start,
end: rangeSpec.End,
})
}
return &FloatField{
name: spec.Name,
required: spec.Required,
min: spec.Min,
minValidation: minValidation,
max: spec.Max,
maxValidation: maxValidation,
signValidation: signValidation,
positive: spec.Positive,
rangeValidation: rangeValidation,
ranges: ranges,
}
}