-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
83 lines (73 loc) · 1.88 KB
/
utils.go
File metadata and controls
83 lines (73 loc) · 1.88 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
package scimpatch
import (
"github.com/elimity-com/scim"
"github.com/elimity-com/scim/schema"
)
var (
attributeMutabilityImmutable = "immutable"
attributeMutabilityReadOnly = "readOnly"
)
func cannotBePatched(op string, attr schema.CoreAttribute) bool {
return isImmutable(op, attr) || isReadOnly(attr)
}
func isImmutable(op string, attr schema.CoreAttribute) bool {
return attr.Mutability() == attributeMutabilityImmutable && (op == scim.PatchOperationReplace || op == scim.PatchOperationRemove)
}
func isReadOnly(attr schema.CoreAttribute) bool {
return attr.Mutability() == attributeMutabilityReadOnly
}
func areEveryItemsMap(s interface{}) ([]map[string]interface{}, bool) {
switch typed := s.(type) {
case []map[string]interface{}:
return typed, true
case []interface{}:
maps := []map[string]interface{}{}
for _, item := range typed {
if map_, ok := item.(map[string]interface{}); ok {
maps = append(maps, map_)
} else {
return nil, false
}
}
return maps, true
default:
return nil, false
}
}
func mergeMap(mergee map[string]interface{}, merger map[string]interface{}) (map[string]interface{}, bool) {
merged := false
for mergerKey, mergerValue := range merger {
if mergeeValue, ok := mergee[mergerKey]; !ok || mergeeValue != mergerValue {
mergee[mergerKey] = mergerValue
merged = true
}
}
return mergee, merged
}
func eqMap(m1 map[string]interface{}, m2 map[string]interface{}) bool {
if len(m1) != len(m2) {
return false
}
for m1k, m1v := range m1 {
if m2v, ok := m2[m1k]; !ok || m2v != m1v {
return false
}
}
return true
}
func containsMap(slice []map[string]interface{}, item map[string]interface{}) bool {
for _, v := range slice {
if eqMap(v, item) {
return true
}
}
return false
}
func containsItem(slice []interface{}, item interface{}) bool {
for _, v := range slice {
if v == item {
return true
}
}
return false
}