structutils

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

ContainerSSH Struct Manipulation Library

This library provides methods for manipulating structs.

Copying structures

The structutils.Copy() method provides a deep-copy mechanism for structs:

oldData := yourStruct{}
newData := yourStruct{}
err := structutils.Copy(&newData, &oldData)

The newData will be completely independent from oldData, including the copying of any pointers.

Merging structures

The structutils.Merge() method provides a deep merge of two structs:

oldData := yourStruct{}
newData := yourStruct{}
err := structutils.Merge(newData, oldData)

The Merge method will copy non-default values from oldData to newData.

Adding default values

The structutils.Defaults() method loads the default values from the default tag in a struct:

type yourStruct struct {
	Text string `default:"Hello world!"`
	Decision bool `default:"true"`
	Number int `default:"42"`
}

//...

func main() {
    data := yourStruct{}
    structutils.Defaults(&data)
    // testdata will now contain the default values
}

Default values can be provided as follows:

  • Scalars can be provided directly.
  • Maps, structs, etc. can be provided in JSON format.
  • time.Duration can be provided in text format (e.g. 60s).
  • Structs may implement a receiver with the method called SetDefaults() as described in defaults.go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(destination interface{}, source interface{}) error

Copy deep copies a struct pointer from source to destination.

func Defaults

func Defaults(data interface{})

Defaults fills a struct pointer with default values from the "default" tag. It only fills public fields.

- Scalars can be provided directly - maps, structs, etc. can be provided in JSON format - time.Duration can be provided in text format (e.g. 60s)

func Merge

func Merge(destination interface{}, source interface{}) error

Merge copies non-default values from source to destination

Types

type DefaultsProvider

type DefaultsProvider interface {
	// SetDefaults sets the default values on the current implementation.
	SetDefaults()
}

DefaultsProvider is a struct that has a custom default setter.

Jump to

Keyboard shortcuts

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