Skip to content

Commit

Permalink
feat: Add DateTime custom scalars (#931)
Browse files Browse the repository at this point in the history
Adds support for DateTime custom scalar types in schemas and filter query arguments. At the moment this doesn't support NonNull DateTimes yet.
  • Loading branch information
jsimnz authored Nov 19, 2022
1 parent 4b14839 commit 82e0d2f
Show file tree
Hide file tree
Showing 47 changed files with 1,600 additions and 189 deletions.
4 changes: 2 additions & 2 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ const (
FieldKind_FLOAT FieldKind = 6
FieldKind_FLOAT_ARRAY FieldKind = 7
FieldKind_DECIMAL FieldKind = 8
FieldKind_DATE FieldKind = 9
FieldKind_TIMESTAMP FieldKind = 10
_ FieldKind = 9 // safe to repurpose (previoulsy old field)
FieldKind_DATETIME FieldKind = 10
FieldKind_STRING FieldKind = 11
FieldKind_STRING_ARRAY FieldKind = 12
FieldKind_BYTES FieldKind = 13
Expand Down
6 changes: 6 additions & 0 deletions connor/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package connor

import (
"reflect"
"time"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/connor/numbers"
ctime "github.com/sourcenetwork/defradb/connor/time"
"github.com/sourcenetwork/defradb/core"
)

Expand Down Expand Up @@ -58,6 +60,8 @@ func eq(condition, data any) (bool, error) {
return false, nil
case int64:
return numbers.Equal(cn, data), nil
case int:
return numbers.Equal(cn, data), nil
case float64:
return numbers.Equal(cn, data), nil
case map[FilterKey]any:
Expand All @@ -76,6 +80,8 @@ func eq(condition, data any) (bool, error) {
}

return m, nil
case time.Time:
return ctime.Equal(cn, data), nil
default:
return reflect.DeepEqual(condition, data), nil
}
Expand Down
51 changes: 34 additions & 17 deletions connor/ge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connor

import (
"fmt"
"time"

"github.com/sourcenetwork/defradb/connor/numbers"
"github.com/sourcenetwork/defradb/errors"
Expand All @@ -15,26 +16,42 @@ func ge(condition, data any) (bool, error) {
return true, nil
}

switch cn := numbers.TryUpcast(condition).(type) {
case float64:
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn >= cn, nil
case int64:
return float64(dn) > cn, nil
switch c := condition.(type) {
case time.Time:
switch d := data.(type) {
case time.Time:
return d.After(c) || d.Equal(c), nil
case string:
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
return false, err
}
return dt.After(c) || dt.Equal(c), nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}

return false, nil
case int64:
switch dn := numbers.TryUpcast(data).(type) {
default:
switch cn := numbers.TryUpcast(condition).(type) {
case float64:
return dn >= float64(cn), nil
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn >= cn, nil
case int64:
return float64(dn) > cn, nil
}

return false, nil
case int64:
return dn >= cn, nil
}
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn >= float64(cn), nil
case int64:
return dn >= cn, nil
}

return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}
}
}
51 changes: 34 additions & 17 deletions connor/gt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connor

import (
"fmt"
"time"

"github.com/sourcenetwork/defradb/connor/numbers"
"github.com/sourcenetwork/defradb/errors"
Expand All @@ -14,26 +15,42 @@ func gt(condition, data any) (bool, error) {
return data != nil, nil
}

switch cn := numbers.TryUpcast(condition).(type) {
case float64:
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn > cn, nil
case int64:
return float64(dn) > cn, nil
switch c := condition.(type) {
case time.Time:
switch d := data.(type) {
case time.Time:
return d.After(c), nil
case string:
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
return false, err
}
return dt.After(c), nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}

return false, nil
case int64:
switch dn := numbers.TryUpcast(data).(type) {
default:
switch cn := numbers.TryUpcast(condition).(type) {
case float64:
return dn > float64(cn), nil
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn > cn, nil
case int64:
return float64(dn) > cn, nil
}

return false, nil
case int64:
return dn > cn, nil
}
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn > float64(cn), nil
case int64:
return dn > cn, nil
}

return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}
}
}
4 changes: 3 additions & 1 deletion connor/in.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connor

