-
Notifications
You must be signed in to change notification settings - Fork 0
/
integerfield.go
84 lines (76 loc) · 1.71 KB
/
integerfield.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
package xdommask
import (
"errors"
"fmt"
"strconv"
"github.com/webability-go/wajaf"
"github.com/webability-go/xamboo/cms/context"
)
type IntegerField struct {
*TextField
DefaultValue int
Min int
Max int
StatusTooLow string
StatusTooHigh string
}
func NewIntegerField(name string) *IntegerField {
inf := &IntegerField{
TextField: NewTextField(name),
}
inf.TextType = TEXTTYPE_INTEGER
return inf
}
func (f *IntegerField) Compile() wajaf.NodeDef {
t := f.TextField.Compile()
t.SetAttribute("format", "^-{0,1}[0-9]{1,}$")
if f.Min != 0 {
t.SetAttribute("min", strconv.Itoa(f.Min))
t.AddMessage("statustoolow", f.StatusTooLow)
}
if f.Max != 0 {
t.SetAttribute("max", strconv.Itoa(f.Max))
t.AddMessage("statustoohigh", f.StatusTooHigh)
}
return t
}
func (f *IntegerField) GetValue(ctx *context.Context, mode Mode) (interface{}, bool, error) {
val, ignored, err := f.DataField.GetValue(ctx, mode)
if err != nil {
return val, ignored, err
}
fmt.Printf("GetValue: %v %T\n", val, val)
newval, err := f.ConvertValue(val)
fmt.Printf("New value: %v %T\n", newval, newval)
if newval == nil && mode == INSERT {
newval = f.DefaultValue
}
return newval, ignored, err
}
func (f *IntegerField) ConvertValue(value interface{}) (interface{}, error) {
ival, ok := value.(int)
if ok {
return ival, nil
}
i8val, ok := value.(int8)
if ok {
return int(i8val), nil
}
i16val, ok := value.(int16)
if ok {
return int(i16val), nil
}
i32val, ok := value.(int32)
if ok {
return int(i32val), nil
}
i64val, ok := value.(int64)
if ok {
return int(i64val), nil
}
sval, ok := value.(string)
if ok {
return strconv.Atoi(sval)
}
return value, errors.New("Cannot convert value")
}