Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline-friendly toString() #730

Open
cpovirk opened this issue May 8, 2019 · 6 comments
Open

Multiline-friendly toString() #730

cpovirk opened this issue May 8, 2019 · 6 comments
Labels
P3 type=enhancement Make an existing feature better

Comments

@cpovirk
Copy link
Member

cpovirk commented May 8, 2019

One of the things we did in Truth a while back was to print values differently if they contain newlines. So, for example, you would see:

expected: foo
but was : bar

But you'd see:

expected:
    public class Foo {
      Foo() {}
    }
but was:
    public class Bar {
      Bar() {}
    }

I would speculate (but it's just speculation) that this might be a nice feature for AutoValue toString() implementations, too.

(Even in the case in which fields aren't multiline, a multiline toString() can be nice in some cases: I think Truth has gotten reports that AutoValue toString() (like, to be fair, almost all toString() implementations) makes it hard to see which field differs when there are a lot of fields. But of course one-line toString() is nice in plenty of cases, too, so I wouldn't advocate for always going multiline (nor, probably, for making it configurable). It's just a nice additional advantage in the cases in which multiline is already justified.)

(It's also possible that Truth should have more special handling of AutoValue types in some cases.)

@cpovirk
Copy link
Member Author

cpovirk commented May 8, 2019

(Naturally this complicates the implementation....)

@eamonnmcmanus
Copy link
Member

I'm not finding this very clear. Could you give an example of what AutoValue does now and what you would like it to do?

@cpovirk
Copy link
Member Author

cpovirk commented May 9, 2019

Sorry about that.

Here's an example toString() from the tests:

Simple{publicString=example, protectedInt=23, packageMap={twenty-three=23}}

If a value has a newline, we end up with something like:

Simple{publicString=Here's another example.
It contains multiple lines., protectedInt=23, packageMap={twenty-three=23}}

In the later case, I'm proposing something more like:

Simple{
  publicString:
    Here's another example.
    It contains multiple lines.
  protectedInt:
    23
  packageMap:
    {twenty-three=23}
}

@eamonnmcmanus
Copy link
Member

Thanks for the clarification!

We've always made AutoValue-generated code standalone, but this sounds as if it would require a support library. Especially if we do things properly, for example indenting further if there is a nested AutoValue object that also has newlines. Perhaps we could instead have a library somewhere, maybe even in Guava, that people could use in an explicit toString()? Of course AutoValue won't override toString() if there already is one.

@cpovirk
Copy link
Member Author

cpovirk commented May 9, 2019

Yeah, this could be a ToStringHelper feature request (though we sorta kinda commit to a format in its docs, so maybe this would have to be opt-in). That said, I suspect (without doing any research) that ToStringHelper is fairly rarely used now that AutoValue is around and that users usually wouldn't care enough to override AutoValue's toString() if it meant listing all their fields again (which might go out of date).

I don't think the code required for this is too complex -- roughly this (though that assumes that we've computed -- and perhaps stored in a List -- all the toString() representations ahead of time).

@cpovirk
Copy link
Member Author

cpovirk commented Apr 14, 2022

(@ToPrettyString happened at some point. That isn't the sort of automatic formatting change that I was thinking about, but it's a way of improving upon long, single-line toString() output, so I'm noting it here.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

5 participants