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

Variants: Implement toString #12138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rdblue
Copy link
Contributor

@rdblue rdblue commented Jan 31, 2025

This implements toString for Variant classes, which is more friendly when reading parameterized test cases.

@github-actions github-actions bot added the core label Jan 31, 2025
builder.append(", ");
}

builder.append(i).append(" => ").append(metadata.get(i));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Why not just use ":" here as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Basically we are creating an ID => object key string mapping. Seems fine to use ":" to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an object, so this isn't really a key. It's a mapping from ID.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I mean the the strings in the dictionary are the keys from the variant object.

Comment on lines +38 to +39
static String asString(VariantMetadata metadata) {
StringBuilder builder = new StringBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just add as regular method rather than static function.

That also will be consistent with other conversion like as asStructType() in Type.java.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be added in the interface because it would be a default that conflicts with an existing method (Object#toString). To share the implementations, I added these as static methods in the interfaces and then call them from the concrete classes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. We need to add as default. We are adding asString(), not toString(). Why will that conflict? I tried and seems that works.

@@ -38,4 +38,23 @@ default Variants.PhysicalType type() {
default VariantObject asObject() {
return this;
}

static String asString(VariantObject object) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Probably add as regular class method rather than static funciton.

}
}

static String asString(VariantPrimitive<?> primitive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Probably add as regular class method rather than static funciton.

builder.append(", ");
}

builder.append(i).append(" => ").append(metadata.get(i));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Basically we are creating an ID => object key string mapping. Seems fine to use ":" to be consistent.

@@ -210,4 +210,9 @@ public int writeTo(ByteBuffer outBuffer, int offset) {

throw new UnsupportedOperationException("Unsupported primitive type: " + type());
}

@Override
public String toString() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add basic test coverage for the changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are for convenience and are not part of a contract. I don't think it is worth adding tests.

Copy link
Contributor

@aihuaxu aihuaxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments. Otherwise, looks good to me.

Copy link
Member

@RussellSpitzer RussellSpitzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we had better string builders in Java because I would prefer something like

    StreamSupport.stream(object.fieldNames().spliterator(), false)
      .map(fieldName -> fieldName + ": " + object.get(fieldName) )
      .collect(Collectors.joining(","));

But that obviously wouldn't use the string builder for the internal strings and has to do the ugly stream support thing. So i'm good with this as is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants