ParameterValue.toXYZ() returns Object instead of primitive got numeric, boolean, chart#208
ParameterValue.toXYZ() returns Object instead of primitive got numeric, boolean, chart#208
Conversation
…c, boolean, chart
|
I think this will break some (all?) existing apps that use controllers with primitive types. public void getItems(int page, int size, boolean includeMetadata) {
}In the above example we'll get |
|
I have the same problem in if (getSettings().getWorkerThreads() > 0) {
builder.setWorkerThreads(getSettings().getWorkerThreads());
}I prefer this construction: if (getSettings().getWorkerThreads() != null) {
// I am sure that "workerThreads" doesn't exist in settings
...
}It's a little tricky to assign a default value for a null (missing) parameter/setting. From my point of view the code is more clean with object (Integer, Boolean) instead of primitives.
Sure, I see this thing and it's a problem for controllers. I don't know is it's not possible to make a compromise for controllers (the idea is to use |
|
I'd really prefer not to lose the simplicity of primitive values. Your proposed syntax: Long id = routeContext.getParameter("id").toLong();
// id is null if the parameter value is null or empty
if (id != null) {
// do my job
}My syntax counter-proposal: Long id = null;
if (routeContext.hasParameter("id")) { // empty value is acceptable
id = routeContext.getParameter("id").toLong(); // primitive auto-boxed to object
}
// OR
if (routeContext.hasParameterValue("id")) { // empty value is not acceptable
id = routeContext.getParameter("id").toLong(); // primitive auto-boxed to object
}
if (id != null) {
// do my job
}Looks like we need to fixup things like
Interesting. I have the exact opposite opinion. When reviewing code a deeper understanding of runtime behavior is required when you use the object peers of primitives and you introduce more surface area for NPEs. Switching to objects also adds value sanity checks in addition to normal range checks: if (getSettings().getWorkerThreads() != null) {
// I am sure that "workerThreads" doesn't exist in settings
if (getSettings().getWorkerThreads() > 0) {
// you can't assign 0 (or fewer) worker threads
...
}
} |
My goal is to simplify the parameter value read process for null or empty values.
Before this PR:
The problems with last snippet are:
isNull()from checkif (isNull()) { return defaultValue; }with a more comprehensiveisNullOrEmpty()With current PR we can write:
For my tests this PR does't not affect existing applications built with Pippo.
What do you think, brings value or not this PR?