-
Notifications
You must be signed in to change notification settings - Fork 55
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
结构体中包含数组字段,数组中的子结构体字段设置默认值不生效。 #141
Comments
可以给个结构体示例吗 |
|
… field and struct-slice field - for support the gookit/config#141
已增强处理,下个版本支持。 测试示例示例 config.json: {
"loggers": [
{
"name": "error",
"logFile": "logs/error.log"
},
{
"name": "request",
"logFile": "logs/request.log",
"maxSize": 2048,
"maxDays": 30,
"compress": false
}
]
} type Logger struct {
Name string `json:"name"`
LogFile string `json:"logFile"`
MaxSize int `json:"maxSize" default:"1024"` // MB
MaxDays int `json:"maxDays" default:"7"`
Compress bool `json:"compress" default:"true"`
}
type LogConfig struct {
Loggers []*Logger `default:""` // mark for parse default
}
c := config.New("issues141", config.ParseDefault)
c.LoadFiles("config.json")
opt := &LogConfig{}
err = c.Decode(opt)
dump.Println(opt) 输出效果: === RUN TestIssues_141
PRINT AT github.com/gookit/config/v2_test.TestIssues_141(issues_test.go:348)
&config_test.LogConfig {
Loggers: []*config_test.Logger [ #len=2,cap=2
&config_test.Logger {
Name: string("error"), #len=5
LogFile: string("logs/error.log"), #len=14
MaxSize: int(1024),
MaxDays: int(7),
Compress: bool(true),
},
&config_test.Logger {
Name: string("request"), #len=7
LogFile: string("logs/request.log"), #len=16
MaxSize: int(2048),
MaxDays: int(30),
Compress: bool(false),
},
],
},
--- PASS: TestIssues_141 (0.00s)
PASS |
示例config.json
通过如上的config.json测试不通过。 |
示例config.json
通过如上的config.json测试不通过。 |
Thanks @ansoda , 本地再调整了下逻辑,先映射配置在做初始化可以解决。 但是有个问题:
=== RUN TestIssues_141/3_elements
PRINT AT github.com/gookit/config/v2_test.TestIssues_141.func1(issues_test.go:402)
&config_test.LogConfig {
Loggers: []*config_test.Logger [ #len=3,cap=3
&config_test.Logger {
Name: string("info"), #len=4
LogFile: string("logs/info.log"), #len=13
MaxSize: int(1024),
MaxDays: int(7),
Compress: bool(true),
},
&config_test.Logger {
Name: string("error"), #len=5
LogFile: string("logs/error.log"), #len=14
MaxSize: int(1024),
MaxDays: int(7),
Compress: bool(true),
},
&config_test.Logger {
Name: string("request"), #len=7
LogFile: string("logs/request.log"), #len=16
MaxSize: int(2048),
MaxDays: int(30),
Compress: bool(true),
},
],
},
--- FAIL: TestIssues_141 (0.00s) |
针对bool类型配置中已经被设置了false,最后结构体还是被置成true的问题。主要是把字段未设置和字段值为false没有区别对待了。不知道这种情况,有没有更好办法应对? |
这是go本身的性质,无法区分。 就跟 |
- resolve multi slice-struct field, see issues #141
No description provided.
The text was updated successfully, but these errors were encountered: