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

load yaml config file can not recognize purely numeric strings without quotatio #4243

Open
leavest opened this issue Jul 11, 2024 · 3 comments

Comments

@leavest
Copy link

leavest commented Jul 11, 2024

Describe the bug
A clear and concise description of what the bug is.
load yaml config file can not recognize purely numeric strings without quotatio
To Reproduce
Steps to reproduce the behavior, if applicable:

  1. The code is
# config.yaml
Name: 123456
Host: 127.0.0.1
Port: 6370


# main.go
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/zeromicro/go-zero/core/conf"
	"gopkg.in/yaml.v2"
)

type Config struct {
	Name string `json:",default=123456789" yaml:"Name"`
	Host string `json:",default=0.0.0.0" yaml:"Host"`
	Port int    `yaml:"Port"`
}

var f = flag.String("f", "config.yaml", "config file")

func main() {
	flag.Parse()
	var c1, c2 Config

	// 读取YAML文件
	yamlFile, err := os.ReadFile(*f)
	if err != nil {
		println(err)
		return
	}

	// 解析YAML
	if err = yaml.Unmarshal(yamlFile, &c1); err != nil {
		println(err)
		return
	}
	fmt.Println(c1)

	conf.MustLoad(*f, &c2)
	// conf.MustLoad(*f, &c,conf.UseEnv()) // 额外从环境变量中加载配置
	fmt.Println(c2.Name)
}
  1. The error is
 {123456 127.0.0.1 6370}
2024/07/11 19:35:55 error: config file config.yaml, type mismatch for field "name", expect "string", actual "number"
exit status 1

Expected behavior
A clear and concise description of what you expected to happen.
can load config as yaml load
Screenshots
If applicable, add screenshots to help explain your problem.

Environments (please complete the following information):

  • OS: [e.g. Linux] windows11
  • go-zero version [e.g. 1.2.1] 1.6.6
  • goctl version [e.g. 1.2.1, optional]

More description
Add any other context about the problem here.

@leavest
Copy link
Author

leavest commented Jul 11, 2024

我知道加个引号可以避免这个问题,但是希望load行为向yaml.Unmarshal看齐

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I know that adding quotes can avoid this problem, but I hope that the load behavior will be consistent with yaml.Unmarshal

@kevwan
Copy link
Contributor

kevwan commented Jul 11, 2024

Let me check how to fix it.

We designed it like this is because go-zero handles config files in the same way inspite of yaml, json, toml, properties etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants