lolno
lolno

Reputation: 3

Groovy collect(?) variables and values to map

I'm working on a piece of Groovy script that relies on values in 'fields' inherited from Java. I need to be able to reference this list of fields (and values) to create a map to work with. I'm struggling with a 'groovy' way of doing this.

What I have access to (inherited):

dialogPartyASelection_7 = 'Denied'
dialogPartyBSelection_7  = 'Accepted' 
dialogPartyASelection_6 = 'Denied' 
dialogPartyBSelection_6 = 'Accepted'
dialogPartyBSelection_5 = 'Denied'
dialogPartyASelection_5 = 'Accepted'
dialogPartyBSelection_4 = 'Denied'
dialogPartyASelection_4 = 'Accepted'
dialogPartyBSelection_3 = 'Denied'
dialogPartyASelection_3 = 'Accepted'
dialogPartyBSelection_2 = 'Denied'
dialogPartyASelection_2 = 'Accepted'
dialogPartyBSelection_1 = 'Denied'
dialogPartyASelection_1 = 'Accepted' 

What I'm trying to get:

 map = [
'dialogPartyASelection_7' : 'Denied',
'dialogPartyBSelection_7' : 'Accepted', 
'dialogPartyASelection_6' : 'Denied', 
'dialogPartyBSelection_6' : 'Accepted',
'dialogPartyBSelection_5' : 'Denied',
'dialogPartyASelection_5' : 'Accepted',
'dialogPartyBSelection_4' : 'Denied',
'dialogPartyASelection_4' : 'Accepted',
'dialogPartyBSelection_3' : 'Denied',
'dialogPartyASelection_3' : 'Accepted',
'dialogPartyBSelection_2' : 'Denied',
'dialogPartyASelection_2' : 'Accepted',
'dialogPartyBSelection_1' : 'Denied',
'dialogPartyASelection_1' : 'Accepted' ]

I'm 'setting my self up' as follows (these lists will be re-used throughout the script)

    def selectionsPartyA = [], selectionsPartyB = [], selections = [], PostSelections = [], PostChildrenSelections = [], 

PostSelections = [
                    '7',
                    '6',
                    '5',
                    '4',
                    '3',
                    '2',
                    '1'
                 ]                 

// selectionsPartyA
for (post in PostSelections) {
    selectionsPartyA += "dialogPartyASelection_"+post
    }

// selectionsPartyB
for (post in PostSelections) {
    selectionsPartyB += "dialogPartyBSelection_"+post
    }
selections.addAll(selectionsPartyB)  
selections.addAll(selectionsPartyA)

I have tried iterating using the lists of references I have, but was unable to reference the 'field' or 'value' as expected.

Upvotes: 0

Views: 718

Answers (2)

lolno
lolno

Reputation: 3

Thank you for your help, with @Steinar's suggestion I have been able to do this like below:

 def selectionsPartyA = [], selectionsPartyB = [], selections = [], PostChildrenSelections = [], 

PostSelections = ['7','6','5','4','3','2','1']                 

// selections
for (post in PostSelections) {
    selectionsPartyA += "dialogPartyASelection_"+post
    selectionsPartyB += "dialogPartyBSelection_"+post
    }
selections.addAll(selectionsPartyB)  
selections.addAll(selectionsPartyA)

dialogPartyASelection_7 = 'Denied'
dialogPartyBSelection_7  = 'Accepted' 
dialogPartyASelection_6 = 'Denied' 
dialogPartyBSelection_6 = 'Accepted'
dialogPartyBSelection_5 = 'Denied'
dialogPartyASelection_5 = 'Accepted'
dialogPartyBSelection_4 = 'Denied'
dialogPartyASelection_4 = 'Accepted'
dialogPartyBSelection_3 = 'Denied'
dialogPartyASelection_3 = 'Accepted'
dialogPartyBSelection_2 = 'Denied'
dialogPartyASelection_2 = 'Accepted'
dialogPartyBSelection_1 = 'Denied'
dialogPartyASelection_1 = 'Accepted' 

test = [:]
for (entry in selections){
test.put(entry, getProperty(entry))
}
assert test  == [dialogPartyBSelection_7:Accepted, dialogPartyBSelection_6:Accepted, dialogPartyBSelection_5:Denied, dialogPartyBSelection_4:Denied, dialogPartyBSelection_3:Denied, dialogPartyBSelection_2:Denied, dialogPartyBSelection_1:Denied, dialogPartyASelection_7:Denied, dialogPartyASelection_6:Denied, dialogPartyASelection_5:Accepted, dialogPartyASelection_4:Accepted, dialogPartyASelection_3:Accepted, dialogPartyASelection_2:Accepted, dialogPartyASelection_1:Accepted]

Upvotes: 0

koji
koji

Reputation: 179

like this?

class Test {
    String dialogPartyASelection_7 = 'Denied'
    String dialogPartyBSelection_7 = 'Accepted' 
    String dialogPartyASelection_6 = 'Denied' 
    String dialogPartyBSelection_6 = 'Accepted'
    String dialogPartyBSelection_5 = 'Denied'
    String dialogPartyASelection_5 = 'Accepted'
    String dialogPartyBSelection_4 = 'Denied'
    String dialogPartyASelection_4 = 'Accepted'
    String dialogPartyBSelection_3 = 'Denied'
    String dialogPartyASelection_3 = 'Accepted'
    String dialogPartyBSelection_2 = 'Denied'
    String dialogPartyASelection_2 = 'Accepted'
    String dialogPartyBSelection_1 = 'Denied'
    String dialogPartyASelection_1 = 'Accepted' 
}

def obj = new Test()
def propMap = obj.properties as Map
assert propMap['dialogPartyASelection_5'] == 'Accepted'

Upvotes: 2

Related Questions