Skip to content

Commit

Permalink
Merge df61b63 into 34c996b
Browse files Browse the repository at this point in the history
  • Loading branch information
wildan2711 committed Mar 16, 2020
2 parents 34c996b + df61b63 commit 37bbab0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ require (
dmitri.shuralyov.com/gpu/mtl v0.0.0-20191203043605-d42048ed14fd // indirect
github.com/cncf/udpa/go v0.0.0-20200124205748-db4b343e48c1 // indirect
github.com/creack/pty v1.1.9 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dgraph-io/dgo v1.0.0
github.com/dgraph-io/dgo/v2 v2.1.0
github.com/envoyproxy/go-control-plane v0.9.2 // indirect
github.com/gin-gonic/gin v1.5.0
github.com/golang/mock v1.4.0 // indirect
github.com/golang/protobuf v1.3.2
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515
github.com/kr/pretty v0.2.0 // indirect
github.com/kr/pty v1.1.8 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_model v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.5.2 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
Expand All @@ -27,15 +23,14 @@ require (
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
golang.org/x/mobile v0.0.0-20200123024942-82c397c4c527 // indirect
golang.org/x/mod v0.2.0 // indirect
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9
golang.org/x/text v0.3.2
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
golang.org/x/tools v0.0.0-20200125223703-d33eef8e6825 // indirect
golang.org/x/tools/gopls v0.1.8-0.20191220234730-f13409bbebaf // indirect
google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150
google.golang.org/grpc v1.26.0
gopkg.in/yaml.v2 v2.2.8 // indirect
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919 // indirect
rsc.io/sampler v1.99.99 // indirect
)

go 1.13
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand All @@ -93,22 +94,27 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand Down Expand Up @@ -230,6 +236,7 @@ golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200107162124-548cf772de50/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -322,8 +329,10 @@ google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg=
google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
Expand Down
5 changes: 5 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type TxnInterface interface {
Get(model interface{}) *Query
}

// SchemaType allows defining a custom type as a dgraph schema type
type SchemaType interface {
SchemaType() string
}

var (
_ TxnInterface = (*TxnContext)(nil)
)
19 changes: 12 additions & 7 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,29 +193,34 @@ func getSchemaType(fieldType reflect.Type) string {
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
schemaType := fieldType.Name()

// check if implements SchemaType
schemaTypeElem := reflect.New(fieldType).Interface()
if schemaTyper, ok := schemaTypeElem.(SchemaType); ok {
return schemaTyper.SchemaType()
}

switch fieldType.Kind() {
case reflect.Slice:
sliceType := fieldType.Elem()
schemaType = fmt.Sprintf("[%s]", getSchemaType(sliceType))
return fmt.Sprintf("[%s]", getSchemaType(sliceType))
case reflect.Struct:
switch fieldType.PkgPath() {
case "time":
// golang std time
schemaType = "datetime"
return "datetime"
default:
// one-to-one relation
schemaType = "uid"
return "uid"
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
schemaType = "int"
return "int"
case reflect.Float32, reflect.Float64:
schemaType = "float"
return "float"
}

return schemaType
return fieldType.Name()
}

func getPredicate(field *reflect.StructField) string {
Expand Down
16 changes: 12 additions & 4 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (

type EnumType int

type CustomTime time.Time

func (c CustomTime) SchemaType() string {
return "datetime"
}

type GeoLoc struct {
Type string `json:"type"`
Coord []float64 `json:"coordinates"`
Expand All @@ -39,6 +45,7 @@ type User struct {
Password string `json:"password,omitempty"`
Height *int `json:"height,omitempty"`
IsAdmin bool `json:"is_admin,omitempty"`
CustomTime CustomTime `json:"custom_time,omitempty"`
Dob *time.Time `json:"dob,omitempty"`
Status EnumType `json:"status,omitempty" dgraph:"type=int"`
Created *time.Time `json:"created,omitempty"`
Expand Down Expand Up @@ -83,7 +90,7 @@ func TestMarshalSchema(t *testing.T) {
typeSchema := NewTypeSchema()
typeSchema.Marshal(true, &User{})
types, schema := typeSchema.Types, typeSchema.Schema
assert.Len(t, schema, 20)
assert.Len(t, schema, 21)
assert.Equal(t, "username: string @index(hash) @upsert .", schema["username"].String())
assert.Equal(t, "email: string @index(hash) @upsert .", schema["email"].String())
assert.Equal(t, "noconflict: string @index(hash) @noconflict .", schema["noconflict"].String())
Expand All @@ -96,6 +103,7 @@ func TestMarshalSchema(t *testing.T) {
assert.Equal(t, "school_ptr: uid @count @reverse .", schema["school_ptr"].String())
assert.Equal(t, "status: int .", schema["status"].String())
assert.Equal(t, "height: int .", schema["height"].String())
assert.Equal(t, "custom_time: datetime .", schema["custom_time"].String())
assert.Equal(t, "dob: datetime .", schema["dob"].String())
assert.Equal(t, "is_admin: bool .", schema["is_admin"].String())
assert.Equal(t, "created: datetime .", schema["created"].String())
Expand All @@ -108,7 +116,7 @@ func TestMarshalSchema(t *testing.T) {
assert.Len(t, types, 2)
assert.Contains(t, types, "User")
assert.Contains(t, types, "School")
assert.Len(t, types["User"], 17)
assert.Len(t, types["User"], 18)
assert.Len(t, types["School"], 2)
}

Expand All @@ -132,7 +140,7 @@ func TestCreateSchema(t *testing.T) {
if err != nil {
t.Error(err)
}
assert.Len(t, firstSchema.Schema, 20)
assert.Len(t, firstSchema.Schema, 21)
assert.Len(t, firstSchema.Types, 2)

secondSchema, err := CreateSchema(c, &NewUser{})
Expand All @@ -159,7 +167,7 @@ func TestMutateSchema(t *testing.T) {
if err != nil {
t.Error(err)
}
assert.Len(t, firstSchema.Schema, 20)
assert.Len(t, firstSchema.Schema, 21)
assert.Len(t, firstSchema.Types, 2)

secondSchema, err := MutateSchema(c, &NewUser{})
Expand Down

0 comments on commit 37bbab0

Please sign in to comment.