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

Simplify type definitions. #4017

Merged
merged 5 commits into from
Oct 1, 2019
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
18 changes: 9 additions & 9 deletions dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,15 +1226,15 @@ func TestListTypeSchemaChange(t *testing.T) {
func TestDeleteAllSP2(t *testing.T) {
s := `
type Node12345 {
nodeType: string
name: string
date: dateTime
weight: float
weightUnit: string
lifeLoad: int
stressLevel: int
plan: string
postMortem: string
nodeType
name
date
weight
weightUnit
lifeLoad
stressLevel
plan
postMortem
}
`
require.NoError(t, dropAll())
Expand Down
14 changes: 7 additions & 7 deletions dgraph/cmd/live/load-json/family.schema
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type FamilyMember {
name: string
age: int
role: string
aka: string
carries: string
parent_to: [FamilyMember]
sibling_of: [FamilyMember]
name
age
role
aka
carries
parent_to
sibling_of
}

name:string @index(term) .
Expand Down
14 changes: 7 additions & 7 deletions dgraph/cmd/live/load-uids/family.schema
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type FamilyMember {
name: string
age: int
role: string
aka: string
carries: string
parent_to: [FamilyMember]
sibling_of: [FamilyMember]
name
age
role
aka
carries
parent_to
sibling_of
}

name:string @index(term) .
Expand Down
34 changes: 17 additions & 17 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,38 +192,38 @@ func addGeoMultiPolygonToCluster(uid uint64, polygons [][][][]float64) {

const testSchema = `
type Person {
name: string
pet: Animal
name
pet
}

type Animal {
name: string
name
}

type CarModel {
make: string
model: string
year: int
previous_model: CarModel
make
model
year
previous_model
}

type SchoolInfo {
name: string
abbr: string
school: [uid]
district: [uid]
state: [uid]
county: [uid]
name
abbr
school
district
state
county
}

type User {
name: string
password: password
name
password
}

type Node {
node: uid
name: string
node
name
}

name : string @index(term, exact, trigram) @count @lang .
Expand Down
10 changes: 6 additions & 4 deletions query/query3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2119,19 +2119,21 @@ func TestQueryUnknownType(t *testing.T) {
require.JSONEq(t, `{"data": {}}`, js)
}

// TODO(martinmr): Change type query representation since the type of a field is no longer
// relevant.
func TestQuerySingleType(t *testing.T) {
query := `schema(type: Person) {}`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"types":[{"name":"Person",
"fields":[{"name":"name", "type":"string"}, {"name":"pet", "type":"Animal"}]}]}}`,
"fields":[{"name":"name", "type":"default"}, {"name":"pet", "type":"default"}]}]}}`,
js)
}

func TestQueryMultipleTypes(t *testing.T) {
query := `schema(type: [Person, Animal]) {}`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"types":[{"name":"Animal",
"fields":[{"name":"name", "type":"string"}]},
{"name":"Person", "fields":[{"name":"name", "type": "string"},
{"name":"pet", "type":"Animal"}]}]}}`, js)
"fields":[{"name":"name", "type":"default"}]},
{"name":"Person", "fields":[{"name":"name", "type": "default"},
{"name":"pet", "type":"default"}]}]}}`, js)
}
27 changes: 16 additions & 11 deletions schema/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -335,7 +336,7 @@ func parseTypeDeclaration(it *lex.ItemIterator) (*pb.TypeUpdate, error) {
typeUpdate.Fields = fields
return typeUpdate, nil
case itemText:
field, err := parseTypeField(it)
field, err := parseTypeField(it, typeUpdate.TypeName)
if err != nil {
return nil, err
}
Expand All @@ -349,11 +350,19 @@ func parseTypeDeclaration(it *lex.ItemIterator) (*pb.TypeUpdate, error) {
return nil, errors.Errorf("Shouldn't reach here.")
}

func parseTypeField(it *lex.ItemIterator) (*pb.SchemaUpdate, error) {
func parseTypeField(it *lex.ItemIterator, typeName string) (*pb.SchemaUpdate, error) {
field := &pb.SchemaUpdate{Predicate: it.Item().Val}
var list bool

it.Next()

// Simplified type definitions only require the field name. If a new line is found,
// proceed to the next field in the type.
if it.Item().Typ == itemNewLine {
return field, nil
}

// For the sake of backwards-compatibility, process type definitions in the old format,
// but ignore the information after the colon.
if it.Item().Typ != itemColon {
return nil, it.Item().Errorf("Missing colon in type declaration. Got %v", it.Item().Val)
}
Expand All @@ -368,34 +377,30 @@ func parseTypeField(it *lex.ItemIterator) (*pb.SchemaUpdate, error) {
return nil, it.Item().Errorf("Missing field type in type declaration. Got %v",
it.Item().Val)
}
field.ValueType = getType(it.Item().Val)
if field.ValueType == pb.Posting_OBJECT {
field.ObjectTypeName = it.Item().Val
}

it.Next()
if it.Item().Typ == itemExclamationMark {
field.NonNullable = true
it.Next()
}

if list {
if it.Item().Typ != itemRightSquare {
return nil, it.Item().Errorf("Expected matching square bracket. Got %v", it.Item().Val)
}
field.List = true
it.Next()

if it.Item().Typ == itemExclamationMark {
field.NonNullableList = true
it.Next()
}
}

if it.Item().Typ != itemNewLine {
return nil, it.Item().Errorf("Expected new line after field declaration. Got %v", it.Item().Val)
return nil, it.Item().Errorf("Expected new line after field declaration. Got %v",
it.Item().Val)
}

glog.Warningf("Type declaration for type %s includes deprecated information about field type "+
"for field %s which will be ignored.", typeName, field.Predicate)
return field, nil
}

Expand Down
Loading