package
0.0.0-20200416044943-d22e7c36ff52
Repository: https://github.com/albertwidi/go-project-example.git
Documentation: pkg.go.dev

# README

Defaults

Defaults is a library to set default value to a struct using a struct tag. Struct tag default:"value" is used to replace struct field value. The field only set with default value if the field value is empty or zero, which means:

  • 0 for number
  • "" for string

Boolean is not supported

Boolean is not supported because when the default is true, it will always be set to true. Because false is similar to 0 for number.

The workaround for this problem is to make your boolean field to always has false as default value.

Example

type A struct {
    Str string `default:"asd"`
    I   int    `default:"123"`
}

func main() {
    a := A{
        I:10,
    }
    if err := defaults.SetDefault(&a); err != nil {
        // do something to error
    }
}

The Str field in struct A will be replaced by value in the default tag.

Default Value Replacer (Experimental)

Replacing a struct default value with another struct value. This is useful for overriding some values in a config.

More to be added here.

Known Issues

The integer and float value is vurnerable to incompability of its type. For example, a very large int value is being set for int8 value.

type A struct {
    I8 int8 `default:"100000"`
}

Will make I8 value to 0

# Functions

ReplaceDefaultFrom will replace value of a struct with another value in a struct to be replaced, field name and type between two struct must be the same for example: type A struct { Field1 string } type B struct { Field1 string}.
SetDefault set default value from struct tag: default for example: type A struct { S string `default:"this is default"` }.

# Variables

ErrNotStruct is returned if the caller pass a pointer of non struct.
ErrPassValue is returned if the caller pass a value instead of a pointer.