apply

package
v1.13.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2018 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MergeStrategy is the strategy to merge the local and remote values
	MergeStrategy = "merge"

	// RetainKeysStrategy is the strategy to merge the local and remote values, but drop any fields not defined locally
	RetainKeysStrategy = "retainKeys"

	// ReplaceStrategy is the strategy to replace the remote value with the local value
	ReplaceStrategy = "replace"
)

Variables

This section is empty.

Functions

func IsAdd

func IsAdd(e Element) bool

IsAdd returns true if the field represented by e should have the local value directly added to the merged object instead of merging the recorded, local and remote values

func IsDrop

func IsDrop(e Element) bool

IsDrop returns true if the field represented by e should be dropped from the merged object

Types

type CombinedMapSlice

type CombinedMapSlice struct {
	Items []*ListItem
}

CombinedMapSlice is a slice of maps or types with merge keys

func (*CombinedMapSlice) UpsertLocal

func (s *CombinedMapSlice) UpsertLocal(key MergeKeys, l interface{}) error

UpsertLocal adds l to the slice. If there is already a value of l sharing l's merge key in the slice for either the recorded or remote, set l the local value Otherwise append a new item to the list with the local value.

func (*CombinedMapSlice) UpsertRecorded

func (s *CombinedMapSlice) UpsertRecorded(key MergeKeys, l interface{}) error

UpsertRecorded adds l to the slice. If there is already a value of l sharing l's merge key in the slice for either the local or remote, set l the recorded value Otherwise append a new item to the list with the recorded value.

func (*CombinedMapSlice) UpsertRemote

func (s *CombinedMapSlice) UpsertRemote(key MergeKeys, l interface{}) error

UpsertRemote adds l to the slice. If there is already a value of l sharing l's merge key in the slice for either the recorded or local, set l the remote value Otherwise append a new item to the list with the remote value.

type CombinedPrimitiveSlice

type CombinedPrimitiveSlice struct {
	Items []*PrimitiveListItem
}

CombinedPrimitiveSlice implements a slice of primitives

func (*CombinedPrimitiveSlice) UpsertLocal

func (s *CombinedPrimitiveSlice) UpsertLocal(l interface{})

UpsertLocal adds l to the slice. If there is already a value of l in the slice for either the recorded or remote, set on that value as the local value Otherwise append a new item to the list with the local value.

func (*CombinedPrimitiveSlice) UpsertRecorded

func (s *CombinedPrimitiveSlice) UpsertRecorded(l interface{})

UpsertRecorded adds l to the slice. If there is already a value of l in the slice for either the local or remote, set on that value as the recorded value Otherwise append a new item to the list with the recorded value.

func (*CombinedPrimitiveSlice) UpsertRemote

func (s *CombinedPrimitiveSlice) UpsertRemote(l interface{})

UpsertRemote adds l to the slice. If there is already a value of l in the slice for either the local or recorded, set on that value as the remote value Otherwise append a new item to the list with the remote value.

type ConflictDetector added in v1.11.1

type ConflictDetector interface {
	HasConflict() error
}

ConflictDetector defines the capability to detect conflict. An element can examine remote/recorded value to detect conflict.

type ConflictError added in v1.11.1

type ConflictError struct {
	// contains filtered or unexported fields
}

ConflictError represents a conflict error occurred during the merge operation.

func NewConflictError added in v1.11.1

func NewConflictError(e PrimitiveElement) *ConflictError

NewConflictError returns a ConflictError with detailed conflict information in element

func (*ConflictError) Error added in v1.11.1

func (c *ConflictError) Error() string

Error implements error

type Element

