defaults

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2014 License: MIT Imports: 2 Imported by: 0

README

go-defaults Build Status GoDoc

This library allow to define a default value to any struct, this is made thanks to struct tags.

A StructTag is the tag string in a struct field.

By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

Installation

The recommended way to install go-defaults

go get github.com/mcuadros/go-defaults

Examples

A basic example:

import "github.com/mcuadros/go-defaults"
import "fmt"

type ExampleBasic struct {
    Foo bool   `default:"true"` //<-- StructTag with a default key
    Bar string `default:"33"`
    Qux int8
}

func NewExampleBasic() *ExampleBasic {
    example := new(ExampleBasic)
    SetDefaults(example) //<-- This set the defaults values

    return example
}

...

test := NewExampleBasic()
fmt.Println(test.Foo) //Prints: true
fmt.Println(test.Bar) //Prints: 33
fmt.Println(test.Qux) //Prints:

License

MIT, see LICENSE

Documentation

Overview

This library allow to define a default value to any struct, this is made thanks to struct tags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaults

func SetDefaults(variable interface{})

Applies the default values to the struct object, the struct type must have the StructTag with name "default" and the directed value.

Usage

type ExampleBasic struct {
    Foo bool   `default:"true"`
    Bar string `default:"33"`
    Qux int8
}

 foo := &ExampleBasic{}
 SetDefaults(foo)

Types

type Filler added in v1.1.0

type Filler struct {
	FuncByName map[string]fillerFunc
	FuncByKind map[reflect.Kind]fillerFunc
	Tag        string
}

func (*Filler) Fill added in v1.1.0

func (self *Filler) Fill(variable interface{})

Jump to

Keyboard shortcuts

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