-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_division.go
73 lines (66 loc) · 1.85 KB
/
auth_division.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
package gcloudcx
import (
"encoding/json"
"github.com/gildas/go-errors"
"github.com/google/uuid"
)
type AuthorizationDivision struct {
ID uuid.UUID `json:"-"`
SelfUri string `json:"selfUri"`
Name string `json:"name"`
Description string `json:"description"` // required
IsHome bool `json:"homeDivision"`
ObjectCounts map[string]int `json:"objectCounts"`
}
// GetID gets the identifier
//
// implements core.Identifiable
func (division AuthorizationDivision) GetID() uuid.UUID {
return division.ID
}
// String returns a string representation of the AuthorizationDivision
//
// implements fmt.Stringer
func (division AuthorizationDivision) String() string {
if len(division.Name) > 0 {
return division.Name
}
return division.ID.String()
}
func (division AuthorizationDivision) MarshalJSON() ([]byte, error) {
type surrogate AuthorizationDivision
inner := struct {
surrogate
ID string `json:"id"`
}{
surrogate: surrogate(division),
ID: "*",
}
if division.ID != uuid.Nil {
inner.ID = division.ID.String()
}
data, err := json.Marshal(inner)
return data, errors.JSONMarshalError.Wrap(err)
}
func (division *AuthorizationDivision) UnmarshalJSON(payload []byte) (err error) {
type surrogate AuthorizationDivision
var inner struct {
surrogate
ID string `json:"id"`
}
if err = json.Unmarshal(payload, &inner); errors.Is(err, errors.JSONUnmarshalError) {
return err
} else if err != nil {
return errors.JSONUnmarshalError.Wrap(err)
}
(*division) = AuthorizationDivision(inner.surrogate)
if inner.ID != "*" {
if division.ID, err = uuid.Parse(inner.ID); err != nil {
return errors.WrapErrors(errors.JSONMarshalError, errors.ArgumentInvalid.With("id", inner.ID), err)
}
}
if division.ObjectCounts == nil {
division.ObjectCounts = make(map[string]int)
}
return nil
}