go-zero
go-zero copied to clipboard
load yaml config file can not recognize purely numeric strings without quotatio
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:
- 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)
}
- 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.
我知道加个引号可以避免这个问题,但是希望load行为向yaml.Unmarshal看齐
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
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.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.