2016-09-22 43 views
3

这是我YAML文件:Golang:检索嵌套键在YAML

hosts: all 
gather_facts: no 
remote_user: ubuntu 
name: install latest nginx 
tasks: 
    - name: install the nginx key 
    apt_key: 
     url: http://nginx.org/keys/nginx_signing.key 
     state: present 
    become: yes 

    - name: install aws cli 
    command: pip3 install awscli 
    become: yes 

这是我的代码:

package main 

import (
    "github.com/davecgh/go-spew/spew" 
    "gopkg.in/yaml.v2" 
    "io/ioutil" 
) 

type Config struct { 
    Hosts  string    `yaml:hosts` 
    Gather_facts string    `yaml:gatherfacts` 
    Remote_user string    `yaml:remoteuser` 
    Name   string    `yaml:names` 
    Tasks  []map[string]string `yaml:tasks` 
} 

func main() { 
    file, err := ioutil.ReadFile("/path-to-nginx1.yml") 
    if err != nil { 
     panic(err) 
    } 
    var config Config 
    yaml.Unmarshal(file, &config) 
    spew.Dump(config) 
} 

这里是输出:

(main.Config) { 
Hosts: (string) (len=3) "all", 
Gather_facts: (string) (len=2) "no", 
Remote_user: (string) (len=6) "ubuntu", 
Name: (string) (len=20) "install latest nginx", 
Tasks: ([]map[string]string) (len=2 cap=2) { 
    (map[string]string) (len=2) { 
    (string) (len=6) "become": (string) (len=3) "yes", 
    (string) (len=4) "name": (string) (len=21) "install the nginx key" 
    }, 
    (map[string]string) (len=3) { 
    (string) (len=4) "name": (string) (len=15) "install aws cli", 
    (string) (len=7) "command": (string) (len=19) "pip3 install awscli", 
    (string) (len=6) "become": (string) (len=3) "yes" 
    } 
} 
} 

问题:如何从我的YAML中检索以下密钥?

apt_key: 
    url: http://nginx.org/keys/nginx_signing.key 
    state: present 

此刻,我的Go语法分析器完全忽略了输出中的上述键。

另外,我有许多YAML文件,其中嵌套的程度不同。然后大多数文件本身具有不同程度的嵌套。那么我的struct需要修改以解决每个单独的密钥?或者,什么是更好的处理YAML文件的方法,每个键的嵌套级别不同?

>>> UPDATE < < <:

我通过修改取得了一些进展我Tasksstruct如下内:

type Config struct { 
    Hosts  string `yaml:hosts` 
    Gather_facts string `yaml:gatherfacts` 
    Remote_user string `yaml:remoteuser` 
    Name   string `yaml:names` 
    Tasks  []struct { 
     Name string `yaml:name` 
     Apt_key struct { 
      Url string `yaml:url` 
      State string `yaml:url` 
     } `yaml:apt_key` 
     Become string `yaml:become` 
    } 
} 

输出:

(main.Config) { 
Hosts: (string) (len=3) "all", 
Gather_facts: (string) (len=2) "no", 
Remote_user: (string) (len=6) "ubuntu", 
Name: (string) (len=20) "install latest nginx", 
Tasks: ([]struct { Name string "yaml:name"; Apt_key struct { Url string "yaml:url"; State string "yaml:url" } "yaml:apt_key"; Become string "yaml:become" }) (len=2 cap=2) { 
    (struct { Name string "yaml:name"; Apt_key struct { Url string "yaml:url"; State string "yaml:url" } "yaml:apt_key"; Become string "yaml:become" }) { 
    Name: (string) (len=21) "install the nginx key", 
    Apt_key: (struct { Url string "yaml:url"; State string "yaml:url" }) { 
    Url: (string) (len=39) "http://nginx.org/keys/nginx_signing.key", 
    State: (string) (len=7) "present" 
    }, 
    Become: (string) (len=3) "yes" 
    }, 
    (struct { Name string "yaml:name"; Apt_key struct { Url string "yaml:url"; State string "yaml:url" } "yaml:apt_key"; Become string "yaml:become" }) { 
    Name: (string) (len=15) "install aws cli", 
    Apt_key: (struct { Url string "yaml:url"; State string "yaml:url" }) { 
    Url: (string) "", 
    State: (string) "" 
    }, 
    Become: (string) (len=3) "yes" 
    } 
} 
} 

所以现在我可以看到apt_key部分早先完全失踪。 不过,我不觉得这是写在YAML作为command部分:

 command: pip3 install awscli 

如何获得呢?

此外,我没有得到一个不错的感觉,因为我必须在struct中声明几乎每一个键,在这种情况下,因为我的YAML几乎不是15行。但是,如果YAML越来越大,这将会是丑陋和麻烦的。 我确信必须有更好更高效的方式来处理YAML文件。

回答

1

看起来好像你已经完成了所有的事情,你只是在配置结构定义中错过了Command部分。我看到其他答案指出,你有不一致的数据,这是正确的,但你可以在同一个结构中获取它们,只是如果缺少yaml文件,缺少的字段将是空的。

package main 

    import (
     "github.com/davecgh/go-spew/spew" 
     "gopkg.in/yaml.v2" 
     "io/ioutil" 
    ) 

    type Config struct { 
     Hosts  string `yaml:hosts` 
     Gather_facts string `yaml:gatherfacts` 
     Remote_user string `yaml:remoteuser` 
     Name   string `yaml:names` 
     Tasks  []struct { 
      Name string `yaml:name` 
      Apt_key struct { 
       Url string `yaml:url` 
       State string `yaml:url` 
      } `yaml:apt_key` 
      Become string `yaml:become` 
      Command string `yaml:command` 
     } 
    } 

    func main() { 
     file, err := ioutil.ReadFile("/home/bane/Desktop/go/a.yml") 
     if err != nil { 
      panic(err) 
     } 
     var config Config 
     yaml.Unmarshal(file, &config) 
     // spew.Dump(config) 
     spew.Dump(config.Tasks[0]) 
     spew.Dump(config.Tasks[1]) 
    } 
+0

谢谢。 +1。我试过,发现'command'在不存在的键中显示为空。我认为这是做错的方法。 – slayedbylucifer

1

它不工作,因为你正在试图解组2型动物组:姓名,易键,变得和名称,命令,成为使用相同的结构。这是不一致的。

+0

是的。我了解数据不一致。但它是一个有效的'YAML',我的实际'YAML'比这个更长,而且更加不一致的键。你能建议我怎么处理这个问题。 – slayedbylucifer

+1

可以使用两个单独的结构 –

+1

如果你不能仅限于两个结构,你可以尝试获取所有的键,然后从这些键获取值 –