Skip to content

Commit

Permalink
drop support for all types other than float64 and string
Browse files Browse the repository at this point in the history
  • Loading branch information
huttotw committed Feb 28, 2018
1 parent c123cb3 commit 7e2023c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 341 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ This package was created with inspiration from Thomas' [go-ruler](https://github

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

**Note**: This package only compares two types: `string` and `float64`, this plays nicely with `encoding/json`.

# Example
```go
// Create a new instance of an engine with some default comparators
Expand Down Expand Up @@ -71,9 +73,14 @@ BenchmarkContainsLong50000-8|100000000|55.6 ns/op|32 B/op|1 allocs/op|
BenchmarkPluckShallow-8|100000000|60.2 ns/op|16 B/op|1 allocs/op|
BenchmarkPluckDeep-8|20000000|242 ns/op|112 B/op|1 allocs/op|

To run benchmarks:
```
go test -run none -bench . -benchtime 3s -benchmem
```

# License

Copyright 2017 Trevor Hutto
Copyright © 2018 Trevor Hutto

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:

Expand Down
259 changes: 42 additions & 217 deletions comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,6 @@ func lessThan(a, b interface{}) bool {
switch ta.Kind() {
case reflect.String:
return a.(string) < b.(string)
case reflect.Int:
return a.(int) < b.(int)
case reflect.Int8:
return a.(int8) < b.(int8)
case reflect.Int16:
return a.(int16) < b.(int16)
case reflect.Int32:
return a.(int32) < b.(int32)
case reflect.Int64:
return a.(int64) < b.(int64)
case reflect.Uint:
return a.(uint) < b.(uint)
case reflect.Uint8:
return a.(uint8) < b.(uint8)
case reflect.Uint16:
return a.(uint16) < b.(uint16)
case reflect.Uint32:
return a.(uint32) < b.(uint32)
case reflect.Uint64:
return a.(uint64) < b.(uint64)
case reflect.Float32:
return a.(float32) < b.(float32)
case reflect.Float64:
return a.(float64) < b.(float64)
}
Expand Down Expand Up @@ -83,28 +61,6 @@ func lessThanEqual(a, b interface{}) bool {
switch ta.Kind() {
case reflect.String:
return a.(string) <= b.(string)
case reflect.Int:
return a.(int) <= b.(int)
case reflect.Int8:
return a.(int8) <= b.(int8)
case reflect.Int16:
return a.(int16) <= b.(int16)
case reflect.Int32:
return a.(int32) <= b.(int32)
case reflect.Int64:
return a.(int64) <= b.(int64)
case reflect.Uint:
return a.(uint) <= b.(uint)
case reflect.Uint8:
return a.(uint8) <= b.(uint8)
case reflect.Uint16:
return a.(uint16) <= b.(uint16)
case reflect.Uint32:
return a.(uint32) <= b.(uint32)
case reflect.Uint64:
return a.(uint64) <= b.(uint64)
case reflect.Float32:
return a.(float32) <= b.(float32)
case reflect.Float64:
return a.(float64) <= b.(float64)
}
Expand All @@ -114,22 +70,56 @@ func lessThanEqual(a, b interface{}) bool {

// greaterThan will return true if a > b
func greaterThan(a, b interface{}) bool {
// We need to check the types here because, lessThanEqual will
// return false if the types do not match.
if reflect.TypeOf(a) != reflect.TypeOf(b) {
// If the values are equal, no more work necessary
if a == b {
return true
}

ta := reflect.TypeOf(a)
tb := reflect.TypeOf(b)

// Make sure the types are the same
if ta != tb {
return false
}
return !lessThanEqual(a, b)

// We have already checked that each argument is the same type
// so it is safe to only check the first argument
switch ta.Kind() {
case reflect.String:
return a.(string) > b.(string)
case reflect.Float64:
return a.(float64) > b.(float64)
}

return false
}

// greaterThanEqual will return true if a >= b
func greaterThanEqual(a, b interface{}) bool {
// We need to check the types here because, lessThan will
// return false if the types do not match.
if reflect.TypeOf(a) != reflect.TypeOf(b) {
// If the values are equal, no more work necessary
if a == b {
return true
}

ta := reflect.TypeOf(a)
tb := reflect.TypeOf(b)

// Make sure the types are the same
if ta != tb {
return false
}
return !lessThan(a, b)

// We have already checked that each argument is the same type
// so it is safe to only check the first argument
switch ta.Kind() {
case reflect.String:
return a.(string) >= b.(string)
case reflect.Float64:
return a.(float64) >= b.(float64)
}

return false
}

// contains will return true if a contains b. We assume
Expand All @@ -146,28 +136,6 @@ func contains(a, b interface{}) bool {
switch t2.Kind() {
case reflect.String:
return containsString(a, b)
case reflect.Int:
return containsInt(a, b)
case reflect.Int8:
return containsInt8(a, b)
case reflect.Int16:
return containsInt16(a, b)
case reflect.Int32:
return containsInt32(a, b)
case reflect.Int64:
return containsInt64(a, b)
case reflect.Uint:
return containsUint(a, b)
case reflect.Uint8:
return containsUint8(a, b)
case reflect.Uint16:
return containsUint16(a, b)
case reflect.Uint32:
return containsUint32(a, b)
case reflect.Uint64:
return containsUint64(a, b)
case reflect.Float32:
return containsFloat32(a, b)
case reflect.Float64:
return containsFloat64(a, b)
default:
Expand All @@ -188,149 +156,6 @@ func containsString(a, b interface{}) bool {
return false
}

func containsInt(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(int); ok && val == b.(int) {
return true
}
}
return false
}

func containsInt8(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(int8); ok && val == b.(int8) {
return true
}
}
return false
}

func containsInt16(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(int16); ok && val == b.(int16) {
return true
}
}
return false
}

func containsInt32(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(int32); ok && val == b.(int32) {
return true
}
}
return false
}

func containsInt64(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(int64); ok && val == b.(int64) {
return true
}
}
return false
}

func containsUint(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(uint); ok && val == b.(uint) {
return true
}
}
return false
}

func containsUint8(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(uint8); ok && val == b.(uint8) {
return true
}
}
return false
}

func containsUint16(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(uint16); ok && val == b.(uint16) {
return true
}
}
return false
}

func containsUint32(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(uint32); ok && val == b.(uint32) {
return true
}
}
return false
}

func containsUint64(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(uint64); ok && val == b.(uint64) {
return true
}
}
return false
}

func containsFloat32(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
return false
}
for _, elem := range as {
if val, ok := elem.(float32); ok && val == b.(float32) {
return true
}
}
return false
}

func containsFloat64(a, b interface{}) bool {
as, ok := a.([]interface{})
if !ok {
Expand Down
Loading

0 comments on commit 7e2023c

Please sign in to comment.