-
Notifications
You must be signed in to change notification settings - Fork 9
/
resultset.go
115 lines (101 loc) · 2.76 KB
/
resultset.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Copyright 2016-2018 Bryan T. Meyers <bmeyers@datadrake.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package results
import (
"fmt"
"sort"
)
// ResultSet is a collection of the Results of a Provider query
type ResultSet struct {
results []*Result
query string
}
// NewResultSet creates as empty ResultSet for the provided query
func NewResultSet(query string) *ResultSet {
return &ResultSet{make([]*Result, 0), query}
}
var skipWords = []string{
"master", "Master", "MASTER",
"rc", "RC",
"alpha", "Alpha", "ALPHA",
"beta", "Beta", "BETA",
"dev", "DEV",
"unstable", "Unstable", "UNSTABLE",
"eap", "EAP",
}
// AddResult appends a new Result
func (rs *ResultSet) AddResult(r *Result) {
if r == nil {
return
}
if r.Version[0] == "N/A" {
return
}
for _, part := range r.Version {
for _, skip := range skipWords {
if part == skip {
return
}
}
}
rs.results = append(rs.results, r)
}
// Empty checks if there were no results
func (rs *ResultSet) Empty() bool {
return len(rs.results) == 0
}
// First retrieves the first result from a query
func (rs *ResultSet) First() *Result {
return rs.results[0]
}
// Last retrieves the first result from a query
func (rs *ResultSet) Last() *Result {
switch len(rs.results) {
case 0:
return nil
case 1:
return rs.results[0]
default:
return rs.results[len(rs.results)-1]
}
}
// PrintAll pretty-prints an entire ResultSet
func (rs *ResultSet) PrintAll() {
fmt.Printf("%-25s: '%s'\n", "Results of Query", rs.query)
fmt.Printf("%-25s: %d\n\n", "Total Number of Results", len(rs.results))
sort.Sort(rs)
for _, r := range rs.results {
r.Print()
}
}
// PrintFirst pretty-prints the first result
func (rs *ResultSet) PrintFirst() {
fmt.Printf("First Result of Query: '%s'\n", rs.query)
rs.results[0].Print()
}
// Len is the number of elements in the ResultSet (sort.Interface)
func (rs *ResultSet) Len() int {
return len(rs.results)
}
// Less reports whether the element with
// index i should sort before the element with index j. (sort.Interface)
func (rs *ResultSet) Less(i, j int) bool {
return !rs.results[i].Version.Less(rs.results[j].Version)
}
// Swap swaps the elements with indexes i and j.
func (rs *ResultSet) Swap(i, j int) {
rs.results[i], rs.results[j] = rs.results[j], rs.results[i]
}