shallow

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 3 Imported by: 0

README

Go Reference

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(first interface{}, second interface{}, keys map[string]interface{}, opts ...Option) (diffKeys []string, err error)

Diff compare structs based on the following rule: for every field of the first struct, if the field's tag (specified by tag option, default "json") can be found in the keys map, compare the corresponding value in the first struct to the value in the second struct. If the keys map is nil, all field will be compared.

First and second must be a pointer to a non-nil struct of the same type.

Returns with a list of diff keys. This list can include elements that are NOT actually different if the first struct and the second struct had the same value for the given key, and the keys map contained this key.

Traverses anonym fields with struct or struct pointer type, even if they are nested (anonym structs within anonym structs). Other types of anonym fields are not supported and will raise an error.

Does NOT check nested fields other than anonym.

func Merge

func Merge(dest interface{}, update interface{}, keys map[string]interface{}, opts ...Option) (updatedKeys []string, err error)

Merge the update struct into the dest struct based on the following rule: for every field of the update struct, if the field's tag (specified by tag option, default "json") can be found in the keys map, set the corresponding value in the dest struct to the value in the update struct.

Dest and update must be a pointer to a non-nil struct of the same type.

Returns with a list of updated keys. This list can include elements that are NOT actually changed if the dest struct and the update struct had the same value for the given key, and the keys map contained this key.

Traverses anonym fields with struct or struct pointer type, even if they are nested (anonym structs within anonym structs). Other types of anonym fields are not supported and will raise an error.

Does NOT merge nested fields other than anonym. For these, either the dest value is kept intact, or the update value is used, but the two are never merged.

Types

type Option

type Option func(*options)

func UseTag

func UseTag(tag string) Option

UseTag can be used to use struct tags other than json.

Jump to

Keyboard shortcuts

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