Search code examples
javadictionarygrailsgroovytransform

reconf map 3 level in Groovy 2.4.7


Need to convert map in Groovy 2.4.7:

[
  1:[
    Par1:[
      Podpar1:[s:str1, time:16],
      Podpar2:[s:str2, time:16]
    ],
    Par2:[
      Podpar1:[s:0, time:16],
      Podpar2:[s:0, time:16]
    ]
  ],
  2:[
    Par1:[
      Podpar1:[s:str3, time:16],
      Podpar2:[s:str4, time:16]
    ],
    Par2:[
      Podpar1:[s:0, time:16],
      Podpar2:[s:0, time:16]
    ]
  ]
]

to:

[
  1:[
    Podpar1:[
      Par1:[s:str1, time:16],
      Par2:[s:0, time:16]
    ],
    Podpar2:[
      Par1:[s:str2, time:16],
      Par2:[s:0, time:16]
    ]
  ],
  2:[
    Podpar1:[
      Par1:[s:str3, time:16],
      Par2:[s:0, time:16]
    ],
    Podpar2:[
      Par1:[s:str4, time:16],
      Par2:[s:0, time:16]
    ]
  ]
]

Code which i try to use is:

def res =  [ "1" : [ "Par1" : [ "Podpar1" : [ "s" : "str" , "time" : 16 ] , "Podpar2" : [ "s" : "str" , "time" : 16 ]],
                          "Par2" : ["Podpar1" : ["s" : 0 ,"time" : 16] , "Podpar2" : ["s" : 0 , "time" : 16 ]],
                  "2" : ["Par1" : ["Podpar1" : ["s" : "str1" ,"time" : 16] ,"Podpar2" : ["s" : "str1" ,"time" : 16]]],
                         "Par2" : ["Podpar1" : ["s" : 0 ,"time" : 16 ] ,"Podpar2" : ["s" : 0 ,"time" : 16]]]]

def reconfMap = [:]
res.each { k, v ->
    reconfMap[k] = [:]
    def temppar = [:]
    def temppodpar = [:]
    v.each { park, parv ->
        parv.each { podpark, podparv ->
            temppar[park]=podparv
            temppodpar[podpark]= temppar
            reconfMap[k] = temppodpar
        }
    }
}

but all elements of map are replacing by last element. Can anyone help me?

Given result:

[1:[Podpar1:[Par1:[s:str2, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str2, time:16], Par2:[s:0, time:16]]], 2:[Podpar1:[Par1:[s:str4, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str4, time:16], Par2:[s:0, time:16]]]]

Expected result:

[1:[Podpar1:[Par1:[s:str1, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str2, time:16], Par2:[s:0, time:16]]], 2:[Podpar1:[Par1:[s:str3, time:16], Par2:[s:0, time:16]], Podpar2:[Par1:[s:str4, time:16], Par2:[s:0, time:16]]]]


Solution

  • What a confusing example 😉

    I think this should do what you want though:

    def res =  [
      1:[
        Par1:[
          Podpar1:[s:'str1', time:16],
          Podpar2:[s:'str2', time:17]
        ],
        Par2:[
          Podpar1:[s:0, time:18],
          Podpar2:[s:0, time:19]
        ]
      ],
      2:[
        Par1:[
          Podpar1:[s:'str3', time:20],
          Podpar2:[s:'str4', time:21]
        ],
        Par2:[
          Podpar1:[s:0, time:22],
          Podpar2:[s:0, time:23]
        ]
      ]
    ]
    
    
    def result = res.collectEntries { numberKey, list ->
        def munged = list.inject([:]) { accumulator, parKey, podparList -> 
            podparList.each { podParKey, values ->
                accumulator[podParKey] = (accumulator[podParKey] ?: [:]) + [(parKey): values] 
            }
            accumulator
        }
        [numberKey, munged]
    }