getRoot gets the root graph query object after parsing the args.
(it *lex.ItemIterator)
| 2817 | |
| 2818 | // getRoot gets the root graph query object after parsing the args. |
| 2819 | func getRoot(it *lex.ItemIterator) (gq *GraphQuery, rerr error) { |
| 2820 | gq = &GraphQuery{ |
| 2821 | Args: make(map[string]string), |
| 2822 | } |
| 2823 | if !it.Next() { |
| 2824 | return nil, it.Errorf("Invalid query") |
| 2825 | } |
| 2826 | item := it.Item() |
| 2827 | if item.Typ != itemName { |
| 2828 | return nil, item.Errorf("Expected some name. Got: %v", item) |
| 2829 | } |
| 2830 | |
| 2831 | peekIt, err := it.Peek(1) |
| 2832 | if err != nil { |
| 2833 | return nil, it.Errorf("Invalid Query") |
| 2834 | } |
| 2835 | if peekIt[0].Typ == itemName && strings.ToLower(peekIt[0].Val) == "as" { |
| 2836 | gq.Var = item.Val |
| 2837 | it.Next() // Consume the "AS". |
| 2838 | it.Next() |
| 2839 | item = it.Item() |
| 2840 | } |
| 2841 | |
| 2842 | gq.Alias = item.Val |
| 2843 | if !it.Next() { |
| 2844 | return nil, item.Errorf("Invalid query") |
| 2845 | } |
| 2846 | item = it.Item() |
| 2847 | if item.Typ != itemLeftRound { |
| 2848 | return nil, item.Errorf("Expected Left round brackets. Got: %v", item) |
| 2849 | } |
| 2850 | |
| 2851 | expectArg := true |
| 2852 | order := make(map[string]bool) |
| 2853 | // Parse in KV fashion. Depending on the value of key, decide the path. |
| 2854 | loop: |
| 2855 | for it.Next() { |
| 2856 | var key string |
| 2857 | // Get key. |
| 2858 | item := it.Item() |
| 2859 | switch item.Typ { |
| 2860 | case itemName: |
| 2861 | if !expectArg { |
| 2862 | return nil, item.Errorf("Not expecting argument. Got: %v", item) |
| 2863 | } |
| 2864 | key = item.Val |
| 2865 | expectArg = false |
| 2866 | case itemRightRound: |
| 2867 | if isEmpty(gq) { |
| 2868 | // Used to do aggregation at root which would be fetched in another block. |
| 2869 | gq.IsEmpty = true |
| 2870 | } |
| 2871 | break loop |
| 2872 | case itemComma: |
| 2873 | if expectArg { |
| 2874 | return nil, item.Errorf("Expected Argument but got comma.") |
| 2875 | } |
| 2876 | expectArg = true |
no test coverage detected