Comments (8)
当用yaml配置多个Logger时,Logger中的默认值不生效
@inhere
from config.
可以给个结构体示例吗
from config.
已增强处理,下个版本支持。
测试示例
示例 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
from config.
示例config.json
{
"loggers": [
{
"name": "info",
"logFile": "logs/info.log"
},
{
"name": "error",
"logFile": "logs/error.log"
},
{
"name": "request",
"logFile": "logs/request.log",
"maxSize": 2048,
"maxDays": 30,
"compress": false
}
]
}
通过如上的config.json测试不通过。
当数组中超过2个结构体需要默认值时,只有第一个结构体被赋了默认值,后面的结构体没有赋默认值。
from config.
已增强处理,下个版本支持。
测试示例
示例 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
{
"loggers": [
{
"name": "info",
"logFile": "logs/info.log"
},
{
"name": "error",
"logFile": "logs/error.log"
},
{
"name": "request",
"logFile": "logs/request.log",
"maxSize": 2048,
"maxDays": 30,
"compress": false
}
]
}
通过如上的config.json测试不通过。
当数组中超过2个结构体需要默认值时,只有第一个结构体被赋了默认值,后面的结构体没有赋默认值。
from config.
Thanks @ansoda , 本地再调整了下逻辑,先映射配置在做初始化可以解决。
但是有个问题:
bool字段 你配置了 false,也会被初始化为 default:"true"。因为 bool(false) 是零值,会认为是没有设置过。
Compress bool
json:"compress" default:"true"
=== 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)
from config.
针对bool类型配置中已经被设置了false,最后结构体还是被置成true的问题。主要是把字段未设置和字段值为false没有区别对待了。不知道这种情况,有没有更好办法应对?
from config.
这是go本身的性质,无法区分。 就跟 int
字段设置个 0,你也不知道是go初始化的还是外部设置的。
from config.
Related Issues (20)
- [FEAT] Support Encrypted Config File HOT 4
- Add Duration type support HOT 2
- ReloadFiles方法在重载json格式的文件时会发生错误但toml正常
- Support slice with ParseEnv HOT 1
- Panic on missing key/value HOT 1
- More of a question setDecoder manually HOT 3
- yaml Binding ist not working correctly HOT 1
- String Durations are not parsed with ParseTime and ParseEnv
- 如何实现自动保存,或者set回调以实现自动保存 HOT 6
- ``Config.Set()`` is Not Override New Value if Value is Not The Same Type HOT 2
- Support Pretty Print? HOT 4
- 能增加一个配置写入吗? HOT 5
- Config.Error() HOT 1
- 你好作者,请问之后有没有修改文件后热重载功能的开发计划? HOT 7
- `time.Duration`支持从`string`加载 HOT 1
- 并发调用 Structure,存在获取不到数据的情况
- hi , 請問在使用yml時 , struct結構體怎給default值? HOT 2
- [FEAT] Support Set with Slice (Array) Index Using Square Brackets? HOT 3
- feat: support parse ENV var on struct default tag
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 config.