函数无法识别Yaml嵌套结构

E235:

我有以下结构YAML:

type YamlConfig struct {
    Items struct {
        RiskyRoles []struct {
            Name string `yaml:"name"`
            Rules []struct{
                Verbs []string `yaml:"verbs"`
                ResourceOperator string `yaml:"resourcesOperator"`
                Resources []string `yaml:"resources"`
            }
        } `yaml:"RiskyRoles"`
    } `yaml:"Items"`
}  

我有一个将YAML文件解析为对象的函数,然后将Rules结构对象发送给名为的函数DoingStuff(..)

yamlFile, err := ioutil.ReadFile("actionItems.yaml")
if err != nil {
    fmt.Printf("Error reading YAML file: %s\n", err)
} else{
    var yamlConfig YamlConfig
    err = yaml.Unmarshal(yamlFile, &yamlConfig)
    if err != nil {
        fmt.Printf("Error parsing YAML file: %s\n", err)
    }

    for _, yamlRole := range yamlConfig.Items.RiskyRoles{
       DoingStuff(yamlRole.Rules)
    }
}

但是在函数内部DoingStuffRules无法识别struct对象

func DoingStuff(yamlRules []struct{}) {
  // Not recognize ****
  for _, rule := range yamlRules {
      fmt.Print(rule.ResourceOperator)
  }
}

如何将其转换为:

Rules []struct{
    Verbs []string `yaml:"verbs"`
    ResourceOperator string `yaml:"resourcesOperator"`
    Resources []string `yaml:"resources"`
}

我应该重新声明这个结构吗?还是使用接口进行投射?

编辑:

我添加了新结构并在YamlConfig结构内部使用了它,但是解析无法解析规则:

type RulesStruct struct {
    Rules []struct{
        Verbs []string `yaml:"verbs"`
        ResourceOperator string `yaml:"resourcesOperator"`
        Resources []string `yaml:"resources"`
    }
}
type YamlConfig struct {
    Items struct {
        RiskyRoles []struct {
            Name string `yaml:"name"`
            Message string `yaml:"message"`
            Priority string `yaml:"priority"`
            Rules []RulesStruct
        } `yaml:"RiskyRoles"`
    } `yaml:"Items"`
}
E235:

感谢@mkporiva的帮助,我像这样更改了结构:

type RulesStruct struct {

    Verbs []string `yaml:"verbs"`
    ResourceOperator string `yaml:"resourcesOperator"`
    Resources []string `yaml:"resources"`

}

type YamlConfig struct {
    Items struct {
        RiskyRoles []struct {
            Name string `yaml:"name"`
            Message string `yaml:"message"`
            Priority string `yaml:"priority"`
            Rules []RulesStruct
        } `yaml:"RiskyRoles"`
    } `yaml:"Items"`
}  

现在工作正常。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章