Example #1
0
/*
This method takes in an object, a name and a value
and returns a new object that contains the name /
attribute pair. If the type of input is missing
then return a missing value, and if not an object
return a null value.
If the key is found, an error is thrown
*/
func (this *ObjectAdd) Apply(context Context, first, second, third value.Value) (value.Value, error) {

	// First must be an object, or we're out
	if first.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() != value.OBJECT {
		return value.NULL_VALUE, nil
	}

	// second must be a non empty string
	if second.Type() != value.STRING || second.Actual().(string) == "" {
		return first, nil
	}

	field := second.Actual().(string)

	// we don't overwrite
	_, exists := first.Field(field)
	if exists {
		return value.NULL_VALUE, nil
	}

	// SetField will remove if the attribute is missing, but we don't
	// overwrite anyway, so we might just skip now
	if third.Type() != value.MISSING {
		rv := first.CopyForUpdate()
		rv.SetField(field, third)
		return rv, nil
	}
	return first, nil
}
Example #2
0
/*
This method takes in an object and a name and returns
an object with the name / attribute pair removed.
If the type of input is missing then return a missing value, and
if not an object return a null value.
*/
func (this *ObjectRemove) Apply(context Context, first, second value.Value) (value.Value, error) {
	// First must be an object, or we're out
	if first.Type() == value.MISSING {
		return value.MISSING_VALUE, nil
	} else if first.Type() != value.OBJECT {
		return value.NULL_VALUE, nil
	}

	// second must be a non empty string
	if second.Type() != value.STRING || second.Actual().(string) == "" {
		return first, nil
	}

	field := second.Actual().(string)

	rv := first.CopyForUpdate()
	rv.UnsetField(field)
	return rv, nil
}