-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
72 lines (61 loc) · 1.52 KB
/
types.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
package reservedfixedips
import (
"encoding/json"
"fmt"
)
type ReservedFixedIPType string
const (
External ReservedFixedIPType = "external"
Subnet ReservedFixedIPType = "subnet"
AnySubnet ReservedFixedIPType = "any_subnet"
IPAddress ReservedFixedIPType = "ip_address"
)
func (t ReservedFixedIPType) String() string {
return string(t)
}
func (t ReservedFixedIPType) List() []ReservedFixedIPType {
return []ReservedFixedIPType{External, Subnet, AnySubnet, IPAddress}
}
func (t ReservedFixedIPType) StringList() []string {
lst := t.List()
strings := make([]string, 0, len(lst))
for _, x := range lst {
strings = append(strings, x.String())
}
return strings
}
func (t ReservedFixedIPType) IsValid() error {
switch t {
case External, Subnet, AnySubnet, IPAddress:
return nil
}
return fmt.Errorf("invalid ReservedFixedIPType type: %v", t)
}
func (t ReservedFixedIPType) ValidOrNil() (*ReservedFixedIPType, error) {
if t.String() == "" {
return nil, nil
}
err := t.IsValid()
if err != nil {
return &t, err
}
return &t, nil
}
// UnmarshalJSON - implements Unmarshaler interface for ReservedFixedIPType.
func (t *ReservedFixedIPType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
v := ReservedFixedIPType(s)
err := v.IsValid()
if err != nil {
return err
}
*t = v
return nil
}
// MarshalJSON - implements Marshaler interface for ReservedFixedIPType.
func (t *ReservedFixedIPType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}