-
Notifications
You must be signed in to change notification settings - Fork 535
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
Adding new methods to manage the path params map #2645
base: 4.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -613,7 +613,20 @@ default LanguageHeader preferredLanguage() { | |
} | ||
|
||
/** | ||
* Returns a map of named parameters as defined in path declaration with their actual values | ||
* Add a new one or replace an existing path parameter | ||
* @throws NullPointerException when the name or value is null | ||
*/ | ||
void addOrReplacePathParam(String name, String value); | ||
|
||
/** | ||
* Remove a path parameter. | ||
* | ||
* @return {@code true} when removed, {@code false} otherwise | ||
*/ | ||
boolean removePathParam(String s); | ||
|
||
/** | ||
* Returns an unmodifiable map of named parameters as defined in path declaration with their actual values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change that is not mandatory (even if I understand the intention). Can you please revert it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean that you prefer to expose it directly as it was? without being not modifiable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because we some users will expect this to be modifiable. |
||
* | ||
* @return the map of named parameters | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
import java.nio.charset.Charset; | ||
import java.util.*; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.BiConsumer; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.vertx.ext.web.handler.impl.SessionHandlerImpl.SESSION_USER_HOLDER_KEY; | ||
|
@@ -439,14 +440,41 @@ public void reroute(HttpMethod method, String path) { | |
restart(); | ||
} | ||
|
||
@Override | ||
public void addOrReplacePathParam(final String name, final String value) { | ||
Objects.requireNonNull(name, "name"); | ||
Objects.requireNonNull(value, "value"); | ||
getOrCreatePathParams().put(name, value); | ||
} | ||
|
||
@Override | ||
public Map<String, String> pathParams() { | ||
return getPathParams(); | ||
if (pathParams == null || pathParams.isEmpty()) { | ||
return Collections.emptyMap(); | ||
} | ||
return Collections.unmodifiableMap(pathParams); | ||
} | ||
|
||
@Override | ||
public boolean removePathParam(String s) { | ||
if (s == null) { | ||
return false; | ||
} | ||
if (pathParams != null) { | ||
return pathParams.remove(s) != null; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public @Nullable String pathParam(String name) { | ||
return getPathParams().get(name); | ||
if (name == null) { | ||
return null; | ||
} | ||
if (pathParams != null) { | ||
return pathParams.get(name); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
|
@@ -490,9 +518,10 @@ private MultiMap getQueryParams(Charset charset) { | |
return queryParams; | ||
} | ||
|
||
private Map<String, String> getPathParams() { | ||
private Map<String, String> getOrCreatePathParams() { | ||
if (pathParams == null) { | ||
pathParams = new HashMap<>(); | ||
// let's start small | ||
pathParams = new HashMap<>(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this too aggressive? How about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in theory with 1 it should have 2 as capacity - but yes, anything but the default can be a good improvement; given that it happen in our hot path, saving half array is still wellcome! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then, let's be a little conservative and go with 4. |
||
} | ||
return pathParams; | ||
} | ||
|
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.
Do we really need this new method? It will create the underlying map if needed anyway.
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.
good point; it was consistent to not expose the underlying form/nature/content of the map - in order to enable to not allocate it, if possible
But any suggestion is more than welcome, bud 🙏 and thanks for looking at this!