-
Notifications
You must be signed in to change notification settings - Fork 11
feat: adds fdv2 payload parsing and protocol handling #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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
6 changes: 5 additions & 1 deletion
6
lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/GsonHelpers.java
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
111 changes: 111 additions & 0 deletions
111
...ared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/DeleteObject.java
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,111 @@ | ||
| package com.launchdarkly.sdk.internal.fdv2.payloads; | ||
|
|
||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonToken; | ||
| import com.launchdarkly.sdk.json.SerializationException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Represents the delete-object event, which contains a payload object that should be deleted. | ||
| */ | ||
| public final class DeleteObject { | ||
| private final int version; | ||
| private final String kind; | ||
| private final String key; | ||
|
|
||
| /** | ||
| * Constructs a new DeleteObject. | ||
| * | ||
| * @param version the minimum payload version this change applies to | ||
| * @param kind the kind of object being deleted ("flag" or "segment") | ||
| * @param key the identifier of the object being deleted | ||
| */ | ||
| public DeleteObject(int version, String kind, String key) { | ||
| this.version = version; | ||
| this.kind = Objects.requireNonNull(kind, "kind"); | ||
| this.key = Objects.requireNonNull(key, "key"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the minimum payload version this change applies to. | ||
| * | ||
| * @return the version | ||
| */ | ||
| public int getVersion() { | ||
| return version; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the kind of the object being deleted ("flag" or "segment"). | ||
| * | ||
| * @return the kind | ||
| */ | ||
| public String getKind() { | ||
| return kind; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the identifier of the object being deleted. | ||
| * | ||
| * @return the key | ||
| */ | ||
| public String getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a DeleteObject from a JsonReader. | ||
| * | ||
| * @param reader the JSON reader | ||
| * @return the parsed DeleteObject | ||
| * @throws SerializationException if the JSON is invalid | ||
| */ | ||
| public static DeleteObject parse(JsonReader reader) throws SerializationException { | ||
| Integer version = null; | ||
| String kind = null; | ||
| String key = null; | ||
|
|
||
| try { | ||
| if (reader.peek() != JsonToken.BEGIN_OBJECT) { | ||
| throw new SerializationException("expected object"); | ||
| } | ||
| reader.beginObject(); | ||
|
|
||
| while (reader.peek() != JsonToken.END_OBJECT) { | ||
| String name = reader.nextName(); | ||
| switch (name) { | ||
| case "version": | ||
| version = reader.nextInt(); | ||
| break; | ||
| case "kind": | ||
| kind = reader.nextString(); | ||
| break; | ||
| case "key": | ||
| key = reader.nextString(); | ||
| break; | ||
| default: | ||
| reader.skipValue(); | ||
| break; | ||
| } | ||
| } | ||
| reader.endObject(); | ||
|
|
||
| if (version == null) { | ||
| throw new SerializationException("delete object missing required property 'version'"); | ||
| } | ||
| if (kind == null) { | ||
| throw new SerializationException("delete object missing required property 'kind'"); | ||
| } | ||
| if (key == null) { | ||
| throw new SerializationException("delete object missing required property 'key'"); | ||
| } | ||
|
|
||
| return new DeleteObject(version, kind, key); | ||
| } catch (IOException | RuntimeException e) { | ||
| throw new SerializationException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
91 changes: 91 additions & 0 deletions
91
lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/Error.java
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,91 @@ | ||
| package com.launchdarkly.sdk.internal.fdv2.payloads; | ||
|
|
||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonToken; | ||
| import com.launchdarkly.sdk.json.SerializationException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Represents the error event, which indicates an error encountered server-side affecting | ||
| * the payload transfer. SDKs must discard partially transferred data. The SDK remains | ||
| * connected and expects the server to recover. | ||
| */ | ||
| public final class Error { | ||
| private final String id; | ||
| private final String reason; | ||
|
|
||
| /** | ||
| * Constructs a new Error. | ||
| * | ||
| * @param id the unique string identifier of the entity the error relates to | ||
| * @param reason human-readable reason the error occurred | ||
| */ | ||
| public Error(String id, String reason) { | ||
| this.id = id; | ||
| this.reason = Objects.requireNonNull(reason, "reason"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the unique string identifier of the entity the error relates to. | ||
| * | ||
| * @return the identifier, or null if not present | ||
| */ | ||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the human-readable reason the error occurred. | ||
| * | ||
| * @return the reason | ||
| */ | ||
| public String getReason() { | ||
| return reason; | ||
| } | ||
|
|
||
| /** | ||
| * Parses an Error from a JsonReader. | ||
| * | ||
| * @param reader the JSON reader | ||
| * @return the parsed Error | ||
| * @throws SerializationException if the JSON is invalid | ||
| */ | ||
| public static Error parse(JsonReader reader) throws SerializationException { | ||
| String id = null; | ||
| String reason = null; | ||
|
|
||
| try { | ||
| if (reader.peek() != JsonToken.BEGIN_OBJECT) { | ||
| throw new SerializationException("expected object"); | ||
| } | ||
| reader.beginObject(); | ||
|
|
||
| while (reader.peek() != JsonToken.END_OBJECT) { | ||
| String name = reader.nextName(); | ||
| switch (name) { | ||
| case "id": | ||
| id = reader.nextString(); | ||
| break; | ||
| case "reason": | ||
| reason = reader.nextString(); | ||
| break; | ||
| default: | ||
| reader.skipValue(); | ||
| break; | ||
| } | ||
| } | ||
| reader.endObject(); | ||
|
|
||
| if (reason == null) { | ||
| throw new SerializationException("error missing required property 'reason'"); | ||
| } | ||
|
|
||
| return new Error(id, reason); | ||
| } catch (IOException | RuntimeException e) { | ||
| throw new SerializationException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
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.
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.
For other types I think this was handled by a type adapter factory?
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'm a dummy, I clicked merge before I saw this. Let me take a look.