Skip to content

Commit

Permalink
feat: equal operator typed helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed Apr 28, 2022
1 parent c8718e1 commit 5444834
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.17.6'
go-version: '1.18'

- name: Install test deps
run: |
Expand Down
4 changes: 2 additions & 2 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ func TestHTTPDriverAuth(t *testing.T) {
}

func TestGRPCTokenRefresh(t *testing.T) {
ToekenRefreshURL = test.HTTPURL + "/token"
TokenRefreshURL = test.HTTPURL + "/token"

client, mockServer, cancel := SetupGRPCTests(t, &config.Config{Token: "token_config_123:refresh_token_123"})
defer cancel()
Expand All @@ -763,7 +763,7 @@ func TestGRPCTokenRefresh(t *testing.T) {
}

func TestHTTPTokenRefresh(t *testing.T) {
ToekenRefreshURL = test.HTTPURL + "/token"
TokenRefreshURL = test.HTTPURL + "/token"

client, mockServer, cancel := SetupHTTPTests(t, &config.Config{Token: "token_config_123:refresh_token_123"})
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion driver/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func getAuthToken(ctx context.Context, config *config.Config) (*oauth2.Token, *o
TLSClientConfig: config.TLS,
}

ocfg := &oauth2.Config{Endpoint: oauth2.Endpoint{TokenURL: ToekenRefreshURL}}
ocfg := &oauth2.Config{Endpoint: oauth2.Endpoint{TokenURL: TokenRefreshURL}}

return &t, ocfg, context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: tr})
}
4 changes: 2 additions & 2 deletions driver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const (
)

var (
DefaultProtocol = GRPC
ToekenRefreshURL = "https://tigrisdata-dev.us.auth0.com/oauth/token"
DefaultProtocol = GRPC
TokenRefreshURL = "https://tigrisdata-dev.us.auth0.com/oauth/token"
)

type Document json.RawMessage
Expand Down
45 changes: 45 additions & 0 deletions filter/eq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package filter

import "time"

// EqInt64 composes 'equal' operation from int64 value.
// Result is equivalent to: field == value
func EqInt64(field string, value int64) expr {
return expr{field: comparison{Eq: value}}
}

// EqInt32 composes 'equal' operation from int32 value.
// Result is equivalent to: field == value
func EqInt32(field string, value int32) expr {
return expr{field: comparison{Eq: value}}
}

// EqString composes 'equal' operation from string value.
// Result is equivalent to: field == value
func EqString(field string, value string) expr {
return expr{field: comparison{Eq: value}}
}

// EqBytes composes 'equal' operation from []byte value.
// Result is equivalent to: field == value
func EqBytes(field string, value []byte) expr {
return expr{field: comparison{Eq: value}}
}

// EqFloat32 composes 'equal' operation from float32 value.
// Result is equivalent to: field == value
func EqFloat32(field string, value float32) expr {
return expr{field: comparison{Eq: value}}
}

// EqFloat64 composes 'equal' operation from float64 value.
// Result is equivalent to: field == value
func EqFloat64(field string, value float64) expr {
return expr{field: comparison{Eq: value}}
}

// EqTime composes 'equal' operation. from time.Time value.
// Result is equivalent to: field == value
func EqTime(field string, value *time.Time) expr {
return expr{field: comparison{Eq: value}}
}
32 changes: 32 additions & 0 deletions filter/eq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package filter

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestEq(t *testing.T) {
cases := []struct {
name string
expr expr
exp string
}{
{"int32", EqInt32("f", 12345), `{"f":{"$eq":12345}}`},
{"int64", EqInt64("f", 123456789012), `{"f":{"$eq":123456789012}}`},
{"float32", EqFloat32("f", 12345.67), `{"f":{"$eq":12345.67}}`},
{"float64", EqFloat64("f", 123456789012.34), `{"f":{"$eq":123456789012.34}}`},
{"string", EqString("f", "1234"), `{"f":{"$eq":"1234"}}`},
{"bytes", EqBytes("f", []byte("123")), `{"f":{"$eq":"MTIz"}}`},
{"time", EqTime("f", &time.Time{}), `{"f":{"$eq":"0001-01-01T00:00:00Z"}}`},
}

for _, v := range cases {
t.Run(v.name, func(t *testing.T) {
act, err := v.expr.Build()
require.NoError(t, err)
require.Equal(t, v.exp, string(act))
})
}
}
3 changes: 2 additions & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"

"github.com/tigrisdata/tigris-client-go/driver"
"github.com/tigrisdata/tigris-client-go/schema"
)

