func Parse(input string) (sg *query.SubGraph, rerr error) { l := lex.NewLexer(input) go run(l) sg = nil for item := range l.Items { if item.Typ == itemText { continue } if item.Typ == itemOpType { if item.Val == "mutation" { return nil, errors.New("Mutations not supported") } } if item.Typ == itemLeftCurl { if sg == nil { sg, rerr = getRoot(l) if rerr != nil { x.Err(glog, rerr).Error("While retrieving subgraph root") return nil, rerr } } else { if err := godeep(l, sg); err != nil { return sg, err } } } } return sg, nil }
func TestNewLexerMutation(t *testing.T) { input := ` mutation { set { What is <this> . Why is this #!!? How is this? } delete { Why is this } } query { me(xid: rick) { _city } }` l := lex.NewLexer(input) go run(l) for item := range l.Items { if item.Typ == lex.ItemError { t.Error(item.String()) } t.Log(item.String()) } }
func Parse(line string) (rnq NQuad, rerr error) { l := lex.NewLexer(line) go run(l) var oval string var vend bool for item := range l.Items { if item.Typ == itemSubject { rnq.Subject = stripBracketsIfPresent(item.Val) } if item.Typ == itemPredicate { rnq.Predicate = stripBracketsIfPresent(item.Val) } if item.Typ == itemObject { rnq.ObjectId = stripBracketsIfPresent(item.Val) } if item.Typ == itemLiteral { oval = item.Val } if item.Typ == itemLanguage { rnq.Language = item.Val } if item.Typ == itemObjectType { // TODO: Strictly parse common types like integers, floats etc. if len(oval) == 0 { glog.Fatalf( "itemObject should be emitted before itemObjectType. Input: %q", line) } oval += "@@" + stripBracketsIfPresent(item.Val) } if item.Typ == lex.ItemError { return rnq, fmt.Errorf(item.Val) } if item.Typ == itemValidEnd { vend = true } if item.Typ == itemLabel { rnq.Label = stripBracketsIfPresent(item.Val) } } if !vend { return rnq, fmt.Errorf("Invalid end of input") } if len(oval) > 0 { rnq.ObjectValue = oval } if len(rnq.Subject) == 0 || len(rnq.Predicate) == 0 { return rnq, fmt.Errorf("Empty required fields in NQuad") } if len(rnq.ObjectId) == 0 && rnq.ObjectValue == nil { return rnq, fmt.Errorf("No Object in NQuad") } return rnq, nil }
func TestNewLexer(t *testing.T) { input := ` mutation { me( id: 10, xid: rick ) { name0 # my name _city, # 0what would fail lex. profilePic(width: 100, height: 100) friends { name } } }` l := lex.NewLexer(input) go run(l) for item := range l.Items { fmt.Println(item.String()) } }
func TestAbruptMutation(t *testing.T) { input := ` mutation { set { What is <this> . Why is this #!!? How is this? }` l := lex.NewLexer(input) go run(l) var typ lex.ItemType for item := range l.Items { t.Log(item.String()) typ = item.Typ } if typ != lex.ItemError { t.Error("This should fail.") } }
func TestNewLexer(t *testing.T) { input := ` query { me( id: 10, xid: rick ) { name0 # my name _city, # 0what would fail lex. profilePic(width: 100, height: 100) friends { name } } }` l := lex.NewLexer(input) go run(l) for item := range l.Items { if item.Typ == lex.ItemError { t.Error(item.String()) } t.Log(item.String()) } }
func Parse(input string) (gq *GraphQuery, mu *Mutation, rerr error) { l := lex.NewLexer(input) go run(l) mu = nil gq = nil for item := range l.Items { if item.Typ == itemText { continue } if item.Typ == itemOpType { if item.Val == "mutation" { if mu != nil { return nil, nil, errors.New("Only one mutation block allowed.") } mu, rerr = getMutation(l) if rerr != nil { return nil, nil, rerr } } } if item.Typ == itemLeftCurl { if gq == nil { gq, rerr = getRoot(l) if rerr != nil { x.Err(glog, rerr).Error("While retrieving subgraph root") return nil, nil, rerr } } else { if err := godeep(l, gq); err != nil { return nil, nil, err } } } } return gq, mu, nil }