Skip to content

Commit d114cb8

Browse files
committed
Fix broken builds against Go 1.6 and 1.7
Avoid using sort.Slice when building against Go 1.6 or 1.7, instead using the older sort.Sort interface. Signed-off-by: Free Ekanayaka <[email protected]>
1 parent 023a4bb commit d114cb8

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

inmem_endpoint.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package metrics
33
import (
44
"fmt"
55
"net/http"
6-
"sort"
76
"time"
87
)
98

@@ -69,9 +68,7 @@ func (i *InmemSink) DisplayMetrics(resp http.ResponseWriter, req *http.Request)
6968
for name, points := range interval.Points {
7069
summary.Points = append(summary.Points, PointValue{name, points})
7170
}
72-
sort.Slice(summary.Points, func(i, j int) bool {
73-
return summary.Points[i].Name < summary.Points[j].Name
74-
})
71+
sortPoints(summary.Points)
7572

7673
for hash, value := range interval.Gauges {
7774
value.Hash = hash
@@ -83,9 +80,7 @@ func (i *InmemSink) DisplayMetrics(resp http.ResponseWriter, req *http.Request)
8380

8481
summary.Gauges = append(summary.Gauges, value)
8582
}
86-
sort.Slice(summary.Gauges, func(i, j int) bool {
87-
return summary.Gauges[i].Hash < summary.Gauges[j].Hash
88-
})
83+
sortGauges(summary.Gauges)
8984

9085
summary.Counters = formatSamples(interval.Counters)
9186
summary.Samples = formatSamples(interval.Samples)
@@ -110,9 +105,7 @@ func formatSamples(source map[string]SampledValue) []SampledValue {
110105
DisplayLabels: displayLabels,
111106
})
112107
}
113-
sort.Slice(output, func(i, j int) bool {
114-
return output[i].Hash < output[j].Hash
115-
})
108+
sortSampled(output)
116109

117110
return output
118111
}

inmem_endpoint_1_6.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// +build go1.6,!go1.8
2+
3+
package metrics
4+
5+
import (
6+
"sort"
7+
)
8+
9+
// Sort the given a PointValue slice in place using Go 1.8's sort.Slice.
10+
func sortPoints(points []PointValue) {
11+
sort.Sort(pointValues(points))
12+
}
13+
14+
type pointValues []PointValue
15+
16+
func (points pointValues) Len() int {
17+
return len(points)
18+
}
19+
20+
func (points pointValues) Less(i, j int) bool {
21+
return points[i].Name < points[j].Name
22+
}
23+
24+
func (points pointValues) Swap(i, j int) {
25+
points[i], points[j] = points[j], points[1]
26+
}
27+
28+
// Sort the given a GaugeValue slice in place using Go 1.8's sort.Slice.
29+
func sortGauges(gauges []GaugeValue) {
30+
sort.Sort(gaugeValues(gauges))
31+
}
32+
33+
type gaugeValues []GaugeValue
34+
35+
func (gauges gaugeValues) Len() int {
36+
return len(gauges)
37+
}
38+
39+
func (gauges gaugeValues) Less(i, j int) bool {
40+
return gauges[i].Hash < gauges[j].Hash
41+
}
42+
43+
func (gauges gaugeValues) Swap(i, j int) {
44+
gauges[i], gauges[j] = gauges[j], gauges[1]
45+
}
46+
47+
// Sort the given a SampledValue slice in place using Go 1.8's sort.Slice.
48+
func sortSampled(sampleds []SampledValue) {
49+
sort.Sort(sampledValues(sampleds))
50+
}
51+
52+
type sampledValues []SampledValue
53+
54+
func (sampled sampledValues) Len() int {
55+
return len(sampled)
56+
}
57+
58+
func (sampled sampledValues) Less(i, j int) bool {
59+
return sampled[i].Hash < sampled[j].Hash
60+
}
61+
62+
func (sampled sampledValues) Swap(i, j int) {
63+
sampled[i], sampled[j] = sampled[j], sampled[1]
64+
}

inmem_endpoint_1_8.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// +build go1.8
2+
3+
package metrics
4+
5+
import (
6+
"sort"
7+
)
8+
9+
// Sort the given PointValue slice in place using Go 1.8's sort.Slice.
10+
func sortPoints(points []PointValue) {
11+
sort.Slice(points, func(i, j int) bool {
12+
return points[i].Name < points[j].Name
13+
})
14+
}
15+
16+
// Sort the given GaugeValue slice in place using Go 1.8's sort.Slice.
17+
func sortGauges(gauges []GaugeValue) {
18+
sort.Slice(gauges, func(i, j int) bool {
19+
return gauges[i].Name < gauges[j].Name
20+
})
21+
}
22+
23+
// Sort the given SampledValue slice in place using Go 1.8's sort.Slice.
24+
func sortSampled(sampled []SampledValue) {
25+
sort.Slice(sampled, func(i, j int) bool {
26+
return sampled[i].Name < sampled[j].Name
27+
})
28+
}

0 commit comments

Comments
 (0)