dtlrp119999 2019-04-11 15:11
浏览 23
已采纳

如何从地图界面解析特定键?

I am reading in a yaml file, and unmarshaling it into a map[interface{}]interface{}. The goal is to replace a certain value within the yaml, and update the file read in with just that value replaced. I'm not sure how to parse the map though to get to the specific key (password) I want to change. I believe you can use reflect, but I'm not 100% sure of the syntax

YAML file I am reading in (config.yml):

i_name: example

instances:
  - name: test-instance
    command: get
    arguments:
        hostname: localhost
        port: 203
        username: test
        password: testing123
    labels:
        env: dev

Code thus far:

func replaceConfigPassword() {
    yamlFile, err := ioutil.ReadFile("config.yml")
    fatalIfErr(err)

    m := make(map[interface{}]interface{})

    err = yaml.Unmarshal(yamlFile, &m)
    fatalIfErr(err)

    fmt.Println(m)

    val := reflect.ValueOf(m["instances"]["arguments"]["password"])
    fmt.Println("val=", val)

  • 写回答

1条回答 默认 最新

  • douyousu9691 2019-04-11 15:56
    关注

    The following shows how you might step through the yaml to arrive at the password:

    package main
    
    import (
        "fmt"
        "gopkg.in/yaml.v2"
    )
    
    func main() {
        in := []byte(`
    i_name: example
    
    instances:
      - name: test-instance
        command: get
        arguments:
            hostname: localhost
            port: 203
            username: test
            password: testing123
        labels:
            env: dev
    `)
    
        m := make(map[string]interface{})
        if err := yaml.Unmarshal(in, &m); err != nil {
            panic(err)
        }
        fmt.Printf("%+v
    ", m)
    
        instances := m["instances"].([]interface{})
        fmt.Printf("%+v
    ", instances)
    
        first := instances[0]
        fmt.Printf("%+v
    ", first)
    
        arguments := first.(map[interface{}]interface{})["arguments"]
        fmt.Printf("%+v
    ", arguments)
    
        password := arguments.(map[interface{}]interface{})["password"]
        fmt.Printf("%+v
    ", password)
    }
    
    $ go run main.go
    map[i_name:example instances:[map[name:test-instance command:get arguments:map[password:testing123 hostname:localhost port:203 username:test] labels:map[env:dev]]]]
    [map[name:test-instance command:get arguments:map[hostname:localhost port:203 username:test password:testing123] labels:map[env:dev]]]
    map[name:test-instance command:get arguments:map[hostname:localhost port:203 username:test password:testing123] labels:map[env:dev]]
    map[username:test password:testing123 hostname:localhost port:203]
    testing123
    

    If you know the input structure before hand, another option could be to model it explicitly:

    type Config struct {
        IName string `yaml:"i_name"`
        Instances []struct{
            Name string
            Command string
            Arguments map[string]interface{}
            Labels map[string]string
        }
    }
    
        var c Config
        if err := yaml.Unmarshal(in, &c); err != nil {
            panic(err)
        }
        fmt.Printf("%+v
    ", c)
        fmt.Printf("%+v
    ", c.Instances[0].Arguments["password"])
    
    $ go run main.go
    
    {IName:example Instances:[{Name:test-instance Command:get Arguments:map[hostname:localhost port:203 username:test password:testing123] Labels:map[env:dev]}]}
    testing123
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行