-
Notifications
You must be signed in to change notification settings - Fork 2
Generate a _type attribute with the target class for min, max, range etc #98
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
Conversation
We are especially interested in the target types of numbers and datetime types if we want to simplify or optimise the min, max, range, size etc adapters as they can be created knowning the specific type that is being validated.
Sonatype Lift is retiringSonatype Lift will be retiring on Sep 12, 2023, with its analysis stopping on Aug 12, 2023. We understand that this news may come as a disappointment, and Sonatype is committed to helping you transition off it seamlessly. If you’d like to retain your data, please export your issues from the web console. |
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 think switch is cooler, but LGTM.
Though hmm, I would like to see at least one test for list/map cases |
I have the thought of translating the Class into a "well known String" and put that into int.class | Integer.class -> "Integer" ... and if it isn't one of these known types leave it out. So not populating _type for List & Map etc. |
not sure if I'm visualizing this correctly, as part of this may you modify either the time/number adapters to show what you mean? |
I also have a question about runtime checks, suppose somebody creates something like this:
for validating whether any of the java.time classes are in the past. We'd still need to check the actual type at runtime to do the validation no? |
…ring, CharSequence only at this stage)
Can't say I like the number of adapters we'll have to add, so I guess my real question is whether using the pattern-matching switch from JDK21 is not enough to optimize the instanceof checks. |
Yup. Far from ideal.
Or base a switch on the _type String, I wonder what that would look like ... |
More like: MaxKnownTypes(AdapterCreateRequest request) {
super(request);
this.targetType = request.targetType();
final var attributes = request.attributes();
this.value = (long) attributes.get("value");
}
@Override
public boolean isValid(Number number) {
// null values are valid
if (number == null) {
return true;
}
return switch (targetType) {
case "Integer", "Long", "Short", "Byte" -> number.longValue() <= value;
case "Double" -> compareDouble(number.doubleValue(), value, GREATER_THAN) <= 0;
case "Float" -> compareFloat((Float)number, value, GREATER_THAN) <= 0;
default -> throw new IllegalStateException();
};
} |
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.
still wanna do the pattern-matching switch, but I can work with this.
OptionalInt -> _type = "Integer" etc
We are especially interested in the target types of numbers and datetime types if we want to simplify or optimise the min, max, range, size etc adapters as they can be created knowning the specific type that is being validated.
For example:
The extra
_type
attributes here withint.class
andlong.class