config: custom AutoUpdate type for validity
Henry committed
Oct 20, 2014 at 18:27 UTC
24e1e6d1e1cb1ab128976fe928b5b2f16c8f8c45
3 files changed
+96
-13
config/version.go
+59
-12
@@ -1,7 +1,9 @@
1
package config
2
3
import (
4
+ "errors"
5
"strconv"
6
+ "strings"
7
"time"
8
)
9
@@ -23,12 +25,8 @@ type Version struct {
25
// (Note: cannot use time.Duration because marshalling with json breaks it)
26
CheckPeriod string
27
26
- // AutoUpdate is optional and has these these options:
27
- // - "never" do not auto-update
28
- // - "patch" auto-update on new patch versions
29
- // - "minor" auto-update on new minor (or patch) versions (Default)
30
- // - "major" auto-update on any new version
31
- AutoUpdate string
28
+ // AutoUpdate is optional
29
+ AutoUpdate AutoUpdateSetting
30
}
31
32
// supported Version.Check values
@@ -43,13 +41,62 @@ const (
41
CheckIgnore = "ignore"
42
)
43
46
-// supported Version.AutoUpdate values
47
-// BUG(cryptix): make this a custom type that implements json.Unmarshaller() to verify values
44
+// AutoUpdateSetting implements json.Unmarshaler to check values in config
45
+// supported values:
46
+// "never" - do not auto-update
47
+// "patch" - auto-update on new patch versions
48
+// "minor" - auto-update on new minor (or patch) versions (Default)
49
+// "major" - auto-update on any new version
50
+type AutoUpdateSetting int
51
+
52
+// UnmarshalJSON checks the input against known strings
53
+func (s *AutoUpdateSetting) UnmarshalJSON(in []byte) error {
54
+
55
+ switch strings.ToLower(string(in)) {
56
+ case `"never"`:
57
+ *s = UpdateNever
58
+ case `"major"`:
59
+ *s = UpdateMajor
60
+ case `"minor"`:
61
+ *s = UpdateMinor
62
+ case `"patch"`:
63
+ *s = UpdatePatch
64
+ default:
65
+ *s = UpdateMinor
66
+ return ErrUnknownAutoUpdateSetting
67
+ }
68
+ return nil
69
+}
70
+
71
+// MarshalJSON converts the value back to JSON string
72
+func (s AutoUpdateSetting) MarshalJSON() ([]byte, error) {
73
+ return []byte(`"` + s.String() + `"`), nil
74
+}
75
+
76
+// String converts valye to human readable string
77
+func (s AutoUpdateSetting) String() string {
78
+ switch s {
79
+ case UpdateNever:
80
+ return "never"
81
+ case UpdateMajor:
82
+ return "major"
83
+ case UpdateMinor:
84
+ return "minor"
85
+ case UpdatePatch:
86
+ return "patch"
87
+ default:
88
+ return ErrUnknownAutoUpdateSetting.Error()
89
+ }
90
+}
91
+
92
+// ErrUnknownAutoUpdateSetting is returned when an unknown value is read from the config
93
+var ErrUnknownAutoUpdateSetting = errors.New("unknown value for AutoUpdate")
94
+
95
const (
49
- UpdateNever = "never"
50
- UpdatePatch = "patch"
51
- UpdateMinor = "minor"
52
- UpdateMajor = "major"
96
+ UpdateMinor AutoUpdateSetting = iota // first value so that it is the zero value and thus the default
97
+ UpdatePatch
98
+ UpdateMajor
99
+ UpdateNever
100
)
101
102
// defaultCheckPeriod governs h
config/version_test.go
new
+36
@@ -0,0 +1,36 @@
1
+package config
2
+
3
+import (
4
+ "strings"
5
+ "testing"
6
+)
7
+
8
+func TestAutoUpdateValues(t *testing.T) {
9
+ var tval struct {
10
+ AutoUpdate AutoUpdateSetting
11
+ }
12
+ tests := []struct {
13
+ input string
14
+ val AutoUpdateSetting
15
+ err error
16
+ }{
17
+ {`{"hello":123}`, UpdateMinor, nil}, // default
18
+ {`{"AutoUpdate": "never"}`, UpdateNever, nil},
19
+ {`{"AutoUpdate": "patch"}`, UpdatePatch, nil},
20
+ {`{"AutoUpdate": "minor"}`, UpdateMinor, nil},
21
+ {`{"AutoUpdate": "major"}`, UpdateMajor, nil},
22
+ {`{"AutoUpdate": "blarg"}`, UpdateMinor, ErrUnknownAutoUpdateSetting},
23
+ }
24
+
25
+ for i, tc := range tests {
26
+ err := Decode(strings.NewReader(tc.input), &tval)
27
+ if err != tc.err {
28
+ t.Fatalf("%d failed - got err %q wanted %v", i, err, tc.err)
29
+ }
30
+
31
+ if tval.AutoUpdate != tc.val {
32
+ t.Fatalf("%d failed - got val %q where we wanted %q", i, tval.AutoUpdate, tc.val)
33
+ }
34
+ }
35
+
36
+}
updates/updates.go
+1
-1
@@ -64,7 +64,7 @@ func AbleToApply() error {
64
65
// ShouldAutoUpdate decides wether a new version should be applied
66
// checks against config setting and new version string. returns false in case of error
67
-func ShouldAutoUpdate(setting, newVer string) bool {
67
+func ShouldAutoUpdate(setting config.AutoUpdateSetting, newVer string) bool {
68
if setting == config.UpdateNever {
69
return false
70
}