Exemplo n.º 1
0
func setPath(t *algebra.SetTerm, clone, item value.AnnotatedValue, context *Context) (value.AnnotatedValue, error) {
	if t.UpdateFor() != nil {
		return setFor(t, clone, item, context)
	}

	v, err := t.Value().Evaluate(item, context)
	if err != nil {
		return nil, err
	}

	if v.Type() == value.MISSING {
	}

	if t.Path() != nil {
		t.Path().Set(clone, v, context)
		return clone, nil
	} else {
		av := value.NewAnnotatedValue(v)
		av.SetAttachments(clone.Attachments())
		return av, nil
	}
}
Exemplo n.º 2
0
func setFor(t *algebra.SetTerm, clone, item value.AnnotatedValue, context *Context) (value.AnnotatedValue, error) {
	carrays, err := arraysFor(t.UpdateFor(), clone, context)
	if err != nil {
		return nil, err
	}

	cvals, err := buildFor(t.UpdateFor(), clone, carrays, context)
	if err != nil {
		return nil, err
	}

	iarrays, err := arraysFor(t.UpdateFor(), item, context)
	if err != nil {
		return nil, err
	}

	ivals, err := buildFor(t.UpdateFor(), item, iarrays, context)
	if err != nil {
		return nil, err
	}

	// Clone may have been shortened by previous SET term
	n := len(ivals)
	if len(cvals) < n {
		n = len(cvals)
	}

	when := t.UpdateFor().When()
	for i := 0; i < n; i++ {
		v, err := t.Value().Evaluate(ivals[i], context)
		if err != nil {
			return nil, err
		}

		if when != nil {
			w, err := when.Evaluate(ivals[i], context)
			if err != nil {
				return nil, err
			}

			if !w.Truth() {
				continue
			}
		}

		t.Path().Set(cvals[i], v, context)
	}

	// Set array elements
	f := t.UpdateFor()
	for a, b := range f.Bindings() {
		switch ca := carrays[a].Actual().(type) {
		case []interface{}:
			for i := 0; i < n; i++ {
				ca[i], _ = cvals[i].Field(b.Variable())
			}
		}
	}

	return clone, nil
}