import "github.com/sourcenetwork/defradb/errors"
import (
"github.com/sourcenetwork/defradb/errors"
)

// in will determine whether a value exists within the
// condition's array of available values.
Expand Down
51 changes: 34 additions & 17 deletions connor/le.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connor

import (
"fmt"
"time"

"github.com/sourcenetwork/defradb/connor/numbers"
"github.com/sourcenetwork/defradb/errors"
Expand All @@ -15,26 +16,42 @@ func le(condition, data any) (bool, error) {
return data == nil, nil
}

switch cn := numbers.TryUpcast(condition).(type) {
case float64:
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn <= cn, nil
case int64:
return float64(dn) <= cn, nil
switch c := condition.(type) {
case time.Time:
switch d := data.(type) {
case time.Time:
return d.Before(c) || d.Equal(c), nil
case string:
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
return false, err
}
return dt.Before(c) || dt.Equal(c), nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}

return false, nil
case int64:
switch dn := numbers.TryUpcast(data).(type) {
default:
switch cn := numbers.TryUpcast(condition).(type) {
case float64:
return dn <= float64(cn), nil
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn <= cn, nil
case int64:
return float64(dn) <= cn, nil
}

return false, nil
case int64:
return dn <= cn, nil
}
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn <= float64(cn), nil
case int64:
return dn <= cn, nil
}

return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}
}
}
51 changes: 34 additions & 17 deletions connor/lt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connor

import (
"fmt"
"time"

"github.com/sourcenetwork/defradb/connor/numbers"
"github.com/sourcenetwork/defradb/errors"
Expand All @@ -15,26 +16,42 @@ func lt(condition, data any) (bool, error) {
return false, nil
}

switch cn := numbers.TryUpcast(condition).(type) {
case float64:
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn < cn, nil
case int64:
return float64(dn) < cn, nil
switch c := condition.(type) {
case time.Time:
switch d := data.(type) {
case time.Time:
return d.Before(c), nil
case string:
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
return false, err
}
return dt.Before(c), nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}

return false, nil
case int64:
switch dn := numbers.TryUpcast(data).(type) {
default:
switch cn := numbers.TryUpcast(condition).(type) {
case float64:
return dn < float64(cn), nil
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn < cn, nil
case int64:
return float64(dn) < cn, nil
}

return false, nil
case int64:
return dn < cn, nil
}
switch dn := numbers.TryUpcast(data).(type) {
case float64:
return dn < float64(cn), nil
case int64:
return dn < cn, nil
}

return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
return false, nil
default:
return false, errors.New(fmt.Sprintf("unknown comparison type '%#v'", condition))
}
}
}
29 changes: 29 additions & 0 deletions connor/time/equality.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package time

import "time"

func Equal(condition, data any) bool {
switch c := condition.(type) {
case time.Time:
switch d := data.(type) {
case time.Time:
return d.Equal(c)
case string:
// todo: Not sure if we should be
// parsing incoming data here, or
// if the DB should handle this.
// (Note: This isnt the user provided
// condition on a query, but the data
// stored in DB for a document
dt, err := time.Parse(time.RFC3339, d)
if err != nil {
return false
}
return dt.Equal(c)
default:
return false
}
default:
return false
}
}
3 changes: 3 additions & 0 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Parser interface {
// Parses the given request, returning a strongly typed model of that request.
Parse(request string) (*request.Request, []error)

// NewFilterFromString creates a new filter from a string.
NewFilterFromString(collectionType string, body string) (client.Option[request.Filter], error)

// Adds the given schema to this parser's model.
AddSchema(ctx context.Context, schema string) error
}
Loading

0 comments on commit 82e0d2f

Please sign in to comment.