在地图内访问集合中的值

时间:2018-08-16 00:35:53

标签: groovy jenkins-groovy

我目前已经初始化了一个新地图。在该地图中,我已经初始化了3套。

myMainMap = [disk:[], build:[], commits:[]]

我有一个从git获取某些值的函数,输出看起来像:

rev = 97sdf9s7fs7896fs0d7fs0
remoteUrl = ssh://git@my-repo:5000/test/project
branch = (7s8a6)

然后将其添加到commits中的集合myMainMap

def commitInfo = [repository: remoteUrl, commit: rev, branch: branch]
myMainMap['commits'].add(commitInfo)

所以现在myMainMap看起来像:

[images:[], buildEnv:[], commits:[[commit:97sdf9s7fs7896fs0d7fs0, repository:ssh://git@my-repo:5000/test/project, branch:(7s8a6)]]]

现在,我需要从位于地图中 commit中提取repositorybranchSet的值,然后运行这些值上的assert

我可以这样运行一个断言:

assert myMainMap.containsValue([[commit:"97sdf9s7fs7896fs0d7fs0", repository:"ssh://git@my-repo:5000/test/project", branch:"(7s8a6)"]])

,但这是Set上的断言,而不是键的单个值。 同样,这仅在键值位于""而非[]

内时有效

那么提取Map内部Set中的键值的最有效方法是什么?

1 个答案:

答案 0 :(得分:0)

请参见以下代码段的最后一行。它仅使用rev进行您想要的检查。

myMainMap.commits*.commit提供了所有rev哈希的列表。

这种Groovy的散布算子功能已在here中说明

def myMainMap = [disk:[], build:[], commits:[]]

def rev = '97sdf9s7fs7896fs0d7fs0'
def remoteUrl = 'ssh://git@my-repo:5000/test/project'
def branch = '(7s8a6)'

def commitInfo = [repository: remoteUrl, commit: rev, branch: branch]
myMainMap.commits.add(commitInfo)

//below line does the contains check in your commit list inside the map
assert myMainMap.commits*.commit.contains('97sdf9s7fs7896fs0d7fs0')
相关问题