如果地图列表有额外的键

时间:2016-07-04 08:25:09

标签: object arraylist groovy hashmap

我的问题与How to convert list of maps to list of objects几乎相似。

但现在问题是,我list maps maps Pojo包含一些额外的密钥,这些密钥在我的List list = [ [param1: "a", param2: ["a","b","c"], param3:[a:"a",b:"b",c:"c"], param4:true, param5:1, param6: "pamra6",param7: "pamra7"], [param1: "b", param2: ["d","e","f"], param3:[d:"d",e:"e",f:"f"], param4:false, param5:2, param6: "pamra6",param7: "pamra7"] ] 类中不存在,如下所示: -

list

在此param6两个额外密钥param7中,Pojo包含Pojo类中不存在的密钥,因为在我的方案中,我只考虑那些属于Pojo的属性,我无法增加list of maps类中的额外属性。

因此,当我将此list of objects转换为list.collect { new Pojo(it) } 时,如下所示: -

list.collect { it as  Pojo } 

它抛出一个错误: -

  

groovy.lang.MissingPropertyException:没有这样的属性:param6 for   class:Pojo

这绝对是正确的,但是当我按照以下方式进行转换时: -

list*.asType(Pojo)

.each { pojo ->

  pojo.param1
  ------------
  ------------
}

它不会抛出任何错误,但是当我们要获得这样的值时: -

null

无法找到任何这些价值。所有值均为list of objects

当我使用.dump()检查转换后的Pojo1_groovyProxy时,它会像代理对象一样转换为2016-07-01 13:23:32 DEBUG copy command: ssh-copy-id -i /yyy/.ssh/id_rsa.pub someuser@some.ip 2016-07-01 13:23:33 DEBUG first expect: 1 2016-07-01 13:23:33 DEBUG sending PASSW0RD 2016-07-01 13:23:33 DEBUG consuming output from remote side ... 2016-07-01 13:24:03 INFO Timeout occured ... stack trace information ... 2016-07-01 13:24:03 INFO Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1535, in expect_loop c = self.read_nonblocking(self.maxread, timeout) File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 968, in read_nonblocking raise TIMEOUT('Timeout exceeded.') pexpect.TIMEOUT: Timeout exceeded. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "xxx/PrepareSsh.py", line 28, in execute self.copy_keys(context, user, timeout) File "xxx/PrepareSsh.py", line 83, in copy_keys child.expect('[#\$]') File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1451, in expect timeout, searchwindowsize) File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1466, in expect_list timeout, searchwindowsize) File "/usr/local/lib/python3.5/site-packages/pexpect-3.3-py3.5.egg/pexpect/__init__.py", line 1568, in expect_loop raise TIMEOUT(str(err) + '\n' + str(self)) pexpect.TIMEOUT: Timeout exceeded. <pexpect.spawn object at 0x2b74694995c0> version: 3.3 command: /usr/bin/ssh-copy-id args: ['/usr/bin/ssh-copy-id', '-i', '/yyy/.ssh/id_rsa.pub', 'someuser@some.ip'] searcher: <pexpect.searcher_re object at 0x2b746ae1c748> buffer (last 100 chars): b'\r\n/usr/bin/xauth: creating new authorityy file /home/hmcmanager/.Xauthority\r\n' before (last 100 chars): b'\r\n/usr/bin/xauth: creating new authority file /home/hmcmanager/.Xauthority\r\n' after: <class 'pexpect.TIMEOUT'>

所以我的问题是,如何在这种情况下转换???

2 个答案:

答案 0 :(得分:1)

以下是一种方法(如果稍后将param6添加到班级中,则会尝试恢复弹性):

(编辑:更清洁,感谢tim_yates

class Pojo {
    def param1
    def param2
    def param3
    def param4
    def param5

    def static build(def map) {
        def fields = Pojo.declaredFields.findAll { !it.synthetic }*.name
        def keys = map.keySet().findAll { it in fields }
        def subMap = map.subMap(keys)
        new Pojo(subMap)
    }
}


def newList = list.collect { Pojo.build(it) }

答案 1 :(得分:0)

Michael Eastertim_yates建议后,使用以下方式实现此目的: -

class Pojo {
 def param1
 def param2
 def param3
 def param4
 def param5

def static build(def map) {
    new Pojo(map.findAll { k, v -> k in Pojo.metaClass.properties*.name})
  }
}

def newList = list.collect { Pojo.build(it) }

谢谢你给我一个大提示.. :)