zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.42k stars 3.97k forks source link

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

Open leavest opened 4 months ago

leavest commented 4 months ago

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):

More description Add any other context about the problem here.

leavest commented 4 months ago

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

Issues-translate-bot commented 4 months ago

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 commented 4 months ago

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.