You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Donny (JIRA)" <ji...@apache.org> on 2016/04/11 07:52:25 UTC

[jira] [Comment Edited] (GROOVY-7809) Create a map from selected properties of an Object

    [ https://issues.apache.org/jira/browse/GROOVY-7809?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15234518#comment-15234518 ] 

Donny edited comment on GROOVY-7809 at 4/11/16 5:52 AM:
--------------------------------------------------------

Hi Paul,

Yes, You're right. 

Perhaps 'Shortcut for creating map from an object' would be a better summary for this ticket.

Earlier I was thinking of having something like:
{code:title=Example|theme=FadeToGrey|linenumbers=true|language=groovy|firstline=0001|collapse=false}
def heroNoWeakness = hero.with{['name', 'strength', 'constitution']}
{code}

or maybe:
{code:title=Example|theme=FadeToGrey|linenumbers=true|language=groovy|firstline=0001|collapse=false}
def heroNoWeakness = hero.without{['weakness']}
{code}

Donny


was (Author: dodol):
Hi Paul,

Yes, You're right. 

Perhaps 'Shortcut for creating map from an object' would be a better summary for this ticket.

Earlier I was thinking of having something like:

def heroNoWeakness = hero.with{['name', 'strength', 'constitution']}

or maybe:

def heroNoWeakness = hero.without{['weakness']}

Donny

> Create a map from selected properties of an Object
> --------------------------------------------------
>
>                 Key: GROOVY-7809
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7809
>             Project: Groovy
>          Issue Type: New Feature
>          Components: syntax
>            Reporter: Donny
>            Priority: Minor
>
> It would be nice if Groovy allows developers to create a map from selected properties of an Object.
> Below are the example:
> {code:title=Example|theme=FadeToGrey|linenumbers=true|language=groovy|firstline=0001|collapse=false}
> class Hero {
>     String name
>     Integer strength
>     Integer constitution
>     String weakness
> }
> Hero hero = new Hero(name: "Robin", strength: 70, constitution: 80, weakness: "Beautiful Women")
> def mapNoWeakness = hero.asMap(['name', 'strength', 'constitution'])
> println mapNoWeakness // [name: 'Robin', strength: 70, constitution: 80]
> def mapOnlyName = hero.asMap(['name'])
> println mapOnlyName // [name: 'Robin']
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)