方法在Groovy中返回错误类型的值
我正在使用groovy方法查找自定义属性,并在找到键时返回值。方法在Groovy中返回错误类型的值,groovy,Groovy,我正在使用groovy方法查找自定义属性,并在找到键时返回值。 问题是该方法返回的是值的类型,而不是值 // There is more code before, but its not involved with this issue. def UUIDca = 'UUID' String customAttributeValue = grabCustomAttribute(UUIDca, event_work) appendLogfile("\n\
问题是该方法返回的是值的类型,而不是值
// There is more code before, but its not involved with this issue.
def UUIDca = 'UUID'
String customAttributeValue = grabCustomAttribute(UUIDca, event_work)
appendLogfile("\n\nTest grabCustomAttribute: ${customAttributeValue}\n")
}
// Grab the Custom Message Attribute values by name
String grabCustomAttribute (String findElement, OprEvent event){
appendLogfile("""\nIN grabCustomAttribute\nElement to look: ${findElement}\n""")
def firstItem = true
if (event.customAttributes?.customAttributes?.size()) {
event.customAttributes.customAttributes.each { ca ->
// Define each CMA to catch here
appendLogfile("""\nElement: ${ca.name} - """)
appendLogfile("""Valor: ${ca.value}\n""")
if ("${ca.name}" == findElement) {
String customValue = ca.value
appendLogfile("""Custom Attribute Found\n""")
appendLogfile(customValue)
return customValue
}
}
}
}
appendLogfile基本上是日志文件的打印:)这是我得到的输出 要查看的grabCustomAttribute元素中:UUID 元件:UUID-Valor:c3bb9169-0ca3-4bcf-beb1-f94eda8ebf1a
找到自定义属性
c3bb9169-0ca3-4bcf-beb1-f94eda8ebf1a Test grabCustomAttribute:[com.hp.opr.api.ws.model.event。OprCustomAttribute@940e503a] 它不返回值,而是返回对象的类型。这是正确的,但我正在寻找值。
我相信解决方案非常简单,但我对Groovy非常陌生 任何帮助都将不胜感激。
谢谢。在本例中,return语句用于闭包,而不是方法,因此您的方法实际上是返回“each”正在迭代的列表 这里可以采用的最简单的方法是使用Groovy find方法来查找正在搜索的元素。大概是这样的:
String grabCustomAttribute (String findElement, OprEvent event) {
return event.customAttributes.customAttributes?.find { ca -> ca.name == findElement }.value
}
它实际上返回了
每个迭代的列表