| 124 | } |
| 125 | |
| 126 | func (obj *ForFunc) replaceSubGraph(subgraphInput interfaces.Func) error { |
| 127 | // delete the old subgraph |
| 128 | if err := obj.init.Txn.Reverse(); err != nil { |
| 129 | return errwrap.Wrapf(err, "could not Reverse") |
| 130 | } |
| 131 | |
| 132 | obj.ClearIterBody(obj.lastInputListLength) // XXX: pass in size? |
| 133 | |
| 134 | for i := 0; i < obj.lastInputListLength; i++ { |
| 135 | argName := "forInputList" |
| 136 | |
| 137 | inputElemFunc := SimpleFnToDirectFunc( |
| 138 | "forInputElem["+strconv.Itoa(i)+"]", |
| 139 | &types.FuncValue{ |
| 140 | V: func(_ context.Context, args []types.Value) (types.Value, error) { |
| 141 | if len(args) != 1 { |
| 142 | return nil, fmt.Errorf("inputElemFunc: expected a single argument") |
| 143 | } |
| 144 | arg := args[0] |
| 145 | |
| 146 | list, ok := arg.(*types.ListValue) |
| 147 | if !ok { |
| 148 | return nil, fmt.Errorf("inputElemFunc: expected a ListValue argument") |
| 149 | } |
| 150 | |
| 151 | return list.List()[i], nil |
| 152 | }, |
| 153 | T: types.NewType(fmt.Sprintf("func(%s %s) %s", argName, obj.listType(), obj.ValueType)), |
| 154 | }, |
| 155 | ) |
| 156 | obj.init.Txn.AddVertex(inputElemFunc) |
| 157 | |
| 158 | obj.init.Txn.AddEdge(subgraphInput, inputElemFunc, &interfaces.FuncEdge{ |
| 159 | Args: []string{argName}, |
| 160 | }) |
| 161 | |
| 162 | if err := obj.AppendToIterBody(obj.init.Txn, i, inputElemFunc); err != nil { |
| 163 | return errwrap.Wrapf(err, "could not call AppendToIterBody()") |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return obj.init.Txn.Commit() |
| 168 | } |
| 169 | |
| 170 | func (obj *ForFunc) listType() *types.Type { |
| 171 | return types.NewType(fmt.Sprintf("[]%s", obj.ValueType)) |