go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

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

Open leavest opened this issue 1 year ago • 3 comments
trafficstars

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 avatar Jul 11 '24 11:07 leavest

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

leavest avatar Jul 11 '24 11:07 leavest

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

Issues-translate-bot avatar Jul 11 '24 11:07 Issues-translate-bot

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.

kevwan avatar Jul 11 '24 14:07 kevwan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jul 13 '25 02:07 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Oct 15 '25 02:10 github-actions[bot]