parse parses a schema string and returns the schema representation for it. If namespace == math.MaxUint64, then it preserves the namespace. Else it forces the passed namespace on schema/types. Example schema: [ns1] name: string . [ns2] age: string . parse(schema, 0) --> All the schema fields go to n
(s string, namespace uint64)
| 636 | // parse(schema, x) --> All the schema fields go to namespace x. |
| 637 | // parse(schema, math.MaxUint64) --> name (ns1), age(ns2) // Preserve the namespace |
| 638 | func parse(s string, namespace uint64) (*ParsedSchema, error) { |
| 639 | var result ParsedSchema |
| 640 | |
| 641 | var l lex.Lexer |
| 642 | l.Reset(s) |
| 643 | l.Run(lexText) |
| 644 | if err := l.ValidateResult(); err != nil { |
| 645 | return nil, err |
| 646 | } |
| 647 | |
| 648 | parseTypeOrSchema := func(item lex.Item, it *lex.ItemIterator, ns uint64) error { |
| 649 | if isTypeDeclaration(item, it) { |
| 650 | typeUpdate, err := parseTypeDeclaration(it, ns) |
| 651 | if err != nil { |
| 652 | return err |
| 653 | } |
| 654 | result.Types = append(result.Types, typeUpdate) |
| 655 | return nil |
| 656 | } |
| 657 | |
| 658 | schema, err := parseScalarPair(it, item.Val, ns) |
| 659 | if err != nil { |
| 660 | return err |
| 661 | } |
| 662 | result.Preds = append(result.Preds, schema) |
| 663 | return nil |
| 664 | } |
| 665 | |
| 666 | it := l.NewIterator() |
| 667 | for it.Next() { |
| 668 | item := it.Item() |
| 669 | switch item.Typ { |
| 670 | case lex.ItemEOF: |
| 671 | if err := resolveTokenizers(result.Preds); err != nil { |
| 672 | return nil, errors.Wrapf(err, "failed to enrich schema") |
| 673 | } |
| 674 | return &result, nil |
| 675 | |
| 676 | case itemText: |
| 677 | // For schema which does not contain the namespace information, use the default |
| 678 | // namespace, if namespace has to be preserved. Else, use the passed namespace. |
| 679 | ns := x.RootNamespace |
| 680 | if namespace != math.MaxUint64 { |
| 681 | ns = namespace |
| 682 | } |
| 683 | if err := parseTypeOrSchema(item, it, ns); err != nil { |
| 684 | return nil, err |
| 685 | } |
| 686 | |
| 687 | case itemLeftSquare: |
| 688 | // We expect a namespace. |
| 689 | ns, err := parseNamespace(it) |
| 690 | if err != nil { |
| 691 | return nil, errors.Wrapf(err, "While parsing namespace:") |
| 692 | } |
| 693 | if namespace != math.MaxUint64 { |
| 694 | // Use the passed namespace, if we don't want to preserve the namespace. |
| 695 | ns = namespace |
no test coverage detected