/
types.go
99 lines (85 loc) · 2.44 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package wix
import (
"encoding/json"
"errors"
)
// YesNoType implements Wix.YesNoType
type YesNoType int
// Supported YesNoType values
const (
YesNoUnspecified = YesNoType(iota)
Yes
No
)
// InstallUninstallType implements Wix.InstallUninstallType
type InstallUninstallType int
// Supported InstallUninstallType values
const (
InstallUninstallUnspecified = InstallUninstallType(iota) // unspecified
InstallOnly // install-only action
UninstallOnly // uninstall-only action
InstallAndUninstall // action that occurs both when installing and uninstalling
)
// ErrInvalidYesNoValue is an error that indicates invalid YesNoType value
var ErrInvalidYesNoValue = errors.New("invalid InstallUninstallType value")
// ToYesNo converts true->Yes and false->No
func ToYesNo(v bool) YesNoType {
if v {
return Yes
}
return No
}
func (v YesNoType) toString() (string, error) {
switch v {
case YesNoUnspecified:
return "unspecified", nil
case Yes:
return "yes", nil
case No:
return "no", nil
default:
return "<invalid>", ErrInvalidInstallUninstall
}
}
// MarshalJSON is used when writing YesNoType into a JSON stream
func (v *YesNoType) MarshalJSON() ([]byte, error) {
s, err := v.toString()
if err != nil {
return nil, err
}
return json.Marshal(s)
}
// MarshalText implements TextMarshaler interface for YesNoType
func (v *YesNoType) MarshalText() ([]byte, error) {
s, err := v.toString()
return []byte(s), err
}
// ErrInvalidInstallUninstall is an error that indicates invalid InstallUninstallType value
var ErrInvalidInstallUninstall = errors.New("invalid InstallUninstallType value")
func (v InstallUninstallType) toString() (string, error) {
switch v {
case InstallUninstallUnspecified:
return "unspecified", nil
case InstallOnly:
return "install", nil
case UninstallOnly:
return "uninstall", nil
case InstallAndUninstall:
return "both", nil
default:
return "<invalid>", ErrInvalidInstallUninstall
}
}
// MarshalJSON is used when writing InstallUninstallType into a JSON stream
func (v *InstallUninstallType) MarshalJSON() ([]byte, error) {
s, err := v.toString()
if err != nil {
return nil, err
}
return json.Marshal(s)
}
// MarshalText implements TextMarshaler interface for InstallUninstallType
func (v *InstallUninstallType) MarshalText() ([]byte, error) {
s, err := v.toString()
return []byte(s), err
}