Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go Viper中访问嵌套YAML结构时出现问题_Go_Viper_Viper Go - Fatal编程技术网

Go Viper中访问嵌套YAML结构时出现问题

Go Viper中访问嵌套YAML结构时出现问题,go,viper,viper-go,Go,Viper,Viper Go,最近用我的眼镜蛇应用程序尝试Viper来解析我的配置,但结果证明我无法解析内部嵌套块。ClustersOuter的映射始终为空,我确信映射结构标记正确。我更喜欢使用Viper解组功能,而不是手动获取数据类型 输出 map[zones:[{ClustersOuter:map[] Id:1} {ClustersOuter:map[] Id:2}]] 不知何故,我似乎只能检索区域地图及其ID,而不能检索集群地图的任何地图 我的意见: config.yml zones: - id: 1 c

最近用我的眼镜蛇应用程序尝试Viper来解析我的配置,但结果证明我无法解析内部嵌套块。
ClustersOuter
的映射始终为空,我确信映射结构标记正确。我更喜欢使用Viper解组功能,而不是手动获取数据类型

输出

map[zones:[{ClustersOuter:map[] Id:1} {ClustersOuter:map[] Id:2}]]
不知何故,我似乎只能检索
区域
地图及其ID,而不能检索
集群
地图的任何地图

我的意见:
config.yml

zones:
  - id: 1
    clusters:
      - cluster_id: 1
        k3s_config_file: "k3s-cluster-1-config.yaml"
      - cluster_id: 2
        k3s_config_file: "k3s-cluster-2-config.yaml"
      - cluster_id: 3
        k3s_config_file: "k3s-cluster-3-config.yaml" 
  - id: 2
    clusters:
      - cluster_id: 1
        k3s_config_file: "k3s-cluster-1-config.yaml"
      - cluster_id: 2
        k3s_config_file: "k3s-cluster-2-config.yaml"
      - cluster_id: 3
        k3s_config_file: "k3s-cluster-3-config.yaml"   
configuration.go

package config

type Zones struct {
    ClustersOuter map[string][]ClustersOuter `mapstructure:"zones"`
    Id            int                        `mapstructure:"id"`
}
type ClustersInner struct {
    ClusterId     int    `mapstructure:"cluster_id"`
    K3sConfigFile string `mapstructure:"k3s_config_file"`
}

type ClustersOuter struct {
    ClustersInner map[string][]ClustersInner `mapstructure:"clusters"`
}

type ConfigSuper map[string][]Zones

    viper.SetConfigName("config")
    viper.AddConfigPath(".")
    viper.SetConfigType("yaml")

    if err := viper.ReadInConfig(); err != nil {
        log.Fatalf("Error reading config file, %s", err)
        return false
    }

    var configuration config.ConfigSuper
    err := viper.Unmarshal(&configuration)
    if err != nil {
        log.Fatalf("unable to decode into struct, %v", err)
    }

    fmt.Printf("%+v\n", configuration)
main.go

package config

type Zones struct {
    ClustersOuter map[string][]ClustersOuter `mapstructure:"zones"`
    Id            int                        `mapstructure:"id"`
}
type ClustersInner struct {
    ClusterId     int    `mapstructure:"cluster_id"`
    K3sConfigFile string `mapstructure:"k3s_config_file"`
}

type ClustersOuter struct {
    ClustersInner map[string][]ClustersInner `mapstructure:"clusters"`
}

type ConfigSuper map[string][]Zones

    viper.SetConfigName("config")
    viper.AddConfigPath(".")
    viper.SetConfigType("yaml")

    if err := viper.ReadInConfig(); err != nil {
        log.Fatalf("Error reading config file, %s", err)
        return false
    }

    var configuration config.ConfigSuper
    err := viper.Unmarshal(&configuration)
    if err != nil {
        log.Fatalf("unable to decode into struct, %v", err)
    }

    fmt.Printf("%+v\n", configuration)

您的结构类型不反映YAML类型,看起来您在处理切片时将贴图和切片组合在一起了。在YAML中,地图如下所示:

myMap:
    key1: value1
myArray:
    - foo: bar
而数组是这样的:

myMap:
    key1: value1
myArray:
    - foo: bar
不幸的是,您的配置对象混乱不堪,我无法理解其意图,因此我为您重新编写了它。这些类型可以解码毒蛇没有任何问题

type Config struct {
    Zones []Zone `mapstructure:"zones"`
}

type Zone struct {
    ID       int        `mapstructure:"id"`
    Clusters []Cluster `mapstructure:"clusters"`
}

type Cluster struct {
    ClusterID     int    `mapstructure:"cluster_id"`
    K3sConfigFile string `mapstructure:"k3s_config_file"`
}
Config
struct是根对象,请确保将其解组

config := Config{}
err = viper.Unmarshal(&config)