Documentation
¶
Overview ¶
Package merge defines useful functions to deeply merge arbitrary values.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeepMap ¶ added in v0.2.0
DeepMap “deeply map,” the contents of src into dst defined as follows. Two values of identical kind are always deeply map if one of the following cases applies. Values of distinct kinds can may be deeply map.
Array values are deeply map their corresponding elements.
Struct values are deeply map their corresponding exported fields. As a special case, src can have kind Map, keys will be dst fields' names in lower camel case.
Func values deeply map if dst is nil and src is not and both have the same signature; otherwise they not deeply mapped.
Interface values are deeply map they hold concrete values.
Map values deeply map when all of the following are true: either they are the same map object or their corresponding keys (matched using Go equality) map to deeply map values. As a special case, src can have kind Struct, keys will be src fields' names in lower camel case.
Pointer values are deeply map if they are equal using Go's == operator or if they point to deeply map values.
Slice values deeply map when all of the following are true: either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) deeply mapped.
Other values - numbers, bools, strings, and channels - deeply mapped if dst is zero value and src is not, they deeply mapped dst = src using Go's = operator.
Numeric types values deeply map without precision lost and overflow.
String values alse deeply map from a signed or unsigned integer value, slices of bytes, and slices of runes.
On the other hand, pointer values are always equal to themselves, even if they point at or contain such problematic values, because they compare equal using Go's == operator, and that is a sufficient condition to be deeply mapped, regardless of content. DeepMap has been defined so that the same short-cut applies to slices and maps: if x and y are the same slice or the same map, they are deeply mapped regardless of content.
As DeepMap traverses the data values it may find a cycle. The second and subsequent times that DeepMap compares two pointer values that have been mapped before, it treats the values as mapped rather than examining the values to which they point. This ensures that DeepMap terminates.
func DeepMerge ¶
DeepMerge "deeply merge," the contents of src into dst defined as follows. Two values of identical type can deeply merge it following cases applies. Values of distinct types can not deeply merge.
Array values deeply merge their corresponding elements.
Struct values deeply merge their corresponding exported fields.
Func values deeply merge if dst is nil and src is not; otherwise they not deeply merge.
Interface values deeply merge they hold concrete values.
Map values deeply merge when all of the following are true: either they are the same map object or their corresponding keys (matched using Go equality) map to deeply merged values.
Pointer values deeply merge if they are equal using Go's == operator or if they point to deeply merged values.
Slice values deeply merge when all of the following are true: either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) deeply merged.
Other values - numbers, bools, strings, and channels - deeply merge if dst is zero value and src is not, they deeply merged dst = src using Go's = operator.
On the other hand, pointer values are always equal to themselves. because they compare equal using Go's == operator, and that is a sufficient condition to be deeply merged, regardless of content. DeepMerge has been defined so that the same short-cut applies to slices and maps: if x and y are the same slice or the same map, they are deeply merged regardless of content.
As DeepMerge traverses the data values it may find a cycle. The second and subsequent times that DeepMerge compares two pointer values that have been merged before, it treats the values as merged rather than examining the values to which they point. This ensures that DeepMerge terminates.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures for specific behavior of DeepMerge and DeepMap.
func WithAppendSlice ¶
func WithAppendSlice() Option
WithAppendSlice make merge append slices instead of overwriting it.
func WithOverwrite ¶
func WithOverwrite() Option
WithOverwrite make merge overwrite non-empty dst attributes with non-empty src attributes values.
func WithOverwriteEmptySlice ¶
func WithOverwriteEmptySlice() Option
WithOverwriteEmptySlice will make merge override empty dst slice with empty src slice.
func WithOverwriteWithEmptyValue ¶
func WithOverwriteWithEmptyValue() Option
WithOverwriteWithEmptyValue make merge overwrite non-empty dst attributes with empty src attributes values.
func WithTransformer ¶
WithTransformer adds transformer to merge, allowing to customize the merging of some types. The transformer f must be a function "func(dst *T, src T) error"
func WithTypeCheck ¶
func WithTypeCheck() Option
WithTypeCheck make merge check types while overwriting it (must be used with WithOverwrite).
func WithoutDereference ¶
func WithoutDereference() Option
WithoutDereference prevents dereferencing pointers when evaluating whether they are empty (i.e. a non-nil pointer is never considered empty).