-1

After unmarshalling two yaml files to two different maps, i wanted to compare keys (both outer and inner keys as it is a nested map) of both maps and if any key (outer or inner key) is present in first map 'configMap' and absent in second map 'userconfigMap', i wanted to append that key in second map in proper location. Tried iterating over the maps like this but not able to proceed with the implementation as im a newbie to golang.

for k, v := range configMap {
        for k1, v1 := range userconfigMap {
            if configMap[k] = userconfigMap[k1] {
                if configMap[k].(map[string]interface{})[v] = 
                userconfigMap[k1].(map[string]interface{})[v1] {

                }
                else {
                    userconfigMap[k1].append(v)
                }
            }
        }
    }

sample yaml files configMap yaml file:

config:
    test1: "test1"
    test2: "test2"
http_port: 30008
https_port: 32223

userconfigMap yaml file:

config:
    test1: "test1"
http_port: 30008
https_port: 32223

Im using map string interface for unmarshalling

sjsri
  • 63
  • 1
  • 8

1 Answers1

2

You can check if a map key exist in go with _, found := map[key], and add the key to the second map if not:

for k1 := range configMap {
    if _, found := userconfigMap[k1]; !found {
        userconfigMap[k1] = configMap[k1]
    }
    if v, ok := configMap[k1].(map[string]interface{}); ok {
        if _, ok := userconfigMap[k1].(map[string]interface{}); !ok {
            log.Fatal("userconfigMap[" + k1 + "] is not a map[string]interface{}")
        }
        for k2 := range v {
            if _, found := userconfigMap[k1].(map[string]interface{})[k2]; !found {
                userconfigMap[k1].(map[string]interface{})[k2] = v[k2]
            }
        }
    }
}
Fenistil
  • 3,734
  • 1
  • 27
  • 31
  • it is giving error for the below part of code for following variables: configMap[k1]: cannot range over it map of type interface, userconfigMap[k1][k2], configMap[k1][k2] cannot index over them map of type interface for k2 := range configMap[k1] { if _, found := userconfigMap[k1][k2]; !found { userconfigMap[k1][k2] = configMap[k1][k2] } } – sjsri May 05 '22 at 10:39
  • You don't specify the exact type of your maps. I used `map[string]map[string]interface{}` but based on the sample code you could achive the desired result. If not, please update and clarify your question, add sample data, working playground example and so on. – Fenistil May 05 '22 at 11:09
  • updated the question Fenistil – sjsri May 05 '22 at 12:27
  • @sjsri: I've updated the answer according to it. Now this use a `map[string]interface{}` as the configs. – Fenistil May 05 '22 at 12:40
  • Can u tell what the above code is doing like which key of what map is being taken up sequentially? It would be of great help Fenistil – sjsri May 09 '22 at 12:29
  • Ranging over a map in go is always in random order. – Fenistil May 09 '22 at 12:35
  • Any other way to update map, like converting it to some other data structure like slice, etc so that it is easier to iterate through nested keys? – sjsri May 09 '22 at 12:44