小编Szy*_*iak的帖子

在 groovy 中创建一个具有两个集合的地图 - 带有键和值

我有两个清单:

def ids = [1L, 2L, 3L]
def values = [11, 12, 13]
Run Code Online (Sandbox Code Playgroud)

我想创建一个HashMapwith idsas 键和valuesas 值。

我尝试使用transpose但坚持使用GroovyCastException

collections groovy hashmap

4
推荐指数
1
解决办法
535
查看次数

抓取葡萄时出错-找不到依赖关系

我有一个具有以下代码的常规文件:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
Run Code Online (Sandbox Code Playgroud)

我正在尝试下载要使用的依赖项HTTPBuilder

但这向我显示了以下错误:

Error grabbing Grapes -- [download failed: org.apache.httpcomponents#httpclient;4.2.1!httpclient.jar,download failed: org.apache.httpcomponents#httpcore;4.2.1!httpcore.jar, download failed: commons-codec#commons-codec;1.6!commons-codec.jar, download failed: commons-beanutils#commons-beanutils;1.8.0!commons-beanutils.jar, download failed: commons-lang#commons-lang;2.4!commons-lang.jar]
Run Code Online (Sandbox Code Playgroud)

我尝试在命令提示符下使用以下命令解决此Grape依赖项:

grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.2
Run Code Online (Sandbox Code Playgroud)

以及尝试以下方法:

grape install org.codehaus.groovy.modules.http-builder http-builder 0.7.2
Run Code Online (Sandbox Code Playgroud)

然后它也向我显示了相同的错误。您能帮我解决这个问题吗?我使用Groovy版本2.2.2

grails groovy dependencies httpclient httpbuilder

3
推荐指数
1
解决办法
1万
查看次数

Mustachio:没有作用域的 if 语句 - 有可能吗?

我正在将电子邮件模板从 Mandrill 移至 Postmark,这需要将 Handlebars 转换为 Mustachio。在 Handlebars 我有这样的事情:

{{#if some_variable}}
<p>This text uses variable: {{some_variable}}
{{/if}}
Run Code Online (Sandbox Code Playgroud)

根据 Mustache 文档,转换后应该是这样的:

{{#some_variable}}
<p>This text uses variable: {{some_variable}}
{{/some_variable}}
Run Code Online (Sandbox Code Playgroud)

问题是 Postmark 的 Mustachio 使用范围(https://github.com/wildbit/mustachio/wiki#scoping),因此在这种情况下,它需要以下 JSON 模型:

{
    "some_variable": {
        "some_variable": "some_variable_value"
    }
}
Run Code Online (Sandbox Code Playgroud)

代替

{
    "some_variable": "some_variable_value"
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何关闭 Mustachio 的范围,因此它使用预期的示例性 JSON 模型?到目前为止,我看到的唯一解决方法(肮脏的)是以这种嵌套对象形式传递模板模型,但我已经发现它不适用于所有情况。提前致谢,任何帮助表示赞赏。

postmark mustache

3
推荐指数
1
解决办法
1263
查看次数

使用@Field注释关闭Groovy 2.4变量范围

有人可以向我解释为什么在声明中使用closure2时initVars('c')无法修改引用的对象@Field

import groovy.transform.Field;

@Field def lines4 = "a";

void initVars(String pref){
    println('init:'+lines4+'  '+pref)      //*3.init:a  b   *7.init:b  c
    lines4 = pref;  
}
println("closure1")    ///1. closure1
1.times {
    println(lines4)    ///2. a
    initVars('b')      ///3. init:a  b
    lines4 += 'p1'
    println(lines4)    ///4. bp1
}
println("closure2")    ///5. closure2
1.times {
    println(lines4)    ///6. bp1
    initVars('c')      ///7. init:b  c
    println(lines4)    ///8. bp1     Why not c
    lines4 += 'q1'
    println(lines4)    ///9. bp1q1   Why not cq1
}
Run Code Online (Sandbox Code Playgroud)

输出:

C:\projects\ATT>groovy test.groovy
1. closure1
2. a
3. …
Run Code Online (Sandbox Code Playgroud)

groovy

3
推荐指数
1
解决办法
2466
查看次数

使用JSON ARRAY(地图列表)中的特定键值对查找/过滤列表

我有一个JSON数组(地图列表)类似于:

def listOfMap = [[TESTCASE:1, METHOD:'CLICK', RESULT:'PASS'], 
      [TESTCASE:2, METHOD:'CLICK', RESULT:'FAIL'], 
      [TESTCASE:3, METHOD:'CLICK', RESULT:'FAIL'], 
      [TESTCASE:4, METHOD:'TYPETEXT', RESULT:'FAIL']]
Run Code Online (Sandbox Code Playgroud)

1)我想获取/过滤/返回包含键值对" METHOD:CLICK "和" RESULT:FAIL "的所有列表

My output should return 2 lists out of 4: [TESTCASE:2, METHOD:CLICK, RESULT:FAIL], [TESTCASE:3, METHOD:CLICK, RESULT:FAIL]
Run Code Online (Sandbox Code Playgroud)

2)我想获得包含键值对" METHOD:CLICK "和" RESULT:FAIL " 的列表计数

My output should be : 2 
Run Code Online (Sandbox Code Playgroud)

3)从上面的地图列表中,我想获得关键字" METHOD "的所有唯一/不同值

My output should return unique values of the key method : CLICK, TYPETEXT
Run Code Online (Sandbox Code Playgroud)

groovy groovyshell groovy-console jenkins-groovy

3
推荐指数
1
解决办法
2556
查看次数

如何从参数值替换jenkins job scm中的github分支

我有一个像下面的詹金斯工作习惯脚本。我希望从参数值中获取Github分支。

Groovy脚本:

 git_url = "git@github.deere.com:ABC/XYZ.git" jenkins_node = "master"
    freeStyleJob('myjob') {
    logRotator(numToKeep = 100)
    parameters {    stringParam("GIT_BRANCH", "master" , "master cert dev")   }
    label(jenkins_node)
    scm {
     git {
      remote { url(git_url) }
      branch($GIT_BRANCH)  
      extensions { }
         }
     }
Run Code Online (Sandbox Code Playgroud)

groovy github jenkins jenkins-job-dsl

3
推荐指数
1
解决办法
587
查看次数

Jenkins 管道抛出报告:org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException

Jenkins 管道抛出报告:org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:不允许脚本使用方法 groovy.util.XmlSlurper parseText java.lang.String

这是代码:

def testsuites = new XmlSlurper().parseText(xml)
Run Code Online (Sandbox Code Playgroud)

我也无法在 ScriptApproval 中看到此方法。我们如何手动将此方法列入白名单或通过任何其他解决方案来解决此问题?

groovy jenkins jenkins-plugins jenkins-pipeline

3
推荐指数
1
解决办法
9692
查看次数

在并行流中的hashmap中插入值时的线程安全性

我需要使用10秒的超时进行异步调用,并且需要对映射中的每个元素执行此操作.异步调用的结果存储在另一个映射中.HashMap在这种情况下使用是否安全或我需要使用ConcurrentMap

Map<String, String> x = ArrayListMultimap.create();
Map<String, Boolean> value = Maps.newHashMap();

x.keySet().paralleStream().forEach(req -> {
   try {
      Response response = getResponseForRequest(req);
      value.put(req, response.getTitle());
   } catch(TimeoutException e) {
      value.put(req, null);
   }
}
Run Code Online (Sandbox Code Playgroud)

这个线程安全吗?我无法理解.我知道另一种方法是创建一个并发的hashmap,并考虑一些其他填充值而不是null,因为Concurrent map不支持null值.

java concurrency hashmap concurrenthashmap java-8

3
推荐指数
1
解决办法
363
查看次数

基于 Spock 交互的测试:对方法的调用太少

我在下面有一个非常简单的方法。它调用另一种方法来软删除 API 密钥,然后调用另一种方法来创建一个新的并返回它。

测试也在下面,它只是检查这两个方法是否被正确调用。但是仍然在两种方法上都得到 0 调用错误。是什么导致了这个问题?

AuthApiKeyPair updateApiKeyPair(AuthApiKeyPair apiKeyPair, Boolean createNewKey) {

    AuthApiKeyPair newKeyPair

    if (createNewKey) {
        deleteApiKeyPair(apiKeyPair)

        //The key will be created with the same info as the previous key.
        newKeyPair = createApiKeyPair(apiKeyPair.label, apiKeyPair.accountMode, apiKeyPair.source)
    }

    newKeyPair
}
Run Code Online (Sandbox Code Playgroud)

测试:

def "should soft delete key pair and create new one"() {
    setup:
    AuthApiKeyPair apiKeyPair = AuthApiKeyPair.build(acquirerId: 123, source: PaymentSource.BOARDING_API, label: 'Boarding API key')

    when:
    service.updateApiKeyPair(apiKeyPair, true)

    then:
    1 * service.deleteApiKeyPair(apiKeyPair)
    1 * service.createApiKeyPair(apiKeyPair.label, apiKeyPair.accountMode, apiKeyPair.source)
}
Run Code Online (Sandbox Code Playgroud)

grails groovy unit-testing spock

3
推荐指数
1
解决办法
1746
查看次数

Groovy 的 trampoline() 使递归执行速度慢得多 - 为什么?

我正在试验递归:

def fac
//fac = { int curr, res = 1G -> 1 >= curr ? res : fac( curr - 1, res * curr ) }
fac = { int curr, res = 1G -> 1 >= curr ? res : fac.trampoline( curr - 1, res * curr ) }
fac = fac.trampoline()

def rnd = new Random()

long s = System.currentTimeMillis()

100000.times{ fac rnd.nextInt( 40 ) }

println "done in ${System.currentTimeMillis() - s} ms / ${fac(40)}"
Run Code Online (Sandbox Code Playgroud)

如果我像这样使用它,我会得到这个:

在 …

recursion groovy trampolines

3
推荐指数
1
解决办法
267
查看次数