-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
56 lines (49 loc) · 988 Bytes
/
role.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
package grafana
import (
"encoding/json"
"fmt"
)
// Role enumerate possible user roles in an organization.
// If role is unknown, it will default to None.
type Role int8
const (
RoleNone Role = iota
RoleViewer
RoleEditor
RoleAdmin
)
// String implements fmt.Stringer.
func (r Role) String() string {
switch r {
case RoleViewer:
return "Viewer"
case RoleEditor:
return "Editor"
case RoleAdmin:
return "Admin"
case RoleNone:
return "None"
default:
panic(fmt.Errorf("unknown role: %v", int8(r)))
}
}
// UnmarshalJSON implements json.Unmarshaler.
func (r *Role) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"Viewer"`:
*r = RoleViewer
case `"Editor"`:
*r = RoleEditor
case `"Admin"`:
*r = RoleAdmin
case `"None"`:
*r = RoleNone
default:
return fmt.Errorf("unknown role: %v", string(data))
}
return nil
}
// MarshalJSON implements json.Marshaler.
func (r Role) MarshalJSON() ([]byte, error) {
return json.Marshal(r.String())
}