blob: f0cc69afe059d7a7198bedf3ffee842c5c74570b (
plain)
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
|
package types
import (
"database/sql/driver"
"encoding"
"encoding/json"
"github.com/icinga/icingadb/internal"
"github.com/pkg/errors"
)
// StateType specifies a state's hardness.
type StateType uint8
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (st *StateType) UnmarshalText(text []byte) error {
return st.UnmarshalJSON(text)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (st *StateType) UnmarshalJSON(data []byte) error {
var i uint8
if err := internal.UnmarshalJSON(data, &i); err != nil {
return err
}
s := StateType(i)
if _, ok := stateTypes[s]; !ok {
return badStateType(data)
}
*st = s
return nil
}
// Value implements the driver.Valuer interface.
func (st StateType) Value() (driver.Value, error) {
if v, ok := stateTypes[st]; ok {
return v, nil
} else {
return nil, badStateType(st)
}
}
// badStateType returns and error about a syntactically, but not semantically valid StateType.
func badStateType(t interface{}) error {
return errors.Errorf("bad state type: %#v", t)
}
const (
StateSoft = StateType(0)
StateHard = StateType(1)
)
// stateTypes maps all valid StateType values to their SQL representation.
var stateTypes = map[StateType]string{
StateSoft: "soft",
StateHard: "hard",
}
// Assert interface compliance.
var (
_ encoding.TextUnmarshaler = (*StateType)(nil)
_ json.Unmarshaler = (*StateType)(nil)
_ driver.Valuer = StateType(0)
)
|