You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Dmitry Lukyanov (Jira)" <ji...@apache.org> on 2022/05/27 05:08:00 UTC
[jira] [Created] (GROOVY-10638) allow custom Map and Collection representation in GString
Dmitry Lukyanov created GROOVY-10638:
----------------------------------------
Summary: 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
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...
--
This message was sent by Atlassian Jira
(v8.20.7#820007)