简体   繁体   English

Groovy每个关闭错误

[英]Groovy each closure error

I know Java but completely new to Groovy. 我知道Java,但对Groovy来说是全新的。 I have got some legacy code in Groovy to work on. 我在Groovy中有一些遗留代码可以处理。

I have below method in Groovy: 我在Groovy中有以下方法:

def mapMyNotificationsByFruits(prefs,fruits) {
    def map = [:]
    prefs.each { MainNotification cn ->
        cn.fruits.each {
            MyNotification an ->
            def ex = map[(an.fruitId)]
            if (!ex) ex = []
            ex.add(an)
            map[(an.fruitId)] = ex
        }
    }
    log.info("map is: $map")
    return map
}

Above method gets called from another method as below: 上面的方法是从另一个方法调用的,如下所示:

def notificationPrefsByFruit = mapMyNotificationsByFruits(prefs, fruits)

When I debug on first line in mapMyNotificationsByFruits method, I get prefs as 当我在mapMyNotificationsByFruits方法的第一行上调试时,得到的prefs

MainNotification [someId=ABC123, email=abc@gmail.com, fruits=[{fruitId=XYZ123, someField=0}]]

On running this code, I get following error: 在运行此代码时,出现以下错误:

groovy.lang.MissingMethodException: No signature of method: com.somepackage.SomeClass$_mapMyNotificationsByFruits_closure5$_closure10.doCall() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[{fruitId=XYZ123, someField=0}]]

What is wrong here ? 这是怎么了

What does these line do: 这些行是做什么的:

MyNotification an ->
        def ex = map[(an.fruitId)]
        if (!ex) ex = []
        ex.add(an)
        map[(an.fruitId)] = ex

Is it a casting issue? 这是强制性的问题吗?

Replacing above lines with below code resolves the issue: 用下面的代码替换上面的行可以解决此问题:

MyNotification an = it
def ex = map[(an.fruitId)]
if (!ex) ex = []
ex.add(an)
map[(an.fruitId)] = ex

But I am not sure if both code blocks are same and I am fixing it correctly. 但是我不确定两个代码块是否相同并且我正在正确修复它。

Thanks in advance! 提前致谢!

What does the Algorithm do? 该算法做什么?
Given list of MainNotification instances it is grouping the MyNotification instances by fruitId . 给定MainNotification实例的列表, MainNotificationfruitIdMyNotification实例分组。

Given : 鉴于

[   
    [
        someId: "ABC123",
        email: "abc@gmail.com",
        fruits: [
            [fruitId : "XYZ123", someField: 0],
            [fruitId : "XYZ124", someField: 6],
        ]
    ],

    [
        someId: "XYSK",
        email: "yax@gmail.com",
        fruits: [
            [fruitId : "XYZ123", someField: 5],
            [fruitId : "XYZ124", someField: 2],
            [fruitId : "XYZ144", someField: 9],
        ]
    ]
]

Expected Output: 预期产量:

[
    XYZ123 : [
            [fruitId : "XYZ123", someField: 0],
            [fruitId : "XYZ123", someField: 5]
         ],
    XYZ124 : [
            [fruitId : "XYZ124", someField: 6],
            [fruitId : "XYZ124", someField: 2]
         ],
    XYZ144 : [ 
            [fruitId : "XYZ144", someField: 9]
         ]
]

Corrections: 更正:

  • fruits variable is never used on the method so please remove it. 水果变量从未在该方法上使用,因此请删除它。
  • The method is calling each method on the prefs variable so it is expecting prefs to be List<MainNotification> instead of MainNotification . 该方法在prefs变量上调用每个方法,因此期望prefs为List<MainNotification>而不是MainNotification
  • On the nested closure this code: cn.fruits.each is expecting each fruit to be instance of MyNotification ( this is the main fix to your issue btw ). 在嵌套闭包上,此代码:cn.fruits.each期望每个水果都是MyNotification实例( 这是对您的问题btw的主要修复 )。

You did not post the source code for MainNotification and MyNotification so per the input data you gave on your question I modeled them like this: 您没有发布MainNotificationMyNotification的源代码,因此根据您对问题提供的输入数据,我对它们进行了如下建模:

class MainNotification{
    String someId;
    String email;
    List<MyNotification> fruits;
}

class MyNotification{
    String fruitId;
    int someField;
}

With the corrections and assumptions here is the working code: 经过更正和假设,以下是工作代码:

def mapMyNotificationsByFruits(prefs,fruits) {
    def map = [:]
    prefs.each { MainNotification cn ->
        cn.fruits.each { MyNotification an ->
            def ex = map[(an.fruitId)]
            if (!ex) ex = []
            ex.add(an)
            map[(an.fruitId)] = ex
        }
    }
    println ("map is: $map")
    return map
}

MainNotification mainNotification = new MainNotification(someId: "ABC123",
        email: "abc@gmail.com",
        fruits: [new MyNotification(fruitId : "XYZ123", someField: 0)]
);

MyNotification fruits = null; //never used on the method

List<MainNotification>  prefs = [mainNotification];

def notificationPrefsByFruit = mapMyNotificationsByFruits(prefs, fruits)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM