Comments (4)
Could you try this?
type confStruct struct {
ID string `yaml:"id"`
Modules []confModule `yaml:"modules" default:"[]"`
}
from defaults.
Could you try this?
type confStruct struct { ID string `yaml:"id"` Modules []confModule `yaml:"modules" default:"[]"` }
Does not work. The following is the test result and case:
Diff:
--- Expected
+++ Actual
@@ -5,6 +5,3 @@
Name: (string) (len=2) "m1",
- Params: ([]string) (len=2) {
- (string) (len=2) "-c",
- (string) (len=8) "conf.yml"
- }
+ Params: ([]string)
},
package utils
import (
"testing"
"github.com/creasty/defaults"
"github.com/stretchr/testify/assert"
)
type confModule struct {
Name string `yaml:"name"`
Params []string `yaml:"params" default:"[\"-c\", \"conf.yml\"]"`
}
type confStruct struct {
Others string `yaml:"others"`
Modules []confModule `yaml:"modules" default:"[]"`
}
func TestSetDefaults(t *testing.T) {
tests := []struct {
name string
args *confStruct
want *confStruct
wantErr bool
}{
{
name: "defaults-struct-slice",
args: &confStruct{
Others: "others",
Modules: []confModule{
confModule{
Name: "m1",
},
confModule{
Name: "m2",
Params: []string{"arg1", "arg2"},
},
},
},
want: &confStruct{
Others: "others",
Modules: []confModule{
confModule{
Name: "m1",
Params: []string{"-c", "conf.yml"},
},
confModule{
Name: "m2",
Params: []string{"arg1", "arg2"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := defaults.Set(tt.args); (err != nil) != tt.wantErr {
t.Errorf("SetDefaults() error = %v, wantErr %v", err, tt.wantErr)
}
assert.Equal(t, tt.want, tt.args)
})
}
}
from defaults.
And my current workaround is:
package utils
import (
"reflect"
"github.com/creasty/defaults"
"github.com/juju/errors"
)
// SetDefaults set default values
func SetDefaults(ptr interface{}) error {
err := defaults.Set(ptr)
if err != nil {
return errors.Errorf("%v %s", ptr, err.Error())
}
v := reflect.ValueOf(ptr).Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
if f := t.Field(i); f.Type.Kind() == reflect.Slice {
slice := v.Field(i)
for j := 0; j < slice.Len(); j++ {
sliceItem := slice.Index(j)
if sliceItem.Kind() != reflect.Struct {
continue
}
sliceItemTemp := reflect.New(sliceItem.Type())
sliceItemTemp.Elem().Set(sliceItem)
err = SetDefaults(sliceItemTemp.Interface())
if err != nil {
return errors.Trace(err)
}
sliceItem.Set(sliceItemTemp.Elem())
}
}
}
return nil
}
from defaults.
Fixed in https://github.com/creasty/defaults/releases/tag/v1.3.0
from defaults.
Related Issues (20)
- Wrong example in Readme? HOT 1
- Default value for slice of struct doesn't work for golang v 1.12 HOT 2
- Default true on bool does not allow to set false HOT 1
- The Setter interface only works on struct pointer fields, not the struct itself
- bug: not set default for fields of a pointer struct field in an object.
- Add MustSet() function HOT 1
- Default values map[string][]string for struct HOT 2
- Default value to the entry that key already exists in a map HOT 5
- Booleans and defaults seems not to be working HOT 2
- Unable to keep zero value in case of setting the zero value on purpose HOT 2
- Customizable value filler
- Add require field tag
- bool is false default is true, handler is true HOT 1
- bug: []time.Duration not works HOT 5
- Support unmarshalling custom types.
- Unset() HOT 1
- Panic when attempting to initialize a field which references an interface HOT 1
- Struct tag boolean values are not parsed properly when specified with double quotes HOT 4
- no errors if default value doesn't parse HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from defaults.