generated from dnnrly/goclitem
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
httpref.go
80 lines (63 loc) · 1.63 KB
/
httpref.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
package httpref
import (
"strings"
)
// Reference is a single reference item
type Reference struct {
Name string
IsTitle bool
Summary string
Description string
}
// References is a collection of Reference objects
type References []Reference
// ByName finds all of the Reference with a matching Name field
func (r References) ByName(code string) References {
results := References{}
wildcard := strings.HasSuffix(code, "*")
code = strings.ReplaceAll(code, "*", "")
for _, v := range r {
if !wildcard && v.Name == code {
return References{v}
}
if strings.HasPrefix(v.Name, code) {
results = append(results, v)
}
}
return results
}
// InRange attempts to find a numeric in a range in the Name field
func (r References) InRange(code string) References {
for _, v := range r {
if strings.Contains(v.Name, "-") {
parts := strings.Split(v.Name, "-")
start, end := parts[0], parts[1]
if code >= start && code <= end {
return References{v}
}
}
}
return References{}
}
// Titles gives all of the Reference objects marked as a title
func (r References) Titles() References {
results := References{}
for _, v := range r {
if v.IsTitle {
results = append(results, v)
}
}
return results
}
// Search looks for references that contain the search term in their fields
func (r References) Search(term string) References {
results := References{}
for _, v := range r {
if strings.Contains(strings.ToLower(v.Name), term) ||
strings.Contains(strings.ToLower(v.Summary), term) ||
strings.Contains(strings.ToLower(v.Description), term) {
results = append(results, v)
}
}
return results
}