go 2个多层嵌套的map取并集

使用递归取2个map并集,适用于写一个map去替换一个固定模版的map里的值(接口参数化替换等)
可能还存在bug。目前测试,见Test用例。

func MapSwitch(map01, map02 map[string]interface{}) map[string]interface{}{
    /**
     * @Description: 2个嵌套map取并集处理;map01:新的map数据,map02:待替换的map数据
                    PS:需保证map01和map02嵌套中的[]interface{}中对应下标的数据类型一致
     */
    for k, v := range map01{
        if _, ok := map02[k]; ok{
            switch v.(type) {
            case map[string]interface{}:
                MapSwitch(v.(map[string]interface{}), map02[k].(map[string]interface{}))
            case []interface{}:
                res := InterfaceSwitch(v.([]interface{}), map02[k].([]interface{}))
                map02[k] = res
            default:
                map02[k] = v
            }
        }else{
            map02[k] = v
        }
    }
    return map02
}

func InterfaceSwitch(interface01, interface02 []interface{})[]interface{}{
    /**
     * @Description: interface01:新的数据,interface02:待替换的数据
     */
    for k, v := range interface01{
        if k <= len(interface02) -1 {
            switch v.(type) {
            case map[string]interface{}:
                res := MapSwitch(v.(map[string]interface{}), interface02[k].(map[string]interface{}))
                newInterface = append(newInterface, res)
            case []interface{}:
                InterfaceSwitch(v.([]interface{}), interface02[k].([]interface{}))
            default:
                newInterface = append(newInterface, v)
            }
        }else{
            newInterface = append(newInterface, v)
        }
    }
    return newInterface
}

// Test
func TestQ01(t *testing.T){
    TTNEW := map[string]interface{}{
        "supplier": "Auto_test's Super Store NEW",
        "remark": "Create stock_adjustments Auto_test NEW",
        "items": []interface{}{
            map[string]interface{}{
                "sku_id": "1076802049474499",
                "quantity": 5,
                //"purchase_price": "999711",
                "items03":[]int{166, 266,366},
                "items04":map[string]interface{}{
                    "sku_id11": "10768020494744888",
                    "quantity": 88,
                    "purchase_price": "9997888",
                    "items03":[]int{155, 255,355},
                    "items04":[]int{1666, 2666,3666},
                },
            },
            map[string]interface{}{
                "sku_id": "1076802049474499NEW",
                "quantity": 1,
                //"purchase_price": "999711",
                "items33":[]int{166, 266,366},
                "items44":map[string]interface{}{
                    "sku_id11": "10768020494744888111",
                    "quantity": 88,
                    "purchase_price": "11111",
                    "items33":[]int{22, 22,22},
                    "items44":[]int{33, 33,33},
                },
            },
            []interface{}{
                map[string]interface{}{
                    "sku_id999":   "107680204947449999999",
                    "quantity999": 99999,
                    "quantity555": 5555,
                },
            },
        },
        //"adjust_stock_type": 2,
        "items02":[]int{111, 2222,333},
        "supplier02": "Auto_test's Super Store NEW02",
    }
    TT := map[string]interface{}{
        "adjust_stock_type": 1,
        "supplier": "Auto_test's Super Store",
        "remark": "Create stock_adjustments Auto_test",
        "items": []interface{}{
            map[string]interface{}{
                "sku_id": "10768020494744",
                "quantity": 1,
                "purchase_price": "9997",
                "items03":[]int{1, 2,3},
                "items04":map[string]interface{}{
                    "sku_id11": "10768020494744",
                    "quantity": 1,
                    "purchase_price": "9997",
                    "items03":[]int{1, 2,3},
                    "items04":[]int{1, 2,3},
                },
            },
            map[string]interface{}{
                "sku_id": "1076802049474499",
                "quantity": 3,
                //"purchase_price": "999711",
                "items33":[]int{1663, 2663,3663},
                "items44":map[string]interface{}{
                    "sku_id11": "107680204947448881113",
                    "quantity": 888,
                    "purchase_price": "11113",
                    "items33":[]int{222, 222,222},
                    "items44":[]int{333, 333,333},
                },
            },
            []interface{}{
                map[string]interface{}{
                    "sku_id999":   "107680204947449",
                    "quantity999": 22221,
                },
            },
        },
        "items02":[]int{1, 2,3},
    }
    res := MapSwitch(TTNEW, TT)
    resJson, _ := json.Marshal(res)
    fmt.Println("resJson:", string(resJson))
}

返回json

{
    "adjust_stock_type": 1,
    "items": [
        {
            "items03": [
                166,
                266,
                366
            ],
            "items04": {
                "items03": [
                    155,
                    255,
                    355
                ],
                "items04": [
                    1666,
                    2666,
                    3666
                ],
                "purchase_price": "9997888",
                "quantity": 88,
                "sku_id11": "10768020494744888"
            },
            "purchase_price": "9997",
            "quantity": 5,
            "sku_id": "1076802049474499"
        },
        {
            "items33": [
                166,
                266,
                366
            ],
            "items44": {
                "items33": [
                    22,
                    22,
                    22
                ],
                "items44": [
                    33,
                    33,
                    33
                ],
                "purchase_price": "11111",
                "quantity": 88,
                "sku_id11": "10768020494744888111"
            },
            "quantity": 1,
            "sku_id": "1076802049474499NEW"
        },
        {
            "quantity555": 5555,
            "quantity999": 99999,
            "sku_id999": "107680204947449999999"
        }
    ],
    "items02": [
        111,
        2222,
        333
    ],
    "remark": "Create stock_adjustments Auto_test NEW",
    "supplier": "Auto_test's Super Store NEW",
    "supplier02": "Auto_test's Super Store NEW02"
}

你可能感兴趣的:(go 2个多层嵌套的map取并集)