Code Monkey home page Code Monkey logo

go-defaults's People

Contributors

bc-vincent-zhao avatar mcuadros avatar pagongamedev avatar rs avatar russmatney avatar serabe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-defaults's Issues

Fields without a tag can still change

Example:

type Widget struct {
    B []bytes
}

defaults.SetDefaults(&Widget{})

The B field starts out nil, but will be set to an empty byte slice, even though that field has no "default" tag.

Looking at the code, I'm thinking either setDefaultValues should check for the presence of the tag, and skip the field if not present, or setDefaultValue should check the presence of the tag (after the zero value check).

Alternatively, if this was the intended behavior, then perhaps add a default:"-" option to explicitly skip the field? Or a flag in the Filler struct to toggle the behavior? I'd be happy to do a PR.

feature req: defaults for type pointers

I'd love it if this library also handled type pointers, so something like this:

type test struct {
  Name *string `default:"test"`
}

now obviously a string pointer isn't the most useful thing in the world, but handling it generically should allow us to specify nested struct pointers with their own defaults, and then this library would correctly initialize the whole data tree. I've written a little bit of wrapper code to do this myself (which actually doesn't even handle the string pointer case), but I'd love it if this was something the library just did itself:

setDefaults(reflect.ValueOf(test))

[...]

func setDefaults(v reflect.Value) {
  if v.Kind() != reflect.Ptr { return }
  if v.IsNil() {
    if v.CanSet() {
      v.Set(reflect.New(v.Type().Elem()))
    } else {
      return
    }
  }
  if v.Elem().Kind() != reflect.Struct { return }
  defaults.SetDefaults(v.Interface())
  v = v.Elem()

  for i := 0; i < v.NumField(); i++ {
    field := v.Field(i)
    if field.Type().Kind() == reflect.Ptr {
      setDefaults(field)
    }
  }
}

Default for `time.Duration` not working when using units

For fields of type time.Duration the documentation suggests that a default of "1m" should be parsed into the appropriate duration (i.e. 1 * time.Minute).
This does not seem to be working.

Defaults are only correctly applied when using numbers only (which are then interpreted as nanoseconds:

import (
	"fmt"
	"time"	
	"github.com/mcuadros/go-defaults"
)

type Foo struct {
	ConnectionTimeout time.Duration `default:"2000000000"`
}

func main() {
	c := &Foo{}
	defaults.SetDefaults(c)
	fmt.Printf("Foo: %+v", c)
}

Output:

Foo: &{ConnectionTimeout:2s}

However:

import (
	"fmt"
	"time"	
	"github.com/mcuadros/go-defaults"
)

type Foo struct {
	ConnectionTimeout time.Duration `default:"20s"`
}

func main() {
	c := &Foo{}
	defaults.SetDefaults(c)
	fmt.Printf("Foo: %+v", c)
}

Output:

Foo: &{ConnectionTimeout:0s}

Default value for slice of struct isn't working for me

package main

import (
	"fmt"

	"gopkg.in/mcuadros/go-defaults.v1"
)

func main() {

	foo := &Parent{}
	defaults.SetDefaults(foo)
	fmt.Print(foo)
}

type Child struct {
	Name string
	Age  int `default:"10"`
}

type Parent struct {
	Children []Child
}

output: &{[]}

Default value of true makes it impossible to set a field to false

If you make a field's default value true you can never set it to false. The go-defaults library will always override the false value to be true.

I made a repository with a small self contained example: link.

I don't think there is a way to fix this. Maybe go-defaults could simply ignore any bool fields. Or maybe make a custom bool type for go-defaults which keeps track of if its value has been set.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.