type Element interface {
	// FieldMeta specifies which merge strategy to use for this element
	FieldMeta

	// Merge merges the recorded, local and remote values in the element using the Strategy
	// provided as an argument.  Calls the type specific method on the Strategy - following the
	// "Accept" method from the "Visitor" pattern.
	// e.g. Merge on a ListElement will call Strategy.MergeList(self)
	// Returns the Result of the merged elements
	Merge(Strategy) (Result, error)

	// HasRecorded returns true if the field was explicitly
	// present in the recorded source.  This is to differentiate between
	// undefined and set to null
	HasRecorded() bool

	// GetRecorded returns the field value from the recorded source of the object
	GetRecorded() interface{}

	// HasLocal returns true if the field was explicitly
	// present in the local source.  This is to differentiate between
	// undefined and set to null
	HasLocal() bool

	// GetLocal returns the field value from the local source of the object
	GetLocal() interface{}

	// HasRemote returns true if the field was explicitly
	// present in the remote source.  This is to differentiate between
	// undefined and set to null
	HasRemote() bool

	// GetRemote returns the field value from the remote source of the object
	GetRemote() interface{}
}

Element contains the record, local, and remote value for a field in an object and metadata about the field read from openapi. Calling Merge on an element will apply the passed in strategy to Element - e.g. either replacing the whole element with the local copy or merging each of the recorded, local and remote fields of the element.

type EmptyElement

type EmptyElement struct {
	// FieldMetaImpl contains metadata about the field from openapi
	FieldMetaImpl
}

EmptyElement is a placeholder for when no value is set for a field so its type is unknown

func (EmptyElement) GetLocal

func (e EmptyElement) GetLocal() interface{}

GetLocal implements Element.GetLocal

func (EmptyElement) GetRecorded

func (e EmptyElement) GetRecorded() interface{}

GetRecorded implements Element.GetRecorded

func (EmptyElement) GetRemote

func (e EmptyElement) GetRemote() interface{}

GetRemote implements Element.GetRemote

func (EmptyElement) HasLocal

func (e EmptyElement) HasLocal() bool

HasLocal implements Element.HasLocal

func (EmptyElement) HasRecorded

func (e EmptyElement) HasRecorded() bool

HasRecorded implements Element.HasRecorded

func (EmptyElement) HasRemote

func (e EmptyElement) HasRemote() bool

HasRemote implements Element.IsAdd

func (EmptyElement) IsAdd

func (e EmptyElement) IsAdd() bool

IsAdd implements Element.IsAdd

func (EmptyElement) IsDelete

func (e EmptyElement) IsDelete() bool

IsDelete implements Element.IsDelete

func (EmptyElement) Merge

func (e EmptyElement) Merge(v Strategy) (Result, error)

Merge implements Element.Merge

type FieldMeta

type FieldMeta interface {
	// GetFieldMergeType returns the type of merge strategy to use for this field
	// maybe "merge", "replace" or "retainkeys"
	// TODO: There maybe multiple strategies, so this may need to be a slice, map, or struct
	// Address this in a follow up in the PR to introduce retainkeys strategy
	GetFieldMergeType() string

	// GetFieldMergeKeys returns the merge key to use when the MergeType is "merge" and underlying type is a list
	GetFieldMergeKeys() MergeKeys

	// GetFieldType returns the openapi field type - e.g. primitive, array, map, type, reference
	GetFieldType() string
}

FieldMeta defines the strategy used to apply a Patch for an element

type FieldMetaImpl

type FieldMetaImpl struct {
	// MergeType is the type of merge strategy to use for this field
	// maybe "merge", "replace" or "retainkeys"
	MergeType string

	// MergeKeys are the merge keys to use when the MergeType is "merge" and underlying type is a list
	MergeKeys MergeKeys

	// Type is the openapi type of the field - "list", "primitive", "map"
	Type string

	// Name contains name of the field
	Name string
}

FieldMetaImpl implements FieldMeta

func (FieldMetaImpl) GetFieldMergeKeys

func (s FieldMetaImpl) GetFieldMergeKeys() MergeKeys

GetFieldMergeKeys implements FieldMeta.GetFieldMergeKeys

func (FieldMetaImpl) GetFieldMergeType

func (s FieldMetaImpl) GetFieldMergeType() string

GetFieldMergeType implements FieldMeta.GetFieldMergeType

func (FieldMetaImpl) GetFieldType

func (s FieldMetaImpl) GetFieldType() string

GetFieldType implements FieldMeta.GetFieldType

type HasElementData

type HasElementData struct {
	// contains filtered or unexported fields
}

