ParseMutation parses a block into a mutation. Returns an object with a mutation or an upsert block with mutation, otherwise returns nil with an error.
(mutation string)
| 13 | // ParseMutation parses a block into a mutation. Returns an object with a mutation or |
| 14 | // an upsert block with mutation, otherwise returns nil with an error. |
| 15 | func ParseMutation(mutation string) (req *api.Request, err error) { |
| 16 | var lexer lex.Lexer |
| 17 | lexer.Reset(mutation) |
| 18 | lexer.Run(lexTopLevel) |
| 19 | if err := lexer.ValidateResult(); err != nil { |
| 20 | return nil, err |
| 21 | } |
| 22 | |
| 23 | it := lexer.NewIterator() |
| 24 | if !it.Next() { |
| 25 | return nil, it.Errorf("Invalid mutation") |
| 26 | } |
| 27 | |
| 28 | item := it.Item() |
| 29 | switch item.Typ { |
| 30 | case itemUpsertBlock: |
| 31 | if req, err = parseUpsertBlock(it); err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | case itemLeftCurl: |
| 35 | mu, err := parseMutationBlock(it) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | req = &api.Request{Mutations: []*api.Mutation{mu}} |
| 40 | default: |
| 41 | return nil, it.Errorf("Unexpected token: [%s]", item.Val) |
| 42 | } |
| 43 | |
| 44 | // mutations must be enclosed in a single block. |
| 45 | if it.Next() && it.Item().Typ != lex.ItemEOF { |
| 46 | return nil, it.Errorf("Unexpected %s after the end of the block", it.Item().Val) |
| 47 | } |
| 48 | |
| 49 | return req, nil |
| 50 | } |
| 51 | |
| 52 | // parseUpsertBlock parses the upsert block |
| 53 | func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) { |