Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equal operator typed helpers #62

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions filter/eq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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}}
}

// EqInt composes 'equal' operation from int value.
// Result is equivalent to: field == value
func EqInt(field string, value int) 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}}
}
33 changes: 33 additions & 0 deletions filter/eq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package filter

import (
"testing"
"time"

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

func TestEq(t *testing.T) {
cases := []struct {
name string
expr expr
exp string
}{
{"int", EqInt("f", 12345), `{"f":{"$eq":12345}}`},
{"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 @@ -21,6 +21,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 @@ -65,7 +66,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
22 changes: 18 additions & 4 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"reflect"
"strconv"
"strings"
"time"
"unsafe"

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

// PrimitiveFieldType represents types supported by non-composite fields
type PrimitiveFieldType interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better restrict to our supported types.

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 @@ -112,11 +122,9 @@ func translateType(t reflect.Type) (string, string, error) {
return typeString, formatByte, nil
}
return typeArray, "", nil
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int:
return typeInteger, formatInt32, nil
case reflect.Uint8, reflect.Uint16:
case reflect.Int32:
return typeInteger, formatInt32, nil
case reflect.Int64, reflect.Uint, reflect.Uint32:
case reflect.Int64:
return typeInteger, "", nil
case reflect.String:
return typeString, "", nil
Expand All @@ -126,6 +134,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
25 changes: 6 additions & 19 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,12 @@ func TestCollectionSchema(t *testing.T) {
}

type allTypes struct {
tm time.Time
tmPtr *time.Time
Int8 int8 `json:"int_8"`
Int16 int16 `json:"int_16"`
Int32 int32 `json:"int_32"`
Uint8 uint8 `json:"uint_8"`
Uint16 uint16 `json:"uint_16"`
Uint32 uint32 `json:"uint_32"`
tm time.Time
tmPtr *time.Time
Int32 int32 `json:"int_32"`

Int64 int64 `json:"int_64"`
//Uint64 uint64 `json:"uint_64"`
Int int `json:"int_1"`
Uint uint `json:"uint_1"`
Int int `json:"int_1"`

Bytes []byte `json:"bytes_1"`
BytesA [4]byte `json:"bytes_2"`
Expand Down Expand Up @@ -183,17 +176,11 @@ func TestCollectionSchema(t *testing.T) {
{Name: "tm", Type: typeString, Format: formatDateTime},
{Name: "tmPtr", Type: typeString, Format: formatDateTime},

{Name: "int_8", Type: typeInteger, Format: formatInt32},
{Name: "int_16", Type: typeInteger, Format: formatInt32},
{Name: "int_32", Type: typeInteger, Format: formatInt32},
{Name: "uint_8", Type: typeInteger, Format: formatInt32},
{Name: "uint_16", Type: typeInteger, Format: formatInt32},
{Name: "uint_32", Type: typeInteger},

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

{Name: "bytes_1", Type: typeString, Format: formatByte},
{Name: "bytes_2", Type: typeString, Format: formatByte},
Expand Down Expand Up @@ -270,7 +257,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_32","type":"integer","format":"int32"},{"title":"int_64","type":"integer"},{"title":"int_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