defaults

package module
v0.0.0-...-3d8844e Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: MIT Imports: 4 Imported by: 0

README

defaults

Enabling struct with defaults values

Installation

The recommended way to install defaults

go get -u -v github.com/lycblank/defaults

Examples

Set the default value of the structure field through the structure tag

package main

import (
    "fmt"
    "github.com/lycblank/defaults"
)

func main() {
    car := &Car{}
    if err := defaults.Apply(car); err != nil {
        panic(err)
    }
    fmt.Printf("car:%+v\n", car) //Prints: car:&{Color:red Size:100 IsImport:true WheelRadius:1.01 SeatCount:4}
}

type Car struct {
    Color       string  `default:"red"`
    Size        int     `default:"100"`
    IsImport    bool    `default:"true"`
    WheelRadius float64 `default:"1.01"`
    SeatCount uint `default:"4"`
}

Set the default value of the structure through json parsing

package main

import (
    "fmt"
    "github.com/lycblank/defaults/json"
)

func main() {
    raw := `{}`
    car := &Car{}
    if err := json.Unmarshal([]byte(raw), car); err != nil {
        panic(err)
    }
    fmt.Printf("car:%+v\n", car) //Prints: car:&{Color:red Size:100 IsImport:true WheelRadius:1.01 SeatCount:4}
}

type Car struct {
    Color       string  `default:"red"`
    Size        int     `default:"100"`
    IsImport    bool    `default:"true"`
    WheelRadius float64 `default:"1.01"`
    SeatCount   uint    `default:"4"`
}

Access gin to support the default tag

package main

import (
    "fmt"
    "github.com/gin-gonic/gin/binding"
    "github.com/lycblank/defaults/validator"
)

func main() {
    validator := validator.NewStructValidatorWithDefault(binding.Validator)
    // Access gin to support the default tag
    // binding.Validator = validator.NewStructValidatorWithDefault(binding.Validator)
    carWithDefault := &CarWithDefault{}
    if err := validator.ValidateStruct(carWithDefault); err != nil {
        panic(err)
    }
    fmt.Printf("car:%+v\n", carWithDefault) //Prints: car:&{Color:red Size:100 IsImport:true WheelRadius:1.01 SeatCount:4}

    carWithoutDefault := &CarWithoutDefault{}
    if err := validator.ValidateStruct(carWithoutDefault); err != nil {
        panic(err) // panic: Key: 'CarWithoutDefault.Color' Error:Field validation for 'Color' failed on the 'required' tag
    }
}

type CarWithDefault struct {
    Color       string  `default:"red" binding:"required"`
    Size        int     `default:"100" binding:"required"`
    IsImport    bool    `default:"true" binding:"required"`
    WheelRadius float64 `default:"1.01" binding:"required"`
    SeatCount   uint    `default:"4" binding:"required"`
}

type CarWithoutDefault struct {
    Color       string  `binding:"required"`
    Size        int     `binding:"required"`
    IsImport    bool    `binding:"required"`
    WheelRadius float64 `binding:"required"`
    SeatCount   uint    `binding:"required"`
}

Set the default value of the map

package main

import (
    "fmt"
    "github.com/lycblank/defaults"
)

func main() {
    car := &Car{Wheels: map[string]*CarWheel{
        "1": {},
        "2": {},
        "3": {},
        "4": {Radius: 5},
    }}
    if err := defaults.Apply(car); err != nil {
        panic(err)
    }
    for k, v := range car.Wheels {
        fmt.Printf("%s=%+v\n", k, v)
        // Prints:
        //1=&{Color:black Radius:1.02}
        //2=&{Color:black Radius:1.02}
        //3=&{Color:black Radius:1.02}
        //4=&{Color:black Radius:5}
    }
}

type Car struct {
    Color     string               `default:"red"`
    Size      int                  `default:"100"`
    IsImport  bool                 `default:"true"`
    SeatCount uint                 `default:"4"`
    Wheels    map[string]*CarWheel // note: The value of the map needs to be a pointer type
}

type CarWheel struct {
    Color  string  `default:"black"`
    Radius float64 `default:"1.02"`
}

Caveats

A value of zero will be overwritten as the default value if the default tag is set.

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FieldCanNotSet     = errors.New("field can not set")
	ApplyValueNotPtr   = errors.New("apply value not ptr")
	DataTypeNotSupport = errors.New("data type not support")
	ValueCanNotSet     = errors.New("value can not set")
)

Functions

func Apply

func Apply(v interface{}) error

Apply set default values for variables

Types

type DefaultContainer

type DefaultContainer struct {
	// contains filtered or unexported fields
}

func (*DefaultContainer) Apply

func (d *DefaultContainer) Apply(v interface{}) error

Apply set default values for variables

Directories

Path Synopsis
example
json command
map command
struct command
validator command

Jump to

Keyboard shortcuts

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