Giter Site home page Giter Site logo

Comments (4)

creasty avatar creasty commented on May 20, 2024

Could you try this?

type confStruct struct {
	ID     string       `yaml:"id"`
	Modules []confModule `yaml:"modules" default:"[]"`
}

from defaults.

ludanfeng avatar ludanfeng commented on May 20, 2024

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.

ludanfeng avatar ludanfeng commented on May 20, 2024

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.

creasty avatar creasty commented on May 20, 2024

Fixed in https://github.com/creasty/defaults/releases/tag/v1.3.0

from defaults.

Related Issues (20)

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.