Categorygithub.com/containerssh/structutils
modulepackage
1.1.0
Repository: https://github.com/containerssh/structutils.git
Documentation: pkg.go.dev

# README

ContainerSSH - Launch Containers on Demand

ContainerSSH Struct Manipulation Library

Go Report Card LGTM Alerts

This library provides methods for manipulating structs.

⚠⚠⚠ Warning: This is a developer documentation. ⚠⚠⚠
The user documentation for ContainerSSH is located at containerssh.io.

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)
    // data 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.

# Functions

Copy deep copies a struct pointer from source to destination.
Defaults fills a struct pointer with default values from the "default" tag.
Merge copies non-default values from source to destination.

# Interfaces

DefaultsProvider is a struct that has a custom default setter.