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

结构体中包含数组字段,数组中的子结构体字段设置默认值不生效。 #141

Closed
ansoda opened this issue May 12, 2023 · 8 comments
Assignees
Labels
enhancement New feature or request resolved

Comments

@ansoda
Copy link

ansoda commented May 12, 2023

No description provided.

@inhere inhere added bug Something isn't working enhancement New feature or request labels May 16, 2023
@inhere
Copy link
Member

inhere commented May 19, 2023

可以给个结构体示例吗

@inhere inhere removed the bug Something isn't working label May 20, 2023
@ansoda
Copy link
Author

ansoda commented May 22, 2023

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

inhere added a commit to gookit/goutil that referenced this issue May 25, 2023
@inhere
Copy link
Member

inhere commented Jun 8, 2023

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

测试示例

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

@ansoda
Copy link
Author

ansoda commented Jun 12, 2023

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

@ansoda
Copy link
Author

ansoda commented Jun 12, 2023

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

测试示例

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

@inhere
Copy link
Member

inhere commented Jun 12, 2023

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)

@ansoda
Copy link
Author

ansoda commented Jun 12, 2023

@inhere

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

@inhere
Copy link
Member

inhere commented Jun 12, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resolved
Projects
None yet
Development

No branches or pull requests

2 participants