[ 
https://issues.apache.org/jira/browse/GROOVY-10638?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Dmitry Lukyanov updated GROOVY-10638:
-------------------------------------
    Description: 
Goal: have ability to customize representation of custom Map or Collection in 
GString

Code:
{code:java}
@groovy.transform.CompileStatic
class T extends HashMap {
  @Override
  String toString() { "foo" }
}

def t = new T()
println t.toString()
println "${t}"
{code}
Output:
{code:java}
foo
[:]
{code}
----
It's clear that we could force {{toString()}} inside GString to render map in 
custom way but I would like to define object visual representation in it's 
implementation instead of doing it in every GString...
----
Suggesting the following changes

Here is the point where we are choosing how object will be rendered in GString:
[https://github.com/apache/groovy/blob/9eb74c2ee938204030bae1bb8ab7eaae77687ab0/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java#L459]
{code:java}
    public static void write(Writer out, Object object) throws IOException {
        if (object instanceof String) {
            out.write((String) object);
        } else if (object instanceof Object[]) {
            out.write(toArrayString((Object[]) object));
        } else if (object instanceof Map) {
            out.write(toMapString((Map) object));
        } else if (object instanceof Collection) {
            out.write(toListString((Collection) object));
        } else if (object instanceof Writable) {
            Writable writable = (Writable) object;
            writable.writeTo(out);
        } else if ...
{code}
what if we move
{code:java}
if (object instanceof Writable)
{code}
before {{Map}} and {{Collection}} ?

in this case we will allow developers to implement custom representation of Map 
and Collection inside GString by implementing Writable interface...

----
related questions:

* 
https://stackoverflow.com/questions/45383815/groovy-gstring-rendering-does-not-call-overridden-tostring-method-when-parent
* 
https://stackoverflow.com/questions/72194058/can-a-groovy-gstring-honor-an-overloaded-map-tostring-implementation

  was:
Goal: have ability to customize representation of custom Map or Collection in 
GString

Code:

{code:java}
@groovy.transform.CompileStatic
class T extends HashMap {
  @Override
  String toString() { "foo" }
}

def t = new T()
println t.toString()
println "${t}"
{code}

Output:

{code:java}
foo
[:]
{code}


----

It's clear that we could force {{toString()}} inside GString to render map in 
custom way but I would like to define object visual representation in it's 
implementation instead of doing it in every GString...

----

Suggesting the following changes

Here is the point where we are choosing how object will be rendered in GString:
https://github.com/apache/groovy/blob/9eb74c2ee938204030bae1bb8ab7eaae77687ab0/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java#L459

{code:java}
    public static void write(Writer out, Object object) throws IOException {
        if (object instanceof String) {
            out.write((String) object);
        } else if (object instanceof Object[]) {
            out.write(toArrayString((Object[]) object));
        } else if (object instanceof Map) {
            out.write(toMapString((Map) object));
        } else if (object instanceof Collection) {
            out.write(toListString((Collection) object));
        } else if (object instanceof Writable) {
            Writable writable = (Writable) object;
            writable.writeTo(out);
        } else if ...
{code}

what if we move 
{code:java}
if (object instanceof Writable)
{code}
before {{Map}} and {{Collection}} ?

in this case we will allow developers to implement custom representation of Map 
and Collection inside GString by implementing Writable interface...


> allow custom Map and Collection representation in GString
> ---------------------------------------------------------
>
>                 Key: GROOVY-10638
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10638
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Dmitry Lukyanov
>            Priority: Major
>
> Goal: have ability to customize representation of custom Map or Collection in 
> GString
> Code:
> {code:java}
> @groovy.transform.CompileStatic
> class T extends HashMap {
>   @Override
>   String toString() { "foo" }
> }
> def t = new T()
> println t.toString()
> println "${t}"
> {code}
> Output:
> {code:java}
> foo
> [:]
> {code}
> ----
> It's clear that we could force {{toString()}} inside GString to render map in 
> custom way but I would like to define object visual representation in it's 
> implementation instead of doing it in every GString...
> ----
> Suggesting the following changes
> Here is the point where we are choosing how object will be rendered in 
> GString:
> [https://github.com/apache/groovy/blob/9eb74c2ee938204030bae1bb8ab7eaae77687ab0/src/main/java/org/codehaus/groovy/runtime/FormatHelper.java#L459]
> {code:java}
>     public static void write(Writer out, Object object) throws IOException {
>         if (object instanceof String) {
>             out.write((String) object);
>         } else if (object instanceof Object[]) {
>             out.write(toArrayString((Object[]) object));
>         } else if (object instanceof Map) {
>             out.write(toMapString((Map) object));
>         } else if (object instanceof Collection) {
>             out.write(toListString((Collection) object));
>         } else if (object instanceof Writable) {
>             Writable writable = (Writable) object;
>             writable.writeTo(out);
>         } else if ...
> {code}
> what if we move
> {code:java}
> if (object instanceof Writable)
> {code}
> before {{Map}} and {{Collection}} ?
> in this case we will allow developers to implement custom representation of 
> Map and Collection inside GString by implementing Writable interface...
> ----
> related questions:
> * 
> https://stackoverflow.com/questions/45383815/groovy-gstring-rendering-does-not-call-overridden-tostring-method-when-parent
> * 
> https://stackoverflow.com/questions/72194058/can-a-groovy-gstring-honor-an-overloaded-map-tostring-implementation



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to