Skip to content
Snippets Groups Projects
Commit db048fde authored by Manish R Jain's avatar Manish R Jain
Browse files

Move argument parsing in a separate function

parent 019cb0f2
Branches
No related tags found
No related merge requests found
...@@ -33,6 +33,7 @@ type GraphQuery struct { ...@@ -33,6 +33,7 @@ type GraphQuery struct {
UID uint64 UID uint64
XID string XID string
Attr string Attr string
First int
Children []*GraphQuery Children []*GraphQuery
} }
...@@ -41,6 +42,11 @@ type Mutation struct { ...@@ -41,6 +42,11 @@ type Mutation struct {
Del string Del string
} }
type pair struct {
Key string
Val string
}
func run(l *lex.Lexer) { func run(l *lex.Lexer) {
for state := lexText; state != nil; { for state := lexText; state != nil; {
state = state(l) state = state(l)
...@@ -142,6 +148,35 @@ func parseMutationOp(l *lex.Lexer, op string, mu *Mutation) error { ...@@ -142,6 +148,35 @@ func parseMutationOp(l *lex.Lexer, op string, mu *Mutation) error {
return errors.New("Invalid mutation formatting.") return errors.New("Invalid mutation formatting.")
} }
func parseArguments(l *lex.Lexer) (result []pair, rerr error) {
for {
var p pair
// Get key.
item := <-l.Items
if item.Typ == itemArgName {
p.Key = item.Val
} else if item.Typ == itemRightRound {
break
} else {
return result, fmt.Errorf("Expecting argument name. Got: %v", item)
}
// Get value.
item = <-l.Items
if item.Typ == itemArgVal {
p.Val = item.Val
} else {
return result, fmt.Errorf("Expecting argument value. Got: %v", item)
}
result = append(result, p)
}
return result, nil
}
func getRoot(l *lex.Lexer) (gq *GraphQuery, rerr error) { func getRoot(l *lex.Lexer) (gq *GraphQuery, rerr error) {
item := <-l.Items item := <-l.Items
if item.Typ != itemName { if item.Typ != itemName {
...@@ -155,40 +190,24 @@ func getRoot(l *lex.Lexer) (gq *GraphQuery, rerr error) { ...@@ -155,40 +190,24 @@ func getRoot(l *lex.Lexer) (gq *GraphQuery, rerr error) {
var uid uint64 var uid uint64
var xid string var xid string
for { args, err := parseArguments(l)
var key, val string if err != nil {
// Get key or close bracket return nil, err
item = <-l.Items }
if item.Typ == itemArgName { for _, p := range args {
key = item.Val if p.Key == "_uid_" {
} else if item.Typ == itemRightRound { uid, rerr = strconv.ParseUint(p.Val, 0, 64)
break
} else {
return nil, fmt.Errorf("Expecting argument name. Got: %v", item)
}
// Get corresponding value.
item = <-l.Items
if item.Typ == itemArgVal {
val = item.Val
} else {
return nil, fmt.Errorf("Expecting argument va Got: %v", item)
}
if key == "_uid_" {
uid, rerr = strconv.ParseUint(val, 0, 64)
if rerr != nil { if rerr != nil {
return nil, rerr return nil, rerr
} }
} else if key == "_xid_" { } else if p.Key == "_xid_" {
xid = val xid = p.Val
} else { } else {
return nil, fmt.Errorf("Expecting _uid_ or _xid_. Got: %v", item) return nil, fmt.Errorf("Expecting _uid_ or _xid_. Got: %+v", p)
} }
} }
if item.Typ != itemRightRound {
return nil, fmt.Errorf("Unexpected token. Got: %v", item)
}
gq = new(GraphQuery) gq = new(GraphQuery)
gq.UID = uid gq.UID = uid
gq.XID = xid gq.XID = xid
...@@ -220,7 +239,21 @@ func godeep(l *lex.Lexer, gq *GraphQuery) error { ...@@ -220,7 +239,21 @@ func godeep(l *lex.Lexer, gq *GraphQuery) error {
} else if item.Typ == itemLeftRound { } else if item.Typ == itemLeftRound {
// absorb all these, we don't use them right now. // absorb all these, we don't use them right now.
/*
for {
var key, val string
item = <-l.Items
if item.Typ == itemArgName {
key = item.Val
} else if item.Typ == itemRightRound {
break
} else {
return nil, fmt.Errorf("Expecting argument name. Got: %v", item)
}
}
*/
for ti := range l.Items { for ti := range l.Items {
fmt.Println(ti.String())
if ti.Typ == itemRightRound || ti.Typ == lex.ItemEOF { if ti.Typ == itemRightRound || ti.Typ == lex.ItemEOF {
return nil return nil
} }
......
...@@ -77,6 +77,7 @@ func TestParse(t *testing.T) { ...@@ -77,6 +77,7 @@ func TestParse(t *testing.T) {
func TestParseXid(t *testing.T) { func TestParseXid(t *testing.T) {
// logrus.SetLevel(logrus.DebugLevel) // logrus.SetLevel(logrus.DebugLevel)
// TODO: Why does the query not have _xid_ attribute?
query := ` query := `
query { query {
user(_uid_: 0x11) { user(_uid_: 0x11) {
...@@ -100,6 +101,35 @@ func TestParseXid(t *testing.T) { ...@@ -100,6 +101,35 @@ func TestParseXid(t *testing.T) {
} }
} }
/*
func TestParseFirst(t *testing.T) {
// logrus.SetLevel(logrus.DebugLevel)
query := `
query {
user(_xid_: m.abcd) {
type.object.name
friends (first: 10) {
}
}
}`
gq, _, err := Parse(query)
if err != nil {
t.Error(err)
return
}
if gq == nil {
t.Error("subgraph is nil")
return
}
if len(gq.Children) != 1 {
t.Errorf("Expected 1 children. Got: %v", len(gq.Children))
}
if err := checkAttr(gq.Children[0], "type.object.name"); err != nil {
t.Error(err)
}
}
*/
func TestParse_error2(t *testing.T) { func TestParse_error2(t *testing.T) {
query := ` query := `
query { query {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment