Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hi , 請問在使用yml時 , struct結構體怎給default值? #94

Closed
zeralux opened this issue Aug 19, 2022 · 2 comments
Closed

hi , 請問在使用yml時 , struct結構體怎給default值? #94

zeralux opened this issue Aug 19, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@zeralux
Copy link

zeralux commented Aug 19, 2022

hi , 請問在使用yml時 , struct結構體怎給default值?

@inhere inhere self-assigned this Aug 20, 2022
@inhere inhere added enhancement New feature or request help wanted Extra attention is needed labels Aug 20, 2022
@inhere
Copy link
Member

inhere commented Aug 21, 2022

还没支持的,后续可以考虑加个 tag default:"value"

@inhere
Copy link
Member

inhere commented Aug 28, 2022

新版本已经添加此功能

https://github.com/gookit/config/releases/tag/v2.1.4:

config/issues_test.go

Lines 234 to 255 in edd2db6

func TestIssues_94(t *testing.T) {
is := assert.New(t)
// add option: config.ParseDefault
c := config.New("test").WithOptions(config.ParseDefault)
// only set name
c.SetData(map[string]interface{}{
"name": "inhere",
})
// age load from default tag
type User struct {
Age int `json:"age" default:"30"`
Name string
Tags []int
}
user := &User{}
is.NoErr(c.Decode(user))
dump.Println(user)
is.Eq("inhere", user.Name)
is.Eq(30, user.Age)

@inhere inhere closed this as completed Aug 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants