Ejemplo n.º 1
0
func buildFor(f *algebra.UpdateFor, val value.Value, arrays []value.Value, context *Context) ([]value.Value, error) {
	n := -1
	for _, a := range arrays {
		act := a.Actual()
		switch act := act.(type) {
		case []interface{}:
			if n < 0 || len(act) < n {
				n = len(act)
			}
		}
	}

	if n < 0 {
		return nil, nil
	}

	rv := make([]value.Value, n)
	for i, _ := range rv {
		rv[i] = value.NewScopeValue(make(map[string]interface{}, len(f.Bindings())), val)
		for j, b := range f.Bindings() {
			v, ok := arrays[j].Index(i)
			if ok {
				rv[i].SetField(b.Variable(), v)
			}
		}
	}

	return rv, nil
}
Ejemplo n.º 2
0
func arraysFor(f *algebra.UpdateFor, val value.Value, context *Context) ([]value.Value, error) {
	var err error
	arrays := make([]value.Value, len(f.Bindings()))
	for i, b := range f.Bindings() {
		arrays[i], err = b.Expression().Evaluate(val, context)
		if err != nil {
			return nil, err
		}

		if b.Descend() {
			buffer := make([]interface{}, 0, 256)
			arrays[i] = value.NewValue(arrays[i].Descendants(buffer))
		}
	}

	return arrays, nil
}