Skip to content

Commit 7e2023c

Browse files
author
huttotw
committed
drop support for all types other than float64 and string
1 parent c123cb3 commit 7e2023c

File tree

6 files changed

+94
-341
lines changed

6 files changed

+94
-341
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ This package was created with inspiration from Thomas' [go-ruler](https://github
33

44
This version includes a couple more features including, AND and OR composites and the ability to add custom comparators.
55

6+
**Note**: This package only compares two types: `string` and `float64`, this plays nicely with `encoding/json`.
7+
68
# Example
79
```go
810
// Create a new instance of an engine with some default comparators
@@ -71,9 +73,14 @@ BenchmarkContainsLong50000-8|100000000|55.6 ns/op|32 B/op|1 allocs/op|
7173
BenchmarkPluckShallow-8|100000000|60.2 ns/op|16 B/op|1 allocs/op|
7274
BenchmarkPluckDeep-8|20000000|242 ns/op|112 B/op|1 allocs/op|
7375

76+
To run benchmarks:
77+
```
78+
go test -run none -bench . -benchtime 3s -benchmem
79+
```
80+
7481
# License
7582

76-
Copyright 2017 Trevor Hutto
83+
Copyright © 2018 Trevor Hutto
7784

7885
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
7986

comparators.go

+42-217
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,6 @@ func lessThan(a, b interface{}) bool {
3434
switch ta.Kind() {
3535
case reflect.String:
3636
return a.(string) < b.(string)
37-
case reflect.Int:
38-
return a.(int) < b.(int)
39-
case reflect.Int8:
40-
return a.(int8) < b.(int8)
41-
case reflect.Int16:
42-
return a.(int16) < b.(int16)
43-
case reflect.Int32:
44-
return a.(int32) < b.(int32)
45-
case reflect.Int64:
46-
return a.(int64) < b.(int64)
47-
case reflect.Uint:
48-
return a.(uint) < b.(uint)
49-
case reflect.Uint8:
50-
return a.(uint8) < b.(uint8)
51-
case reflect.Uint16:
52-
return a.(uint16) < b.(uint16)
53-
case reflect.Uint32:
54-
return a.(uint32) < b.(uint32)
55-
case reflect.Uint64:
56-
return a.(uint64) < b.(uint64)
57-
case reflect.Float32:
58-
return a.(float32) < b.(float32)
5937
case reflect.Float64:
6038
return a.(float64) < b.(float64)
6139
}
@@ -83,28 +61,6 @@ func lessThanEqual(a, b interface{}) bool {
8361
switch ta.Kind() {
8462
case reflect.String:
8563
return a.(string) <= b.(string)
86-
case reflect.Int:
87-
return a.(int) <= b.(int)
88-
case reflect.Int8:
89-
return a.(int8) <= b.(int8)
90-
case reflect.Int16:
91-
return a.(int16) <= b.(int16)
92-
case reflect.Int32:
93-
return a.(int32) <= b.(int32)
94-
case reflect.Int64:
95-
return a.(int64) <= b.(int64)
96-
case reflect.Uint:
97-
return a.(uint) <= b.(uint)
98-
case reflect.Uint8:
99-
return a.(uint8) <= b.(uint8)
100-
case reflect.Uint16:
101-
return a.(uint16) <= b.(uint16)
102-
case reflect.Uint32:
103-
return a.(uint32) <= b.(uint32)
104-
case reflect.Uint64:
105-
return a.(uint64) <= b.(uint64)
106-
case reflect.Float32:
107-
return a.(float32) <= b.(float32)
10864
case reflect.Float64:
10965
return a.(float64) <= b.(float64)
11066
}
@@ -114,22 +70,56 @@ func lessThanEqual(a, b interface{}) bool {
11470

11571
// greaterThan will return true if a > b
11672
func greaterThan(a, b interface{}) bool {
117-
// We need to check the types here because, lessThanEqual will
118-
// return false if the types do not match.
119-
if reflect.TypeOf(a) != reflect.TypeOf(b) {
73+
// If the values are equal, no more work necessary
74+
if a == b {
75+
return true
76+
}
77+
78+
ta := reflect.TypeOf(a)
79+
tb := reflect.TypeOf(b)
80+
81+
// Make sure the types are the same
82+
if ta != tb {
12083
return false
12184
}
122-
return !lessThanEqual(a, b)
85+
86+
// We have already checked that each argument is the same type
87+
// so it is safe to only check the first argument
88+
switch ta.Kind() {
89+
case reflect.String:
90+
return a.(string) > b.(string)
91+
case reflect.Float64:
92+
return a.(float64) > b.(float64)
93+
}
94+
95+
return false
12396
}
12497

12598
// greaterThanEqual will return true if a >= b
12699
func greaterThanEqual(a, b interface{}) bool {
127-
// We need to check the types here because, lessThan will
128-
// return false if the types do not match.
129-
if reflect.TypeOf(a) != reflect.TypeOf(b) {
100+
// If the values are equal, no more work necessary
101+
if a == b {
102+
return true
103+
}
104+
105+
ta := reflect.TypeOf(a)
106+
tb := reflect.TypeOf(b)
107+
108+
// Make sure the types are the same
109+
if ta != tb {
130110
return false
131111
}
132-
return !lessThan(a, b)
112+
113+
// We have already checked that each argument is the same type
114+
// so it is safe to only check the first argument
115+
switch ta.Kind() {
116+
case reflect.String:
117+
return a.(string) >= b.(string)
118+
case reflect.Float64:
119+
return a.(float64) >= b.(float64)
120+
}
121+
122+
return false
133123
}
134124

135125
// contains will return true if a contains b. We assume
@@ -146,28 +136,6 @@ func contains(a, b interface{}) bool {
146136
switch t2.Kind() {
147137
case reflect.String:
148138
return containsString(a, b)
149-
case reflect.Int:
150-
return containsInt(a, b)
151-
case reflect.Int8:
152-
return containsInt8(a, b)
153-
case reflect.Int16:
154-
return containsInt16(a, b)
155-
case reflect.Int32:
156-
return containsInt32(a, b)
157-
case reflect.Int64:
158-
return containsInt64(a, b)
159-
case reflect.Uint:
160-
return containsUint(a, b)
161-
case reflect.Uint8:
162-
return containsUint8(a, b)
163-
case reflect.Uint16:
164-
return containsUint16(a, b)
165-
case reflect.Uint32:
166-
return containsUint32(a, b)
167-
case reflect.Uint64:
168-
return containsUint64(a, b)
169-
case reflect.Float32:
170-
return containsFloat32(a, b)
171139
case reflect.Float64:
172140
return containsFloat64(a, b)
173141
default:
@@ -188,149 +156,6 @@ func containsString(a, b interface{}) bool {
188156
return false
189157
}
190158

191-
func containsInt(a, b interface{}) bool {
192-
as, ok := a.([]interface{})
193-
if !ok {
194-
return false
195-
}
196-
for _, elem := range as {
197-
if val, ok := elem.(int); ok && val == b.(int) {
198-
return true
199-
}
200-
}
201-
return false
202-
}
203-
204-
func containsInt8(a, b interface{}) bool {
205-
as, ok := a.([]interface{})
206-
if !ok {
207-
return false
208-
}
209-
for _, elem := range as {
210-
if val, ok := elem.(int8); ok && val == b.(int8) {
211-
return true
212-
}
213-
}
214-
return false
215-
}
216-
217-
func containsInt16(a, b interface{}) bool {
218-
as, ok := a.([]interface{})
219-
if !ok {
220-
return false
221-
}
222-
for _, elem := range as {
223-
if val, ok := elem.(int16); ok && val == b.(int16) {
224-
return true
225-
}
226-
}
227-
return false
228-
}
229-
230-
func containsInt32(a, b interface{}) bool {
231-
as, ok := a.([]interface{})
232-
if !ok {
233-
return false
234-
}
235-
for _, elem := range as {
236-
if val, ok := elem.(int32); ok && val == b.(int32) {
237-
return true
238-
}
239-
}
240-
return false
241-
}
242-
243-
func containsInt64(a, b interface{}) bool {
244-
as, ok := a.([]interface{})
245-
if !ok {
246-
return false
247-
}
248-
for _, elem := range as {
249-
if val, ok := elem.(int64); ok && val == b.(int64) {
250-
return true
251-
}
252-
}
253-
return false
254-
}
255-
256-
func containsUint(a, b interface{}) bool {
257-
as, ok := a.([]interface{})
258-
if !ok {
259-
return false
260-
}
261-
for _, elem := range as {
262-
if val, ok := elem.(uint); ok && val == b.(uint) {
263-
return true
264-
}
265-
}
266-
return false
267-
}
268-
269-
func containsUint8(a, b interface{}) bool {
270-
as, ok := a.([]interface{})
271-
if !ok {
272-
return false
273-
}
274-
for _, elem := range as {
275-
if val, ok := elem.(uint8); ok && val == b.(uint8) {
276-
return true
277-
}
278-
}
279-
return false
280-
}
281-
282-
func containsUint16(a, b interface{}) bool {
283-
as, ok := a.([]interface{})
284-
if !ok {
285-
return false
286-
}
287-
for _, elem := range as {
288-
if val, ok := elem.(uint16); ok && val == b.(uint16) {
289-
return true
290-
}
291-
}
292-
return false
293-
}
294-
295-
func containsUint32(a, b interface{}) bool {
296-
as, ok := a.([]interface{})
297-
if !ok {
298-
return false
299-
}
300-
for _, elem := range as {
301-
if val, ok := elem.(uint32); ok && val == b.(uint32) {
302-
return true
303-
}
304-
}
305-
return false
306-
}
307-
308-
func containsUint64(a, b interface{}) bool {
309-
as, ok := a.([]interface{})
310-
if !ok {
311-
return false
312-
}
313-
for _, elem := range as {
314-
if val, ok := elem.(uint64); ok && val == b.(uint64) {
315-
return true
316-
}
317-
}
318-
return false
319-
}
320-
321-
func containsFloat32(a, b interface{}) bool {
322-
as, ok := a.([]interface{})
323-
if !ok {
324-
return false
325-
}
326-
for _, elem := range as {
327-
if val, ok := elem.(float32); ok && val == b.(float32) {
328-
return true
329-
}
330-
}
331-
return false
332-
}
333-
334159
func containsFloat64(a, b interface{}) bool {
335160
as, ok := a.([]interface{})
336161
if !ok {

0 commit comments

Comments
 (0)