HasElementData contains whether a field was set in the recorded, local and remote sources

func (HasElementData) HasLocal

func (e HasElementData) HasLocal() bool

HasLocal implements Element.HasLocal

func (HasElementData) HasRecorded

func (e HasElementData) HasRecorded() bool

HasRecorded implements Element.HasRecorded

func (HasElementData) HasRemote

func (e HasElementData) HasRemote() bool

HasRemote implements Element.HasRemote

type ListElement

type ListElement struct {
	// FieldMetaImpl contains metadata about the field from openapi
	FieldMetaImpl

	ListElementData

	// Values contains the combined recorded-local-remote value of each item in the list
	// Present for lists that can be merged only.  Contains the items
	// from each of the 3 lists merged into single Elements using
	// the merge-key.
	Values []Element
}

ListElement contains the recorded, local and remote values for a field of type list

func (ListElement) HasConflict added in v1.11.1

func (e ListElement) HasConflict() error

HasConflict returns ConflictError if fields in recorded and remote of ListElement conflict

func (ListElement) Merge

func (e ListElement) Merge(v Strategy) (Result, error)

Merge implements Element.Merge

type ListElementData

type ListElementData struct {
	RawElementData
}

ListElementData contains the recorded, local and remote data for a list

func (ListElementData) GetLocalList

func (e ListElementData) GetLocalList() []interface{}

GetLocalList returns the Local value as a list

func (ListElementData) GetRecordedList

func (e ListElementData) GetRecordedList() []interface{}

GetRecordedList returns the Recorded value as a list

func (ListElementData) GetRemoteList

func (e ListElementData) GetRemoteList() []interface{}

GetRemoteList returns the Remote value as a list

type ListItem

type ListItem struct {
	// KeyValue is the merge key value of the item
	KeyValue MergeKeyValue

	// RawElementData contains the field values
	RawElementData
}

ListItem represents a single value in a slice of maps or types

type MapElement

type MapElement struct {
	// FieldMetaImpl contains metadata about the field from openapi
	FieldMetaImpl

	// MapElementData contains the value a field was set to
	MapElementData

	// Values contains the combined recorded-local-remote value of each item in the map
	// Values contains the values in mapElement.  Element must contain
	// a Name matching its key in Values
	Values map[string]Element
}

MapElement contains the recorded, local and remote values for a field of type map

func (MapElement) GetValues

func (e MapElement) GetValues() map[string]Element

GetValues implements Element.GetValues

func (MapElement) HasConflict added in v1.11.1

func (e MapElement) HasConflict() error

HasConflict returns ConflictError if some elements in map conflict.

func (MapElement) Merge

func (e MapElement) Merge(v Strategy) (Result, error)

Merge implements Element.Merge

type MapElementData

type MapElementData struct {
	RawElementData
}

MapElementData contains the recorded, local and remote data for a map or type

func (MapElementData) GetLocalMap

func (e MapElementData) GetLocalMap() map[string]interface{}

GetLocalMap returns the Local value as a map

func (MapElementData) GetRecordedMap

func (e MapElementData) GetRecordedMap() map[string]interface{}

GetRecordedMap returns the Recorded value as a map

func (MapElementData) GetRemoteMap

func (e MapElementData) GetRemoteMap() map[string]interface{}

GetRemoteMap returns the Remote value as a map

type MergeKeyValue

type MergeKeyValue map[string]string

MergeKeyValue records the value of the mergekey for an item in a list

func (MergeKeyValue) Equal

func (v MergeKeyValue) Equal(o MergeKeyValue) bool

Equal returns true if the MergeKeyValues share the same value, representing the same item in a list

type MergeKeys

type MergeKeys []string

MergeKeys is the set of fields on an object that uniquely identify and is used when merging lists to identify the "same" object independent of the ordering of the objects

func (MergeKeys) GetMergeKeyValue

func (mk MergeKeys) GetMergeKeyValue(i interface{}) (MergeKeyValue, error)

GetMergeKeyValue parses the MergeKeyValue from an item in a list

type Operation

type Operation int

Operation records whether a field should be set or dropped

