-
Notifications
You must be signed in to change notification settings - Fork 6
/
environment_value.go
66 lines (54 loc) · 1.48 KB
/
environment_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
62
63
64
65
66
package revcatgo
import (
"errors"
"fmt"
"strings"
"gopkg.in/guregu/null.v4"
)
type environment struct {
value null.String
}
const (
EnvironmentSandbox = "SANDBOX"
EnvironmentProduction = "PRODUCTION"
)
var validEnvironmentValues = []string{
EnvironmentSandbox,
EnvironmentProduction,
}
func newEnvironment(s string) (*environment, error) {
if !contains(validEnvironmentValues, s) {
return &environment{}, fmt.Errorf("environment value should be one of the following: %v, got %v", strings.Join(validEnvironmentValues, ","), s)
}
return &environment{value: null.StringFrom(s)}, nil
}
func (e environment) String() string {
return e.value.ValueOrZero()
}
func (e *environment) IsProduction() bool {
return e.value.ValueOrZero() == EnvironmentProduction
}
func (e *environment) IsSandBox() bool {
return e.value.ValueOrZero() == EnvironmentSandbox
}
// MarshalJSON serializes a store to JSON.
func (e environment) MarshalJSON() ([]byte, error) {
return e.value.MarshalJSON()
}
// UnmarshalJSON deserializes a store from JSON
func (e *environment) UnmarshalJSON(b []byte) error {
v := &environment{}
err := v.value.UnmarshalJSON(b)
if err != nil {
return fmt.Errorf("failed to unmarshal the value of environment: %w", err)
}
if !v.value.Valid {
return errors.New("environment is a required field")
}
_e, err := newEnvironment(v.value.ValueOrZero())
if err != nil {
return fmt.Errorf("failed to unmarshal the value of environment: %w", err)
}
e.value = _e.value
return nil
}