mutate

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2016 License: MIT Imports: 6 Imported by: 1

README

Mutate codecov Coverage Status Build Status Go Report Card

A mutation is a descriptor of operations on a source object to produce a desired result object. This package compares two objects and generates an object that describes what changed between the two input objects, and then can apply that mutation to the source object to produce the target object.

Mutators

Input objects are generic JSON (string key -> any value). To describe mutations, we reserve the $ character as a prefix to an operation.

Given this initial state:

{
  "myarr": [3, 1],
  "myarr2": [10, 11],
  "mystring": "hi",
  "myobject": {
    "mynestedstring": "hello"
  }
}

Add this mutation:

{
  "myarr": {"$push": [5]},
  "myarr2": {"$set": [1]},
  "mystring": "hello",
  "myobject": {
    "mynestedstring": {"$set": "kappa"}
  }
}

The result would be:

{
  "myarr": [3, 1, 5],
  "myarr2": [1],
  "mystring": "hello",
  "myobject": {
    "mynestedstring": "kappa"
  }
}

The following are the general rules of mutations:

  • A pure value for a non-object field implies a $set operation.
  • Whole object trees can never be set by pure values, a $set operation is required.
  • If a value object has one or more $ prefixed fields, it is considered a mutation object and will be parsed as such.

The following are possible mutations, and will be evaluated in this order:

  • $set: completely overwrite the field value
  • $push: add to an end of an array. If the field is not currently an array, creates a new empty array and pushes the values.
  • $pull: remove from an array. Argument is an array of indexes to delete, pre-mutation (for example, removing 1 and 2 from [0, 5, 2, 3, 1, 9] would be $pull: [4, 2]).
  • $truncate: remove from an index in an array onwards.
  • $mutateIdx: mutate at indexes.
  • $unset: unset a field value.

Usage

You can call mutation := BuildMutation(oldObject, newObject) to produce a mutation from two input objects. You can then call result := ApplyMutationObject(oldObject, mutation), and result will be exactly equal to newObject.

Documentation

Index

Constants

This section is empty.

Variables

MutationImpl are implementations of the mutations named in MutationKeys

View Source
var MutationKeys = []string{
	"$set",
	"$push",
	"$pull",
	"$truncate",
	"$mutateIdx",
	"$unset",
}

MutationKeys are the string mutation keys used to represent mutations.

Functions

func ApplyMutation

func ApplyMutation(oldVal interface{}, mutation map[string]interface{}) (interface{}, error)

ApplyMutation applies a mutation object to a value Note: this changes the input

func ApplyMutationObject

func ApplyMutationObject(oldObj, mutations map[string]interface{}) (map[string]interface{}, error)

ApplyMutationObject applies all mutations in a mutation object hierarchy recursively

func BuildMutation

func BuildMutation(oldObj, newObj map[string]interface{}) map[string]interface{}

BuildMutation recursively builds a mutation from an old and new object.

Types

type IndexMutation

type IndexMutation struct {
}

IndexMutation is a MutationImplementation for $mutateIdx

func (*IndexMutation) Apply

func (*IndexMutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply the $mutateIdx mutation.

type Mutation

type Mutation struct {
	MutationType MutationType
}

Mutation is a wrapper class for an implementation type.

func ParseMutation

func ParseMutation(key string) *Mutation

ParseMutation returns a Mutation from a key like $set or nil if not found

func (*Mutation) Apply

func (m *Mutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply applies a single mutation, after being parsed with ParseMutation.

type MutationImplementation

type MutationImplementation interface {
	Apply(oldVal, arg interface{}) (interface{}, error)
}

MutationImplementation represents an implementation of a mutation type

type MutationType

type MutationType int

MutationType represents a type of mutator, i.e. $set

const (
	// MutateSet represents the set mutation
	MutateSet MutationType = iota
	// MutatePush represents the push mutation
	MutatePush
	// MutatePull represents the pull mutation
	MutatePull
	// MutateTruncate represents the truncate mutation
	MutateTruncate
	// MutateIdx represents the index mutation
	MutateIdx
	// MutateUnset represents the unset mutation
	MutateUnset
)

func (MutationType) String

func (i MutationType) String() string

type PullMutation

type PullMutation struct {
}

PullMutation is the implementation of the $pull mutation.

func (*PullMutation) Apply

func (*PullMutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply the $pull mutation.

type PushMutation

type PushMutation struct {
}

PushMutation is the implementation for the $push mutation.

func (*PushMutation) Apply

func (*PushMutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply the $push mutation.

type SetMutation

type SetMutation struct {
}

SetMutation is a MutationImplementation for $set

func (*SetMutation) Apply

func (*SetMutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply the $set mutation.

type TruncateMutation

type TruncateMutation struct {
}

TruncateMutation is the implementation for the $truncate mutation.

func (*TruncateMutation) Apply

func (*TruncateMutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply the $truncate mutation.

type UnsetMarker

type UnsetMarker struct {
}

UnsetMarker is a marker for unsetting a field returned from Apply

type UnsetMutation

type UnsetMutation struct {
}

UnsetMutation is the implementation of the $unset mutation.

func (*UnsetMutation) Apply

func (*UnsetMutation) Apply(oldVal, arg interface{}) (interface{}, error)

Apply the $unset mutation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL