我有两个清单:
def ids = [1L, 2L, 3L]
def values = [11, 12, 13]
Run Code Online (Sandbox Code Playgroud)
我想创建一个HashMapwith idsas 键和valuesas 值。
我尝试使用transpose但坚持使用GroovyCastException
我有一个具有以下代码的常规文件:
@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
我正在将电子邮件模板从 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 模型?到目前为止,我看到的唯一解决方法(肮脏的)是以这种嵌套对象形式传递模板模型,但我已经发现它不适用于所有情况。提前致谢,任何帮助表示赞赏。
有人可以向我解释为什么在声明中使用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) 我有一个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) 我有一个像下面的詹金斯工作习惯脚本。我希望从参数值中获取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) 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 中看到此方法。我们如何手动将此方法列入白名单或通过任何其他解决方案来解决此问题?
我需要使用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值.
我在下面有一个非常简单的方法。它调用另一种方法来软删除 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) 我正在试验递归:
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)
如果我像这样使用它,我会得到这个:
在 …
groovy ×8
grails ×2
hashmap ×2
jenkins ×2
collections ×1
concurrency ×1
dependencies ×1
github ×1
groovyshell ×1
httpbuilder ×1
httpclient ×1
java ×1
java-8 ×1
mustache ×1
postmark ×1
recursion ×1
spock ×1
trampolines ×1
unit-testing ×1