Giter VIP home page Giter VIP logo

Comments (8)

ansoda avatar ansoda commented on May 14, 2024 1

image
当用yaml配置多个Logger时,Logger中的默认值不生效
@inhere

from config.

inhere avatar inhere commented on May 14, 2024

可以给个结构体示例吗

from config.

inhere avatar inhere commented on May 14, 2024

已增强处理,下个版本支持。

测试示例

示例 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.

ansoda avatar ansoda commented on May 14, 2024

示例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个结构体需要默认值时,只有第一个结构体被赋了默认值,后面的结构体没有赋默认值。

@inhere

from config.

ansoda avatar ansoda commented on May 14, 2024

已增强处理,下个版本支持。

测试示例

示例 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个结构体需要默认值时,只有第一个结构体被赋了默认值,后面的结构体没有赋默认值。

@inhere

from config.

inhere avatar inhere commented on May 14, 2024

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.

ansoda avatar ansoda commented on May 14, 2024

@inhere

针对bool类型配置中已经被设置了false,最后结构体还是被置成true的问题。主要是把字段未设置和字段值为false没有区别对待了。不知道这种情况,有没有更好办法应对?

from config.

inhere avatar inhere commented on May 14, 2024

这是go本身的性质,无法区分。 就跟 int 字段设置个 0,你也不知道是go初始化的还是外部设置的。

from config.

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.