【发布时间】:2019-01-19 01:20:25
【问题描述】:
我有以下代码解析 YAML 文件,需要匹配来自一个结构 external 的值并更新 internal 结构的 type 属性。
例如这是yaml文件(为了简单起见翻译成bin)和正确解析的内容
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"log"
)
//internal config model for parsing
type InternalModel struct {
models []Model2 `yaml:"models"`
}
type Model2 struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Target string `yaml:"target"`
}
var internal_config = []byte(`
models:
- name: myapp
type: app1
target: ./
- name: myapp2
type: app2
target: ./
`)
type ExternalConfig struct {
Landscape Zone `yaml:"Landscape"`
}
type Zone struct {
Zone string `yaml:"zone"`
Models []Model `yaml:"models"`
}
type Model struct {
AppType string `yaml:"app-type"`
ServiceType string `yaml:"service-type"`
}
var external_config = []byte(`
Landscape:
zone: zone1
models:
- app-type: app1
service-type: GCP
- app-type: app2
service-type: AMAZON
zone: zone2
models:
- app-type: app3
service-type: AZURE
- app-type: app4Í
service-type: HEROKU
`)
func main() {
// This is the internal config which needs updated
internalConfiglYaml := InternalModel{}
err := yaml.Unmarshal([]byte(internal_config), &internalConfiglYaml)
if err != nil {
log.Fatalf("error in model internalConfiglYaml: %v", err)
}
//fmt.Printf("%+v\n", internalConfiglYaml)
//--------------------------Second config file-----------------------//
//This is the external config yaml
extConfigYaml := ExternalConfig{}
err = yaml.Unmarshal([]byte(external_config), &extConfigYaml)
if err != nil {
log.Fatalf("error in model extConfigYaml: %v", err)
}
fmt.Printf("%+v\n", extConfigYaml)
landscape := "zone1"
modifiedConfig := ConvertTypes(internalConfiglYaml, extConfigYaml, landscape)
fmt.Printf("%+v\n", modifiedConfig)
}
func ConvertTypes(int_cfg InternalModel, ext_config ExternalConfig, landscape string) (out_cfg InternalModel) {
for _, module := range int_cfg.models {
switch module.Type {
case "app1":
//here I hard-coded the value "GCP" but it should come from the yaml struct after parsing
module.Type = "GCP" // should be something like ext_config.models.service-type when the key in the struct
case "app2":
//here I hard-coded the value "AMAZON" but it should come from the yaml struct after parsing
module.Type = "AMAZON"
}
}
return int_cfg
}
//At the end what I need to do is to get the internal yaml file to be changed to the following struct
//The changes are when the type=app-type I need to modify the type in the internal config, here its GCP and ruby
//internal_config_after_changes := []byte(`
//
//
//models:
// - name: myapp
// type: GCP
// target: ./
//
// - name: myapp2
// type: AMAZON
// target: ./
//
//
//`)
最后我需要做的是将内部yaml文件更改为internal_config_after_changes上面的结构
变化是当type=app-type我需要修改internal_config中的type值,这里从app1到GCP和app2给amazon
问题在于我应该使用第二个循环来迭代 external_config 和匹配值,我不知道如何以有效的方式将它们结合起来......
【问题讨论】:
-
我认为您“没有很好地解释自己”,因为我重新阅读了您的问题,如果我没有回答,那么我不知道您想知道什么。跨度>
-
@ZanLynx 请看我的更新,现在更清楚了吗?
-
@JhonD 所以实际上你想根据你在开关右边定义的类型更新 intconfig 的值。
-
@Himanshu - 是的,确切地说,它应该像
internal_config_after_changes一样在末尾,并且应该从external_configyaml文件内容中获取值,当有像app1这样的值匹配时跨度> -
@JhonD 在
ConvertType函数中使用切片索引来获取底层数组的变化来检查答案。
标签: loops go recursion struct yaml