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

Improve protovalidate overloads #36

Merged
merged 2 commits into from
Sep 12, 2023
Merged

Conversation

pkwarren
Copy link
Member

Update all of the protovalidate overload functions to add proper type checking and return Err.noSuchOverload if the types are incorrect. Greatly simplify the unique implementation as it didn't need to support non-list arguments. Fix a bug in the unique implementation for bytes (if the bytes contained invalid UTF-8). Add testcases for unique and additional tests for invalid arguments.

Update Format.format to use exceptions for control flow instead of having to distinguish Val.Err from other types (a lot of places were just returning null).

Remove all places where we used '==' to compare object type instances with comparing using enum types (which is safe for '==' comparison).

Update all of the protovalidate overload functions to add proper type
checking and return Err.noSuchOverload if the types are incorrect.
Greatly simplify the unique implementation as it didn't need to support
non-list arguments. Fix a bug in the unique implementation for bytes (if
the bytes contained invalid UTF-8). Add testcases for unique and
additional tests for invalid arguments.

Update Format.format to use exceptions for control flow instead of
having to distinguish Val.Err from other types (a lot of places were
just returning null).

Remove all places where we used '==' to compare object type instances
with comparing using enum types (which is safe for '==' comparison).
@pkwarren pkwarren requested a review from rodaine September 12, 2023 21:25
try {
return StringT.stringOf(Format.format(formatString, list));
} catch (Err.ErrException e) {
return e.getErr();
Copy link
Member Author

Choose a reason for hiding this comment

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

Updated this to use exceptions for control flow instead of dealing with interpreting the returned Val type (either Err or String).

case Int:
case String:
case Uint:
return unaryOpForPrimitiveVal(val).invoke(val);
Copy link
Member Author

Choose a reason for hiding this comment

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

A lot of these weren't valid for unique() - it should only operate on list types according to the Go implementation.

Val val = list.get(IntT.intOf(i));
if (exist.contains(val)) {
if (!exist.add(val)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The add() method returns false if the value is already contained in the set.

for (int i = 0; i < list.size().intValue(); i++) {
Object val = list.get(IntT.intOf(i)).value();
if (val instanceof byte[]) {
val = new String((byte[]) val, StandardCharsets.UTF_8);
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the source of a bug if the bytes contained invalid UTF-8. If so, the string would contain replacement characters for invalid bytes and two different bytes values would return equal.

return Err.newErr("format: unparsable format specifier %s", c);
}
if (status.type() == Err.ErrType) {
return status;
Copy link
Member Author

Choose a reason for hiding this comment

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

A lot of the complexity in here went away with switching to exceptions for control flow.

// Previously, the unique() method returned false here as both bytes were converted
// to UTF-8. Since both contain invalid UTF-8, this would lead to them treated as equal
// because they'd have the same substitution character.
.put("[b'\\xFF', b'\\xFE'].unique()", true)
Copy link
Member Author

Choose a reason for hiding this comment

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

This failed before fixing the unique implementation (this test doesn't exist in the Go implementation but it doesn't suffer the same issues).

@pkwarren pkwarren merged commit 841e8e4 into main Sep 12, 2023
@pkwarren pkwarren deleted the pkw/improve-protovalidate-overloads branch September 12, 2023 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants