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

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条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格