fillVars reads the value corresponding to a variable from the map mp and stores it inside SubGraph. This value is then later used for execution of the SubGraph.
(mp map[string]varValue)
| 1804 | // fillVars reads the value corresponding to a variable from the map mp and stores it inside |
| 1805 | // SubGraph. This value is then later used for execution of the SubGraph. |
| 1806 | func (sg *SubGraph) fillVars(mp map[string]varValue) error { |
| 1807 | if sg.Params.Alias == "shortest" { |
| 1808 | if err := sg.fillShortestPathVars(mp); err != nil { |
| 1809 | return err |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | var lists []*pb.List |
| 1814 | // Go through all the variables in NeedsVar and see if we have a value for them in the map. If |
| 1815 | // we do, then we store that value in the appropriate variable inside SubGraph. |
| 1816 | for _, v := range sg.Params.NeedsVar { |
| 1817 | l, ok := mp[v.Name] |
| 1818 | if !ok { |
| 1819 | continue |
| 1820 | } |
| 1821 | switch { |
| 1822 | case (v.Typ == dql.AnyVar || v.Typ == dql.ListVar) && l.strList != nil: |
| 1823 | // This is for the case when we use expand(val(x)) with a value variable. |
| 1824 | // We populate the list of values into ExpandPreds and use that for the expand query |
| 1825 | // later. |
| 1826 | // TODO: If we support value vars for list type then this needn't be true |
| 1827 | sg.ExpandPreds = l.strList |
| 1828 | |
| 1829 | case (v.Typ == dql.UidVar && sg.SrcFunc != nil && sg.SrcFunc.Name == "uid_in"): |
| 1830 | srcFuncArgs := sg.SrcFunc.Args[:0] |
| 1831 | |
| 1832 | for _, uid := range l.Uids.GetUids() { |
| 1833 | // We use base 10 here because the uid parser expects the uid to be in base 10. |
| 1834 | arg := dql.Arg{Value: strconv.FormatUint(uid, 10)} |
| 1835 | srcFuncArgs = append(srcFuncArgs, arg) |
| 1836 | } |
| 1837 | sg.SrcFunc.Args = srcFuncArgs |
| 1838 | |
| 1839 | case (v.Typ == dql.AnyVar || v.Typ == dql.UidVar) && l.Uids != nil: |
| 1840 | lists = append(lists, l.Uids) |
| 1841 | |
| 1842 | case (v.Typ == dql.AnyVar || v.Typ == dql.ValueVar): |
| 1843 | // This should happen only once. |
| 1844 | // TODO: This allows only one value var per subgraph, change it later |
| 1845 | sg.Params.UidToVal = l.Vals |
| 1846 | |
| 1847 | case (v.Typ == dql.AnyVar || v.Typ == dql.UidVar) && l.Vals.Len() != 0: |
| 1848 | // Derive the UID list from value var. |
| 1849 | uids := make([]uint64, 0, l.Vals.Len()) |
| 1850 | l.Vals.Iterate(func(uid uint64, _ types.Val) error { |
| 1851 | uids = append(uids, uid) |
| 1852 | return nil |
| 1853 | }) |
| 1854 | sort.Slice(uids, func(i, j int) bool { return uids[i] < uids[j] }) |
| 1855 | lists = append(lists, &pb.List{Uids: uids}) |
| 1856 | |
| 1857 | case l.Vals.Len() != 0 || l.Uids != nil: |
| 1858 | return errors.Errorf("Wrong variable type encountered for var(%v) %v.", v.Name, v.Typ) |
| 1859 | |
| 1860 | default: |
| 1861 | glog.V(3).Infof("Warning: reached default case in fillVars for var: %v", v.Name) |
| 1862 | } |
| 1863 | } |
no test coverage detected