-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkSet.go
81 lines (69 loc) · 2 KB
/
linkSet.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
package digit
type LinkSet []Link
func NewLinkSet(capacity int) LinkSet {
return make(LinkSet, 0, capacity)
}
// Append adds a new link into the set without inspecting its contents
func (set *LinkSet) Append(links ...Link) {
(*set) = append(*set, links...)
}
// Find returns the first link that matches the provided link (having identical
// "rel" and "type" properties)
func (set LinkSet) Find(link Link) Link {
for _, target := range set {
if target.Matches(link) {
return target
}
}
return NewLink("", "", "")
}
// Apply searches for the first link that matches (with identical "rel" and "type"
// properties) the given link. If found, then the first matching item is updated.
// If not, then a new link is inserted
func (set *LinkSet) Apply(link Link) {
for index, target := range *set {
if link.Matches(target) {
(*set)[index] = link
return
}
}
*set = append(*set, link)
}
// Remove removes all items from the set that match the given link (having identical "rel"
// and "type" properties)
func (set *LinkSet) Remove(link Link) {
for index, target := range *set {
if link.Matches(target) {
*set = append((*set)[:index], (*set)[index+1:]...)
}
}
}
// FindBy returns the first link with a property that matches the given value
func (set LinkSet) FindBy(name string, value string) Link {
for _, link := range set {
if link.GetString(name) == value {
return link
}
}
return NewLink("", "", "")
}
// RemoveBy removes the first link with a property that matches the given value
func (set *LinkSet) RemoveBy(name string, value string) {
for index, link := range *set {
if link.GetString(name) == value {
(*set) = append((*set)[:index], (*set)[index+1:]...)
break
}
}
}
// ApplyBy searches for a matching link, updates it if found, and appends it if not
func (set *LinkSet) ApplyBy(name string, link Link) {
keyID := link.GetString(name)
for index, target := range *set {
if target.GetString(name) == keyID {
(*set)[index] = link
return
}
}
(*set) = append(*set, link)
}