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

Function ParseMutation

dql/parser_mutation.go:15–50  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
15func 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
53func parseUpsertBlock(it *lex.ItemIterator) (*api.Request, error) {

Callers 4

TestParseMutationErrorFunction · 0.85
TestParseMutationError2Function · 0.85
TestParseMutationFunction · 0.85

Calls 9

ResetMethod · 0.95
RunMethod · 0.95
ValidateResultMethod · 0.95
NewIteratorMethod · 0.95
parseUpsertBlockFunction · 0.85
parseMutationBlockFunction · 0.85
ItemMethod · 0.80
NextMethod · 0.45
ErrorfMethod · 0.45

Tested by 4

TestParseMutationErrorFunction · 0.68
TestParseMutationError2Function · 0.68
TestParseMutationFunction · 0.68