-
Notifications
You must be signed in to change notification settings - Fork 5
/
period_type_value.go
61 lines (51 loc) · 1.35 KB
/
period_type_value.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
package revcatgo
import (
"errors"
"fmt"
"strings"
"gopkg.in/guregu/null.v4"
)
const (
PeriodTypeTrial = "TRIAL"
PeriodTypeIntro = "INTRO"
PeriodTypeNormal = "NORMAL"
PeriodTypePromotional = "PROMOTIONAL"
)
var validPeriodTypeValues = []string{
PeriodTypeTrial,
PeriodTypeIntro,
PeriodTypeNormal,
PeriodTypePromotional,
}
type periodType struct {
value null.String
}
func newPeriodType(s string) (*periodType, error) {
if !contains(validPeriodTypeValues, s) {
return &periodType{}, fmt.Errorf("periodType value should be one of the following: %v, got %v", strings.Join(validPeriodTypeValues, ","), s)
}
return &periodType{value: null.StringFrom(s)}, nil
}
func (p periodType) String() string {
return p.value.ValueOrZero()
}
func (p periodType) MarshalJSON() ([]byte, error) {
return p.value.MarshalJSON()
}
// UnmarshalJSON deserializes a store from JSON
func (p *periodType) UnmarshalJSON(b []byte) error {
v := &periodType{}
err := v.value.UnmarshalJSON(b)
if err != nil {
return fmt.Errorf("failed to unmarshal the value of period_type: %w", err)
}
if !v.value.Valid {
return errors.New("period_type is a required field")
}
_p, err := newPeriodType(strings.ToUpper(v.value.ValueOrZero()))
if err != nil {
return fmt.Errorf("failed to unmarshal the value of period_type: %w", err)
}
p.value = _p.value
return nil
}