我目前已经初始化了一个新地图。在该地图中,我已经初始化了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
中提取repository
,branch
和Set
的值,然后运行这些值上的assert
。
我可以这样运行一个断言:
assert myMainMap.containsValue([[commit:"97sdf9s7fs7896fs0d7fs0", repository:"ssh://git@my-repo:5000/test/project", branch:"(7s8a6)"]])
,但这是Set上的断言,而不是键的单个值。
同样,这仅在键值位于""
而非[]
那么提取Map内部Set中的键值的最有效方法是什么?
答案 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')