doupao1530 2018-07-10 17:05
浏览 316
已采纳

Golang Map YAML对象

I have a yaml configuration file that contains sets of configuration commands to send to network devices. Inside of each set, there are vendor-specific keys and the values for each vendor key can be either a configuration command string, a list of configuration command strings, or a list of key-value pairs mapping a vendor-specific model string to a configuration command string. Below is an example:

# example.yml
---
cmds:
  setup:
    cisco: "terminal length 0"
  config:
    cisco:
      - basic  : "show version"
      - basic  : "show boot"
        "3560" : "3560 boot command"
        "2960x": "2960x boot command"
      - basic  : "dir flash:"
        "3560" : "3560 dir command"
  cleanup:
    cisco: ["terminal no length", "quit"]

I want to combine these commands into a map like so:

var cmdMap = map[string][]string{
    "cisco": []string{
        "terminal length 0",
        "show version",
        "show boot",
        "dir flash:",
        "terminal no length",
        "quit",
    },
    "cisco.3560": []string{
        "terminal length 0",
        "show version",
        "3560 boot command",
        "3560 dir command",
        "terminal no length",
        "quit",
    },
    "cisco.2960x": []string{
        "terminal length 0",
        "show version",
        "2960x boot command",
        "dir flash:",
        "terminal no length",
        "quit",
    }
}

I am using spf13/viper to handle parsing the yaml file and have been able to add the specific commands to each vendor and model, but adding the commands that apply to both vendor and specific model is where I am stuck. This is the actual output of my program followed by my code:

$ go run main.go example.yml 
cmdMap["cisco"]
terminal length 0
show version
show boot
dir flash:
terminal no length
quit

# missing terminal length 0, show version, etc.
cmdMap["cisco.3560"]
3560 boot command
3560 dir command

# missing terminal length 0, show version, etc.
cmdMap["cisco.2960x"]
2960x boot command

My code:

package main

import (
    "github.com/spf13/viper"
    "fmt"
    "flag"
    "log"
)

func main() {
    flag.Parse()
    cfgFile := flag.Arg(0)

    v := viper.New()
    v.SetConfigType("yaml")
    v.SetConfigFile(cfgFile)

    if err := v.ReadInConfig(); err != nil {
        log.Fatal(err)
    }

    for k, v := range MapCfgCmds(v.GetStringMap("cmds")) {
        fmt.Printf("cmdMap[\"%s\"]
", k)
        for _, cmd := range v {
            fmt.Println(cmd)
        }
        fmt.Println()
    }
}

func MapCfgCmds(cfgCmds map[string]interface{}) map[string][]string {
    cmdMap := make(map[string][]string)
    for _, cmdSet := range cfgCmds {
        cmdSet, _ := cmdSet.(map[string]interface{})
        for vendor, cmdList := range cmdSet {
            switch cmds := cmdList.(type) {
            case string:
                // single string command (i.e., vendor: cmd)
                cmdMap[vendor] = append(cmdMap[vendor], cmds)
            case []interface{}:
                for _, cmd := range cmds {
                    switch c := cmd.(type) {
                    case string:
                        // list of strings (i.e., vendor: [cmd1,cmd2,...,cmdN])
                        cmdMap[vendor] = append(cmdMap[vendor], c)
                    case map[interface{}]interface{}:
                        // This is where I am stuck
                        //
                        // list of key-value pairs (i.e., vendor: {model: modelCmd})
                        for model, modelCmd := range c {
                            modelCmd, _ := modelCmd.(string)
                            if model == "basic" {
                                cmdMap[vendor] = append(cmdMap[vendor], modelCmd)
                                continue
                            }
                            modelKey := fmt.Sprintf("%s.%s", vendor, model)
                            cmdMap[modelKey] = append(cmdMap[modelKey], modelCmd)
                        }
                    }
                }
            }
        }
    }
    return cmdMap
}

How can I combine the "universal" and model-specific commands to get the expected cmdMap value from above?

  • 写回答

2条回答 默认 最新

  • dousi4257 2018-07-11 08:33
    关注

    I think viper is not helping you here, in the sense that viper does a lot of things you don't need, but it doesn't do one thing you could use here - clear mapping of the data. If you use the yaml library directly, you could declare a structure that corresponds to your data and makes it easier to understand it.

    There are several possible approaches to your problem, here is my attempt at solving it (you might need to tweak few things as I wrote it in the editor, without compiling it):

        type Data struct {
           Cmds struct {
             Setup map[string]interface{} `yaml:"setup"`
             Config map[string][]map[string]string `yaml:"config"`
             Cleanup map[string][]string `yaml:"cleanup"`
           } `yaml:"cmds"`
    
        }   
    
        data := Data{}
        err := yaml.Unmarshal([]byte(input), &data)
        if err != nil {
                log.Fatalf("error: %v", err)
        }
    
        setupCmds := make(map[string][]string)
        cleanupCmds := make(map[string][]string)
    
        result := make(map[string][]string)
    
    
        // Prepare setup commands, grouped by vendor
        for vendor, setupCmd := range data.Cmds.Setup {
            setupCmds[vendor] = append(setupCmds[vendor], setupCmd)
        }
    
        // Prepare cleanup commands, grouped by vendor
         for vendor, commands := range data.Cmds.Cleanup {
            cleanupCmds[vendor] = append(cleanupCmds[vendor], commands...)
        }
    
        // iterate over vendors and models, combine with setup & cleanup commands and store in result
        for vendor, configCmds := range data.Cmds.Config { // vendor = string (e.g. "cisco"), configCmds = []map[string][string] (e.g. - basic: "show version")
            // we now how many config commands there will be
            result[vendor] = make([]string, len(configCmds))
    
            // variantsCache will store all variants we've seen so far
            variantsCache := make(map[string]struct{})
            for i, model := range models { // i = int (number of command) model = map[string]string
                // we assume "basic" is available for each command
                result[vendor][i] = model["basic"]
                for variant, command := range model { // variant = string (e.g. "basic"), command = string (e.g. "show version")
                    if variant == "basic" {
                        // we already covered that
                        continue
                    }
                    variantKey := vendor + "." + variant
                    variantsCache[variantKey]
                    if _, ok := result[variantKey]; !ok {
                        // first command for this model, create a slice
                        result[variantKey] = make([]string, len(configCmds))
                    }
                    result[variantKey][i] = command
                }
            }
            // We need to iterate over all commands for all variants and copy "basic" command if there is none
            for variant, _ := range variantsCache {
                for i, command := range result[variant] {
                    if command == "" {
                        // copy the "basic" command, since there was no variant specific command
                        result[variant][i] = result[vendor][i]
                    }
                } 
            }
        }
    
        // combine setup and cleanup with config
        for variant, _ := result {
            // will return "cisco" for both "cisco" and "cisco.x3650"
            vendor := strings.Split(variant, ".")[0] 
            result[variant] = append(setupCmds[vendor], result[variant]...)
            result[variant] = append(result[variant], cleanupCmds[vendor]...)
        }
    
        return result
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)