Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Merge ¶
Merge the given source onto the given target following the options given. The target value must be a pointer. If opt is nil, defaults will be used. If an error occurs during the merge process the target will be unmodified. Merge will accept any two entities, as long as their types are the same. See Options and MergeFunc for further customization possibilities.
Types ¶
type MergeFunc ¶
A MergeFunc defines how two items are merged together. It should accept a reflect.Value representation of a target and source, and return the final merged product. The value returned from the function will be written directly to the parent value, as long as there is no error. Options are also passed in, and it is the responsibility of the function to honor these options and handle any variations in behavior that should occur.
type Options ¶
type Options struct { // Overwrite a target value with source value even if it already exists Overwrite bool // Unexported fields on a struct can not be set. When a struct contains an unexported // field, the default behavior is to treat the entire struct as a single entity and // replace according to Overwrite settings. If this is enabled, an error will be thrown instead. // // Note: this is used by the default mergeStruct function, and may not apply if that is // overwritten with a custom function. Custom struct merge functions should consider // using this value as well. ErrorOnUnexported bool // To be used by merge functions to pass values down into recursive calls freely Context context.Context // contains filtered or unexported fields }
Options is used to determine the behavior of a merge. It also holds the collection of functions used to determine merge behavior of various types. Always use NewOptions() to generate options and then modify as needed.
func NewOptions ¶
func NewOptions() *Options
NewOptions generates default Options. Overwrite is set to true, and a set of default merge function definitions are added.
func (*Options) SetDefaultMergeFunc ¶ added in v1.1.0
SetDefaultMergeFunc is used to define a default merge func that will be used as a fallback when there is no specific merge behavior defined for a given item. If using NewOptions(), a very basic default merge function is predefined which will return the source in overwrite mode and the target otherwise. Use this to define custom default behavior when the simple case is not sufficient.
func (*Options) SetKindMergeFunc ¶ added in v1.1.0
SetKindMergeFunc is used to define a custom merge func that will be used to merge two items of a particular kind. Accepts reflect.Kind and the MergeFunc to merge it. This is useful for defining more general merge behavior, for instance merge all maps or structs in a particular way. A default merge behavior is predefined for map, slice and struct when using NewOptions()
func (*Options) SetTypeMergeFunc ¶ added in v1.1.0
SetTypeMergeFunc is used to define a custom merge func that will be used to merge two items of a particular type. Accepts the reflect.Type representation of the type and the MergeFunc to merge it. This is useful for defining specific merge behavior of things such as specific struct types