How to create a reusable code in Golang to read different yamls and put them into different structs types

huangapple go评论87阅读模式
英文:

How to create a reusable code in Golang to read different yamls and put them into different structs types

问题

我必须读取2个或3个或更多的不同结构的YAML文件,并为每个结构创建一个结构体来存储它们。到目前为止,我为每个结构体创建了单独的函数,虽然可以工作,但看起来不够优雅...我认为。

以下是当前的函数:

// 将YAML读取到结构体中
type Config struct {...}
type ExecuteQueries struct {...}
func parseYamlConfig(pathYaml string) Config {
myConfig := Config{}
var err error
var yamlFile []byte
if pathYaml == "" {
yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
} else {
yamlFile, err = ioutil.ReadFile(pathYaml)
}
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
if err != nil {
log.Fatalf("error: %v", err)
}
return myConfig
}
func parseYamlConfig2(pathYaml string) ExecuteQueries {
myConfig := ExecuteQueries{}
var err error
var yamlFile []byte
if pathYaml == "" {
yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
} else {
yamlFile, err = ioutil.ReadFile(pathYaml)
}
if err != nil {
log.Fatalf("error: %v", err)
}
err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
if err != nil {
log.Fatalf("error: %v", err)
}
return myConfig
}

请注意,它们实际上在返回值和接收值上是不同的,但数据处理非常相似。如何以更优雅的方式表达这一点?

英文:

I have to read let say 2 or 3 or more yamls that are different in structure and have a struct for each of those structures where I want to store them. So far I am creating separate functions for each and it works, but does not look very elegant... I think.

Here are the functions today:

// read the Yaml into struct(s)
type Config struct {...}
type ExecuteQueries struct {...}
func parseYamlConfig(pathYaml string) Config {
	myConfig := Config{}
	var err error
	var yamlFile []byte
	if pathYaml == "" {
		yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
	} else {
		yamlFile, err = ioutil.ReadFile(pathYaml)
	}
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	return myConfig
}
func parseYamlConfig2(pathYaml string) ExecuteQueries {
	myConfig := ExecuteQueries{}
	var err error
	var yamlFile []byte
	if pathYaml == "" {
		yamlFile, err = ioutil.ReadFile("./conf/conf.yaml")
	} else {
		yamlFile, err = ioutil.ReadFile(pathYaml)
	}
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	err = yaml.Unmarshal([]byte(yamlFile), &myConfig)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	return myConfig
}

Notice that they are actually different in what they return and what they receive, but the processing of data is very similar. How should this be expressed in a more elegant way?

答案1

得分: 2

func unmarshalYAMLFile(path string, v interface{}) error {
    if path == "" {
        path = "./conf/conf.yaml"
    }

    f, err := os.Open(path)
    if err != nil {
        return err
    }
    defer f.Close()

    return yaml.NewDecoder(f).Decode(v)
}
conf1 := Config{}
if err := unmarshalYAMLFile("/path/to/conf.yaml", &conf1); err != nil {
    panic(err)
}

conf2 := ExecuteQueries{}
if err := unmarshalYAMLFile("/path/to/conf_2.yaml", &conf2); err != nil {
    panic(err)
}
英文:
func unmarshalYAMLFile(path string, v interface{}) error {
    if path == "" {
        path = "./conf/conf.yaml"
    }

	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()
	
	return yaml.NewDecoder(f).Decode(v)
}
conf1 := Config{}
if err := unmarshalYAMLFile("/path/to/conf.yaml", &conf1); err != nil {
	panic(err)
}

conf2 := ExecuteQueries{}
if err := unmarshalYAMLFile("/path/to/conf_2.yaml", &conf2); err != nil {
	panic(err)
}

huangapple
  • 本文由 发表于 2021年12月14日 15:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70345509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定