doushi8599 2019-08-17 19:47
浏览 23
已采纳

如何将嵌套接口转换为地图切片的地图?

I have a map of arrays of maps map[string][]map[string]string, only, when I get the data, it's in the format map[interface{}]map[interface{}][]map[interface{}]interface{}, so I'm left to do a bunch of nested type assertions, which is really clunky, takes a long time to write, is hard to read/write, and is probably error prone, like this;

        if key == "identities" {
            idErrMessage := "Sorry, there was a problem with an identity"
            idArray, ok := setting.(map[string]interface{})
            if ok {
                for idType, ids := range idArray {
                    idGroupArray, ok := ids.([]interface{})
                    if ok {
                        for _, idGroup := range idGroupArray {
                            id, ok := idGroup.(map[interface{}]interface{})
                            if ok {
                                log.Println("type:", idType)
                                for key, val := range id {
                                    log.Printf("%v: %v", key, val)
                                }
                            } else {
                                log.Fatal(idErrMessage)
                            }
                        }
                    } else {
                        log.Fatal(idErrMessage)
                    }
                }
            } else {
                log.Fatal(idErrMessage)
            }
        }

I've been searching for a few hours now, and I can't seem to find an easier way to do this than the code above.

Is there anyway I can just v, ok := setting.(map[string]map[string][]map[string]string), or am I just stuck with the code above?

  • 写回答

2条回答 默认 最新

  • duanquanzhi5560 2019-08-17 21:41
    关注

    This is the final code I used:

    if key == "identities" {
        idTypes := convertToStringMap(setting)
        ghIds := convertToMapSlice(idTypes["github"])
        for _, ghId := range ghIds {
            for key, value := range convertToStringMap(ghId) {
                log.Println(key, value)
            }
        }
    }
    

    it basically converts the interfaces to map[string]interface{} one step at a time, which is the best way to do it I've found, and at least you stay out of nesting hell.

    These are the function to convert to the proper types:

    func convertToStringMap(i interface{}) map[string]interface{} {
        v, ok := i.(map[string]interface{})
        if !ok {
            v, ok := i.(map[interface{}]interface{})
            if ok {
                m2 := map[string]interface{}{}
                for k, v := range v {
                    m2[k.(string)] = v
                }
                return m2
            } else {
                log.Fatal("There was a problem converting to a string map")
            }
        }
        return v
    }
    
    func convertToMapSlice(i interface{}) []interface{} {
        v, ok := i.([]interface{})
        if !ok {
            log.Fatal("There was a problem converting to a slice")
        }
        return v
    }
    

    Overall though, I think I'm just going to find a work around so I don't have to do this at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式