-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
base: main
Are you sure you want to change the base?
Variants: Implement toString #12138
Conversation
builder.append(", "); | ||
} | ||
|
||
builder.append(i).append(" => ").append(metadata.get(i)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
static String asString(VariantMetadata metadata) { | ||
StringBuilder builder = new StringBuilder(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
There was a problem hiding this 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
This implements
toString
for Variant classes, which is more friendly when reading parameterized test cases.