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
package manifests
import (
"bytes"
"encoding/json"
"errors"
)
type NameGrant struct {
All bool
Names []string
}
func (g NameGrant) IsZero() bool { return !g.All && len(g.Names) == 0 }
func (g NameGrant) MarshalJSON() ([]byte, error) {
if g.All {
return []byte("true"), nil
}
if len(g.Names) == 0 {
return []byte("false"), nil
}
return json.Marshal(g.Names)
}
func (g *NameGrant) UnmarshalJSON(b []byte) error {
b = bytes.TrimSpace(b)
switch {
case bytes.Equal(b, []byte("null")), bytes.Equal(b, []byte("false")):
*g = NameGrant{}
return nil
case bytes.Equal(b, []byte("true")):
*g = NameGrant{All: true}
return nil
}
var names []string
if err := json.Unmarshal(b, &names); err != nil {
return errors.New("a grant is true, false or a list of names")
}
*g = NameGrant{Names: names}
return nil
}