const (
	// ERROR is an error during merge
	ERROR Operation = iota
	// SET sets the field on an object
	SET
	// DROP drops the field from an object
	DROP
)

type PrimitiveElement

type PrimitiveElement struct {
	// FieldMetaImpl contains metadata about the field from openapi
	FieldMetaImpl

	// RawElementData contains the values the field was set to
	RawElementData
}

PrimitiveElement contains the recorded, local and remote values for a field of type primitive

func (PrimitiveElement) HasConflict added in v1.11.1

func (e PrimitiveElement) HasConflict() error

HasConflict returns ConflictError if primitive element has conflict field. Conflicts happen when either of the following conditions: 1. A field is specified in both recorded and remote values, but does not match. 2. A field is specified in recorded values, but missing in remote values.

func (PrimitiveElement) Merge

func (e PrimitiveElement) Merge(v Strategy) (Result, error)

Merge implements Element.Merge

type PrimitiveListItem

type PrimitiveListItem struct {
	// Value is the value of the primitive, should match recorded, local and remote
	Value interface{}

	RawElementData
}

PrimitiveListItem represents a single value in a slice of primitives

type RawElementData

type RawElementData struct {
	HasElementData
	// contains filtered or unexported fields
}

RawElementData contains the raw recorded, local and remote data and metadata about whethere or not each was set

func NewRawElementData

func NewRawElementData(recorded, local, remote interface{}) RawElementData

NewRawElementData returns a new RawElementData, setting IsSet to true for non-nil values, and leaving IsSet false for nil values. Note: use this only when you want a nil-value to be considered "unspecified" (ignore) and not "unset" (deleted).

func (RawElementData) GetLocal

func (b RawElementData) GetLocal() interface{}

GetLocal implements Element.GetLocal

func (RawElementData) GetRecorded

func (b RawElementData) GetRecorded() interface{}

GetRecorded implements Element.GetRecorded

func (RawElementData) GetRemote

func (b RawElementData) GetRemote() interface{}

GetRemote implements Element.GetRemote

func (*RawElementData) SetLocal

func (b *RawElementData) SetLocal(value interface{})

SetLocal sets the local value

func (*RawElementData) SetRecorded

func (b *RawElementData) SetRecorded(value interface{})

SetRecorded sets the recorded value

func (*RawElementData) SetRemote

func (b *RawElementData) SetRemote(value interface{})

SetRemote sets the remote value

type Result

type Result struct {
	// Operation is the operation that should be performed for the merged field
	Operation Operation
	// MergedResult is the new merged value
	MergedResult interface{}
}

Result is the result of merging fields

type Strategy

type Strategy interface {
	// MergeList is invoked by ListElements when Merge is called
	MergeList(ListElement) (Result, error)

	// MergeMap is invoked by MapElements when Merge is called
	MergeMap(MapElement) (Result, error)

	// MergeType is invoked by TypeElements when Merge is called
	MergeType(TypeElement) (Result, error)

	// MergePrimitive is invoked by PrimitiveElements when Merge is called
	MergePrimitive(PrimitiveElement) (Result, error)

	// MergeEmpty is invoked by EmptyElements when Merge is called
	MergeEmpty(EmptyElement) (Result, error)
}

Strategy implements a strategy for merging recorded, local and remote values contained in an element and returns the merged result. Follows the visitor pattern

type TypeElement

type TypeElement struct {
	// FieldMetaImpl contains metadata about the field from openapi
	FieldMetaImpl

	MapElementData

	// Values contains the combined recorded-local-remote value of each field in the type
	// Values contains the values in mapElement.  Element must contain
	// a Name matching its key in Values
	Values map[string]Element
}

TypeElement contains the recorded, local and remote values for a field that is a complex type

func (TypeElement) GetValues

func (e TypeElement) GetValues() map[string]Element

GetValues implements Element.GetValues

func (TypeElement) HasConflict added in v1.11.1

func (e TypeElement) HasConflict() error

HasConflict returns ConflictError if some elements in type conflict.

func (TypeElement) Merge

func (e TypeElement) Merge(v Strategy) (Result, error)

Merge implements Element.Merge

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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