MCPcopy
hub / github.com/dgraph-io/dgraph / parseTypeDeclaration

Function parseTypeDeclaration

schema/parse.go:470–525  ·  view source on GitHub ↗
(it *lex.ItemIterator, ns uint64)

Source from the content-addressed store, hash-verified

468}
469
470func parseTypeDeclaration(it *lex.ItemIterator, ns uint64) (*pb.TypeUpdate, error) {
471 // Iterator is currently on the token corresponding to the keyword type.
472 if it.Item().Typ != itemText || it.Item().Val != "type" {
473 return nil, it.Item().Errorf("Expected type keyword. Got %v", it.Item().Val)
474 }
475
476 it.Next()
477 if it.Item().Typ != itemText {
478 return nil, it.Item().Errorf("Expected type name. Got %v", it.Item().Val)
479 }
480 typeUpdate := &pb.TypeUpdate{TypeName: x.NamespaceAttr(ns, it.Item().Val)}
481
482 it.Next()
483 if it.Item().Typ != itemLeftCurl {
484 return nil, it.Item().Errorf("Expected {. Got %v", it.Item().Val)
485 }
486
487 var fields []*pb.SchemaUpdate
488 for it.Next() {
489 item := it.Item()
490
491 switch item.Typ {
492 case itemRightCurl:
493 it.Next()
494 if it.Item().Typ != itemNewLine && it.Item().Typ != lex.ItemEOF {
495 return nil, it.Item().Errorf(
496 "Expected new line or EOF after type declaration. Got %v", it.Item())
497 }
498 it.Prev()
499
500 fieldSet := make(map[string]struct{})
501 for _, field := range fields {
502 if _, ok := fieldSet[field.GetPredicate()]; ok {
503 return nil, it.Item().Errorf("Duplicate fields with name: %s",
504 x.ParseAttr(field.GetPredicate()))
505 }
506
507 fieldSet[field.GetPredicate()] = struct{}{}
508 }
509
510 typeUpdate.Fields = fields
511 return typeUpdate, nil
512 case itemText:
513 field, err := parseTypeField(it, typeUpdate.TypeName, ns)
514 if err != nil {
515 return nil, err
516 }
517 fields = append(fields, field)
518 case itemNewLine:
519 // Ignore empty lines.
520 default:
521 return nil, it.Item().Errorf("Unexpected token. Got %v", it.Item().Val)
522 }
523 }
524 return nil, errors.Errorf("Shouldn't reach here.")
525}
526
527func parseTypeField(it *lex.ItemIterator, typeName string, ns uint64) (*pb.SchemaUpdate, error) {

Callers 1

parseFunction · 0.85

Calls 8

NamespaceAttrFunction · 0.92
ParseAttrFunction · 0.92
parseTypeFieldFunction · 0.85
ItemMethod · 0.80
PrevMethod · 0.80
ErrorfMethod · 0.45
NextMethod · 0.45
GetPredicateMethod · 0.45

Tested by

no test coverage detected