type Operand map[string]comparison
Expand Down Expand Up @@ -51,7 +52,7 @@ func Not(op expr) expr {

// Eq composes 'equal' operation.
// Result is equivalent to: field == value
func Eq(field string, value interface{}) expr {
func Eq[T schema.PrimitiveFieldType](field string, value T) expr {
return expr{field: comparison{Eq: value}}
}

Expand Down
18 changes: 17 additions & 1 deletion schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"reflect"
"strconv"
"strings"
"time"
"unsafe"

"github.com/tigrisdata/tigris-client-go/driver"
)
Expand All @@ -29,6 +31,14 @@ const (
tagSkip = "-"
)

// PrimitiveFieldType represents types supported by non-composite fields
type PrimitiveFieldType interface {
string |
int | int32 | int64 |
float32 | float64 |
[]byte | *time.Time
}

// Supported data types
// See translateType for Golang to JSON schema translation rules
const (
Expand Down Expand Up @@ -98,7 +108,7 @@ func translateType(t reflect.Type) (string, string, error) {
return typeString, formatByte, nil
}
return typeArray, "", nil
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int:
case reflect.Int8, reflect.Int16, reflect.Int32:
return typeInteger, formatInt32, nil
case reflect.Uint8, reflect.Uint16:
return typeInteger, formatInt32, nil
Expand All @@ -112,6 +122,12 @@ func translateType(t reflect.Type) (string, string, error) {
return typeBoolean, "", nil
case reflect.Map:
return typeObject, "", nil
case reflect.Int:
var a int
if unsafe.Sizeof(a) == 4 {
return typeInteger, formatInt32, nil
}
return typeInteger, "", nil
}

return "", "", fmt.Errorf("unsupported type: '%s'", t.Name())
Expand Down
4 changes: 2 additions & 2 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestCollectionSchema(t *testing.T) {

{Name: "int_64", Type: typeInteger},
// {Name: "uint_64", Type: typeInteger},
{Name: "int_1", Type: typeInteger, Format: formatInt32},
{Name: "int_1", Type: typeInteger},
{Name: "uint_1", Type: typeInteger},

{Name: "bytes_1", Type: typeString, Format: formatByte},
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestCollectionSchema(t *testing.T) {
b, err := s.Build()
require.NoError(t, err)

require.Equal(t, `{"title":"allTypes","properties":[{"title":"tm","type":"string","format":"date-time"},{"title":"tmPtr","type":"string","format":"date-time"},{"title":"int_8","type":"integer","format":"int32"},{"title":"int_16","type":"integer","format":"int32"},{"title":"int_32","type":"integer","format":"int32"},{"title":"uint_8","type":"integer","format":"int32"},{"title":"uint_16","type":"integer","format":"int32"},{"title":"uint_32","type":"integer"},{"title":"int_64","type":"integer"},{"title":"int_1","type":"integer","format":"int32"},{"title":"uint_1","type":"integer"},{"title":"bytes_1","type":"string","format":"byte"},{"title":"bytes_2","type":"string","format":"byte"},{"title":"float_32","type":"number"},{"title":"float_64","type":"number"},{"title":"bool_1","type":"boolean"},{"title":"string_1","type":"string"},{"title":"data_1","type":"object","properties":[{"title":"field_1","type":"string"},{"title":"Nested","type":"object","properties":[{"title":"ss_field_1","type":"string"}]}]},{"title":"slice_1","type":"array","items":{"type":"string"}},{"title":"arr_1","type":"array","items":{"type":"string"}},{"title":"map_1","type":"object"},{"title":"slice_2","type":"array","items":{"title":"subStruct","type":"object","properties":[{"title":"field_1","type":"string"},{"title":"Nested","type":"object","properties":[{"title":"ss_field_1","type":"string"}]}]}},{"title":"map_2","type":"object"},{"title":"bool_123","type":"boolean"},{"title":"ptrStruct","type":"object","properties":[{"title":"ss_field_1","type":"string"}]}],"primary_key":["data_1.Nested.ss_field_1","string_1"]}`, string(b))
require.Equal(t, `{"title":"allTypes","properties":[{"title":"tm","type":"string","format":"date-time"},{"title":"tmPtr","type":"string","format":"date-time"},{"title":"int_8","type":"integer","format":"int32"},{"title":"int_16","type":"integer","format":"int32"},{"title":"int_32","type":"integer","format":"int32"},{"title":"uint_8","type":"integer","format":"int32"},{"title":"uint_16","type":"integer","format":"int32"},{"title":"uint_32","type":"integer"},{"title":"int_64","type":"integer"},{"title":"int_1","type":"integer"},{"title":"uint_1","type":"integer"},{"title":"bytes_1","type":"string","format":"byte"},{"title":"bytes_2","type":"string","format":"byte"},{"title":"float_32","type":"number"},{"title":"float_64","type":"number"},{"title":"bool_1","type":"boolean"},{"title":"string_1","type":"string"},{"title":"data_1","type":"object","properties":[{"title":"field_1","type":"string"},{"title":"Nested","type":"object","properties":[{"title":"ss_field_1","type":"string"}]}]},{"title":"slice_1","type":"array","items":{"type":"string"}},{"title":"arr_1","type":"array","items":{"type":"string"}},{"title":"map_1","type":"object"},{"title":"slice_2","type":"array","items":{"title":"subStruct","type":"object","properties":[{"title":"field_1","type":"string"},{"title":"Nested","type":"object","properties":[{"title":"ss_field_1","type":"string"}]}]}},{"title":"map_2","type":"object"},{"title":"bool_123","type":"boolean"},{"title":"ptrStruct","type":"object","properties":[{"title":"ss_field_1","type":"string"}]}],"primary_key":["data_1.Nested.ss_field_1","string_1"]}`, string(b))
})

t.Run("multiple_models", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/install_test_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set -ex
export GO111MODULE=on

go install github.com/golang/mock/mockgen@v1.6.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)/bin" v1.44.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)/bin" v1.45.2

if [[ "$OSTYPE" == "darwin"* ]]; then
if command -v brew > /dev/null 2>&1; then
Expand Down

0 comments on commit 5444834

Please sign in to comment.