/
field_input_optional_boolean.go
87 lines (74 loc) · 2.24 KB
/
field_input_optional_boolean.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
package forms
// OptionalBooleanInputField represents a checkbox input field.
type OptionalBooleanInputField struct {
// Name is the name of the field.
Name string
// DisplayName is the display name of the field.
DisplayName string
// Required is true if the field is required.
Required bool
// Value is the string value of the field.
Value string
// Help is the help text of the field.
Help string
// Errors are the errors of the field.
Errors []string
// Codec is the codec of the field.
Codec Codec
}
// Ensure OptionalBooleanInputField implements InputField
var _ InputField = (*OptionalBooleanInputField)(nil)
// GetName implements FieldDefinition.GetName
func (f *OptionalBooleanInputField) GetName() string {
return f.Name
}
// GetStringValue implements FieldDefinition.GetStringValue
func (f *OptionalBooleanInputField) GetStringValue() string {
return f.Value
}
// SetStringValue implements FieldDefinition.SetStringValue
func (f *OptionalBooleanInputField) SetStringValue(value string) {
f.Value = value
}
// GetValue implements InputField.GetValue
func (f *OptionalBooleanInputField) GetValue() (interface{}, error) {
return f.getCodecOrDefault().Decode(f.Value)
}
// SetValue implements InputField.SetValue
func (f *OptionalBooleanInputField) SetValue(value interface{}) error {
codec := f.getCodecOrDefault()
val, err := codec.Encode(value)
if err != nil {
return err
}
f.Value = val
return nil
}
// SetErrors implements FieldDefinition.SetErrors
func (f *OptionalBooleanInputField) SetErrors(errors []string) {
f.Errors = errors
}
// HasErrors implements FieldDefinition.HasErrors
func (f *OptionalBooleanInputField) HasErrors() bool {
return len(f.Errors) > 0
}
// GetErrors implements FieldDefinition.GetErrors
func (f *OptionalBooleanInputField) GetErrors() []string {
copied := make([]string, len(f.Errors))
copy(copied, f.Errors)
return copied
}
func (f *OptionalBooleanInputField) IsSelected(value string) bool {
return f.Value == value
}
// GetKind implements FieldDefinition.GetKind
func (f *OptionalBooleanInputField) GetKind() FieldKind {
return FieldKindOptionalBooleanInput
}
func (f *OptionalBooleanInputField) getCodecOrDefault() Codec {
codec := f.Codec
if codec == nil {
codec = &BoolCodec{}
}
return codec
}