-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscopes.go
54 lines (48 loc) · 1.06 KB
/
scopes.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
package core
import (
"slices"
"strings"
)
type Scope string
type Scopes []Scope
type ScopesString string
func (s *Scopes) Add(scopes ...Scope) {
for _, scope := range scopes {
if slices.Contains(*s, scope) {
continue
}
*s = append(*s, scope)
}
}
func (s *Scopes) Remove(toBeRemoved ...Scope) {
if s == nil {
return
}
result := make(Scopes, 0, len(*s)) // Capacity can't be lower because we can't know if there are repeated scopes to be removed...
for _, existingScope := range *s {
if slices.Contains(toBeRemoved, existingScope) {
continue
}
result = append(result, existingScope)
}
*s = result
}
func (s Scopes) AsScopesString() ScopesString {
// Can't use 'strings.Join' because of 'Scopes' type
var result ScopesString
for i, scope := range s {
if i > 0 {
result += " "
}
result += ScopesString(scope)
}
return result
}
func (s ScopesString) AsScopes() Scopes {
strSlice := strings.Split(string(s), " ")
result := make(Scopes, len(strSlice))
for i, scopeStr := range strSlice {
result[i] = Scope(scopeStr)
}
return result
}