Skip to content
This repository has been archived by the owner on Oct 2, 2022. It is now read-only.

Releases: ContainerSSH/structutils

1.1.0: Fixing merge overwrite

24 May 18:51
Compare
Choose a tag to compare

The previous version of this library didn't overwrite values when merging. This release fixes that.

1.0.0: First stable version

01 Apr 13:36
Compare
Choose a tag to compare

This release pins this library as a stable version for the ContainerSSH 0.4.0 release.

0.9.0: Copy, Merge, and Defaults

05 Dec 11:32
f62a4bc
Compare
Choose a tag to compare
Pre-release

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.