-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresultslice.go
90 lines (73 loc) · 1.88 KB
/
resultslice.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
package graphlator
// ResultSlice is the type used for functions and Results themselves.
// This type exists to simplify the code base for manipulating slices of results
// instead of having duplicate code for the Function type and Result type.
type ResultSlice []Result
// Insert will insert any Result regardless if it already exists in the slice
// or not.
func (r *ResultSlice) Insert(res ...Result) {
*r = append(*r, res...)
}
// Upsert will add only new results given. Any pre-existing results
// will be omitted and then the integer result is the number of results
// that ended up being added to the slice.
func (r *ResultSlice) Upsert(res ...Result) int {
var added int
for _, rr := range res {
if !r.Exists(rr.want) {
added++
*r = append(*r, rr)
}
}
return added
}
// Exists is just a simple conditional that will return a boolean value
// instead of a reference of a Result unlike Find.
func (r ResultSlice) Exists(want string) bool {
for _, res := range r {
if res.want == want {
return true
}
}
return false
}
// IsEmpty is a conditional to see if the result slice has no children.
func (r ResultSlice) IsEmpty() bool {
return len(r) == 0
}
// Find returns a reference to a result that contains the matching string.
func (r ResultSlice) Find(want string) *Result {
for _, res := range r {
if res.want == want {
return &res
}
}
return nil
}
// Remove will find any surface level results and slice out the found
// result with the matching string.
func (r *ResultSlice) Remove(want string) bool {
if r.IsEmpty() {
return false
}
var found bool
var index int
for i, res := range *r {
if res.want == want {
index = i
found = true
break
}
}
if found {
results := *r
if len(*r) <= index+1 {
results = results[:index]
} else {
results = append(results[:index], results[index+1:]...)
}
*r = results
return true
}
return false
}