-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch_group.go
103 lines (81 loc) · 2.06 KB
/
patch_group.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
100
101
102
103
package payloads
import (
"errors"
"io"
"regexp"
"github.com/google/uuid"
)
type GroupPatchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
type GroupPatchPayload struct {
Schemas []string `json:"schemas"`
Operations []*GroupPatchOperation `json:"Operations"`
}
type GroupPatch struct {
DisplayName string
}
func GroupPatchPayloadFromJSON(r io.Reader) (*GroupPatchPayload, error) {
var payload GroupPatchPayload
err := decodeJSON(r, &payload)
if err != nil {
return nil, err
}
return &payload, nil
}
type MemberPatch struct {
Display string
Value uuid.UUID
}
func (o *GroupPatchOperation) ParseIDFromPath() (uuid.UUID, error) {
r := regexp.MustCompile(`^members\[value eq "(\S+)"]$`)
match := r.FindStringSubmatch(o.Path)
if match == nil {
return uuid.UUID{}, errors.New("invalid path")
}
idString := match[1]
id, err := uuid.Parse(idString)
if err != nil {
return uuid.UUID{}, errors.New("invalid value type")
}
return id, nil
}
func (o *GroupPatchOperation) GetPatch() (GroupPatch, error) {
values, ok := o.Value.(map[string]interface{})
if !ok {
return GroupPatch{}, errors.New("invalid value type")
}
displayName, ok := values["displayName"].(string)
if !ok {
return GroupPatch{}, errors.New("invalid value type")
}
patch := GroupPatch{
DisplayName: displayName,
}
return patch, nil
}
func (o *GroupPatchOperation) GetAddMembersPatch() ([]uuid.UUID, error) {
members, ok := o.Value.([]interface{})
if !ok {
return []uuid.UUID{}, errors.New("invalid value type")
}
var patches []uuid.UUID
for _, memberInterface := range members {
member, ok := memberInterface.(map[string]interface{})
if !ok {
return []uuid.UUID{}, errors.New("invalid value type")
}
value, ok := member["value"].(string)
if !ok {
return []uuid.UUID{}, errors.New("invalid value type")
}
id, err := uuid.Parse(value)
if err != nil {
return []uuid.UUID{}, errors.New("invalid value type")
}
patches = append(patches, id)
}
return patches, nil
}