-
Notifications
You must be signed in to change notification settings - Fork 119
chore: Centralise PageToken parsing #601
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package io.a2a.util; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import io.a2a.spec.InvalidParamsError; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Represents a pagination token for keyset-based pagination. | ||
| * <p> | ||
| * PageTokens use the format {@code "timestamp_millis:id"} where: | ||
| * <ul> | ||
| * <li>{@code timestamp_millis} - Unix timestamp in milliseconds (numeric)</li> | ||
| * <li>{@code id} - The entity identifier (String)</li> | ||
| * </ul> | ||
| * This format enables efficient keyset pagination by allowing queries to resume | ||
| * at a specific point in a timestamp-sorted, ID-secondary-sorted result set. | ||
| * | ||
| * @param timestamp The timestamp component of the page token | ||
| * @param id The identifier component of the page token | ||
| */ | ||
| public record PageToken(Instant timestamp, String id) { | ||
|
|
||
| /** | ||
| * Parses a pageToken string into a PageToken record. | ||
| * <p> | ||
| * Expected format: {@code "timestamp_millis:id"} | ||
| * | ||
| * @param tokenStr The pageToken string to parse, may be null or empty | ||
| * @return A PageToken instance, or null if tokenStr is null or empty | ||
| * @throws InvalidParamsError if the token format is invalid or timestamp is not numeric | ||
| */ | ||
| public static @Nullable PageToken fromString(@Nullable String tokenStr) { | ||
| if (tokenStr == null || tokenStr.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| String[] tokenParts = tokenStr.split(":", 2); | ||
| if (tokenParts.length != 2) { | ||
| throw new InvalidParamsError(null, | ||
| "Invalid pageToken format: expected 'timestamp:id'", null); | ||
| } | ||
|
|
||
| try { | ||
| long timestampMillis = Long.parseLong(tokenParts[0]); | ||
| String id = tokenParts[1]; | ||
| if (id.isEmpty()) { | ||
| throw new InvalidParamsError(null, "Invalid pageToken format: id part cannot be empty", null); | ||
| } | ||
| return new PageToken(Instant.ofEpochMilli(timestampMillis), id); | ||
| } catch (NumberFormatException e) { | ||
| throw new InvalidParamsError(null, | ||
| "Invalid pageToken format: timestamp must be numeric milliseconds", null); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts this PageToken to its string representation. | ||
| * <p> | ||
| * Format: {@code "timestamp_millis:id"} | ||
| * | ||
| * @return The pageToken string | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| return timestamp.toEpochMilli() + ":" + id; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.