Skip to content

Commit

Permalink
Address PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Mar 7, 2019
1 parent b6d13c4 commit 295bbfc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 58 deletions.
4 changes: 2 additions & 2 deletions calculation/value.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package calculation

import (
"bytes"
"encoding/json"
"fmt"
"math"

"github.com/xichen2020/eventdb/document/field"
"github.com/xichen2020/eventdb/generated/proto/servicepb"
"github.com/xichen2020/eventdb/x/bytes"
"github.com/xichen2020/eventdb/x/compare"
"github.com/xichen2020/eventdb/x/safe"
)
Expand Down Expand Up @@ -41,7 +41,7 @@ func NewValueFromProto(pbValue servicepb.CalculationValue) (ValueUnion, error) {
v.NumberVal = pbValue.NumberVal
case servicepb.CalculationValue_STRING:
v.Type = StringType
v.StringVal = []byte(pbValue.StringVal)
v.StringVal = safe.ToBytes(pbValue.StringVal)
default:
return v, fmt.Errorf("invalid protobuf calculation value type %v", pbValue.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion document/field/value.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package field

import (
"bytes"
"encoding/json"
"fmt"
"math"

"github.com/xichen2020/eventdb/generated/proto/servicepb"
"github.com/xichen2020/eventdb/x/bytes"
"github.com/xichen2020/eventdb/x/compare"
"github.com/xichen2020/eventdb/x/safe"

Expand Down
20 changes: 11 additions & 9 deletions filter/string_filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package filter

import (
"github.com/xichen2020/eventdb/x/bytes"
"bytes"

xbytes "github.com/xichen2020/eventdb/x/bytes"
)

// StringFilter matches against string values.
Expand Down Expand Up @@ -30,49 +32,49 @@ func notEqualsStringString(rhs []byte) stringFilterFn {

func largerThanStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return bytes.GreaterThan(lhs, rhs)
return xbytes.GreaterThan(lhs, rhs)
}
}

func largerThanOrEqualStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return bytes.GreaterThanOrEqual(lhs, rhs)
return xbytes.GreaterThanOrEqual(lhs, rhs)
}
}

func smallerThanStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return bytes.LessThan(lhs, rhs)
return xbytes.LessThan(lhs, rhs)
}
}

func smallerThanOrEqualStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return bytes.LessThanOrEqual(lhs, rhs)
return xbytes.LessThanOrEqual(lhs, rhs)
}
}

func startsWithStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return bytes.StartsWith(lhs, rhs)
return bytes.HasPrefix(lhs, rhs)
}
}

func doesNotStartWithStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return !bytes.StartsWith(lhs, rhs)
return !bytes.HasPrefix(lhs, rhs)
}
}

func endsWithStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return bytes.EndsWith(lhs, rhs)
return bytes.HasSuffix(lhs, rhs)
}
}

func doesNotEndWithStringString(rhs []byte) stringFilterFn {
return func(lhs []byte) bool {
return !bytes.EndsWith(lhs, rhs)
return !bytes.HasSuffix(lhs, rhs)
}
}

Expand Down
48 changes: 2 additions & 46 deletions x/bytes/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GreaterThan(v1, v2 []byte) bool {

// GreaterThanOrEqual returns true if v1 > v2 || v1 == v2.
func GreaterThanOrEqual(v1, v2 []byte) bool {
return GreaterThan(v1, v2) || bytes.Equal(v1, v2)
return bytes.Compare(v1, v2) != compareResultLessThan
}

// LessThan returns true if v1 < v2.
Expand All @@ -26,49 +26,5 @@ func LessThan(v1, v2 []byte) bool {

// LessThanOrEqual returns true if v1 < v2 || v1 == v2.
func LessThanOrEqual(v1, v2 []byte) bool {
return LessThan(v1, v2) || bytes.Equal(v1, v2)
}

// StartsWith returns true if the v1's starting byte sequence matches v2.
func StartsWith(v1, v2 []byte) bool {
if len(v2) > len(v1) {
return false
}

for idx := range v2 {
if v1[idx] != v2[idx] {
return false
}
}
return true
}

// EndsWith returns true if the v1's ending byte sequence matches v2.
func EndsWith(v1, v2 []byte) bool {
if len(v2) > len(v1) {
return false
}

offset := len(v1) - len(v2)
for idx := range v2 {
if v1[idx+offset] != v2[idx] {
return false
}
}
return true
}

// Equal thinly wraps `bytes.Equal` to allow for consistent imports.
func Equal(v1, v2 []byte) bool {
return bytes.Equal(v1, v2)
}

// Compare thinly wraps `bytes.Compare` to allow for consistent imports.
func Compare(v1, v2 []byte) int {
return bytes.Compare(v1, v2)
}

// Contains thinly wraps `bytes.Contains` to allow for consistent imports.
func Contains(v1, v2 []byte) bool {
return bytes.Contains(v1, v2)
return bytes.Compare(v1, v2) != compareResultGreaterThan
}
File renamed without changes.

0 comments on commit 295bbfc

Please sign in to comment.