-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisibility.go
More file actions
137 lines (118 loc) · 3.35 KB
/
visibility.go
File metadata and controls
137 lines (118 loc) · 3.35 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package clccam
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
// Visibility of a CAM Entity
type Visibility uint32
const (
// Visible to Cloud Application Manager users across all organizations.
Visibility_Public Visibility = iota
// Visible to all users in the organization where the box was created.
Visibility_Organization
// By default, the box is visible only to members of the workspace where it was created.
Visibility_Workspace
// This is used e.g. by variables
Visibility_Internal
// This is used e.g. by variables
Visibility_Private
)
// Implements encoding.TextMarshaler
func (v Visibility) MarshalText() ([]byte, error) {
switch v {
case Visibility_Public:
return []byte("public"), nil
case Visibility_Organization:
return []byte("organization"), nil
case Visibility_Workspace:
return []byte("workspace"), nil
case Visibility_Internal:
return []byte("internal"), nil
case Visibility_Private:
return []byte("private"), nil
}
return nil, fmt.Errorf("invalid Visibility %d", v)
}
// Implements encoding.TextUnmarshaler
func (v *Visibility) UnmarshalText(data []byte) error {
switch string(data) {
case "public":
*v = Visibility_Public
case "organization":
*v = Visibility_Organization
case "workspace":
*v = Visibility_Workspace
case "internal":
*v = Visibility_Internal
case "private":
*v = Visibility_Private
default:
return fmt.Errorf("invalid Visibility %q", string(data))
}
return nil
}
// Implements fmt.Stringer
func (v Visibility) String() string {
if b, err := v.MarshalText(); err != nil {
return err.Error()
} else {
return string(b)
}
}
// VisibilityFromString attempts to parse @s as stringified Visibility.
func VisibilityFromString(s string) (val Visibility, err error) {
err = val.UnmarshalText([]byte(s))
return val, err
}
// VisibilityStrings returns the list of Visibility string literals, or maps @vals if non-empty.
func VisibilityStrings(vals ...Visibility) (ret []string) {
if len(vals) > 0 {
for _, val := range vals {
ret = append(ret, val.String())
}
return ret
}
return []string{"public", "organization", "workspace", "internal", "private"}
}
// Implements database/sql/driver.Valuer
func (v Visibility) Value() (driver.Value, error) {
return v.String(), nil
}
// Implements database/sql.Scanner
func (v *Visibility) Scan(src interface{}) error {
switch src := src.(type) {
case int64:
*v = Visibility(src)
return nil
case []byte:
return v.UnmarshalText(src)
case string:
return v.UnmarshalText([]byte(src))
}
return fmt.Errorf("unable to convert %T to Visibility", src)
}
// Implements json.Marshaler
func (v Visibility) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
// Implements json.Unmarshaler
func (v *Visibility) UnmarshalJSON(data []byte) error {
var output string
if err := json.Unmarshal(data, &output); err != nil {
return fmt.Errorf("failed to parse '%s' as Visibility: %s", string(data), err)
}
return v.UnmarshalText([]byte(output))
}
// Implements yaml.Marshaler
func (v Visibility) MarshalYAML() (interface{}, error) {
return v.String(), nil
}
// Implements yaml.Unmarshaler
func (v *Visibility) UnmarshalYAML(unmarshal func(interface{}) error) error {
var output string
if err := unmarshal(&output); err != nil {
return fmt.Errorf("failed to unmarshal Visibility from YAML: %s", err)
}
return v.UnmarshalText([]byte(output))
}