-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Common code to deserialize a state message in the new format #13772
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6afbdbe
Common code to deserialize a state message in the new format
benmoriceau ee7fd54
PR comments and type changed to typed
benmoriceau 545e6ad
Merge branch 'master' of github.com:airbytehq/airbyte into bmoric/ext…
benmoriceau b2e3250
Format
benmoriceau 7707a97
Add StateType and StateWrapper objects to the model
gosusnp e3c881b
Merge branch 'gosusnp/add_state_wrapper' of github.com:airbytehq/airb…
benmoriceau 27adc8b
Use state wrapper instead of Either
benmoriceau 4ee75aa
Switch to optional
benmoriceau c213f71
Merge branch 'master' into bmoric/extract-message-serialization
benmoriceau 280b114
Merge branch 'master' of github.com:airbytehq/airbyte into bmoric/ext…
benmoriceau 9cb57f6
PR comments
benmoriceau 0ea7eba
Support array legacy state
benmoriceau 0ae05a2
format
benmoriceau 59eba26
Merge branch 'master' of github.com:airbytehq/airbyte into bmoric/ext…
benmoriceau 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 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
36 changes: 36 additions & 0 deletions
36
...protocol/protocol-models/src/main/java/io/airbyte/protocol/models/StateMessageHelper.java
This file contains 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,36 @@ | ||
/* | ||
* Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.protocol.models; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.airbyte.commons.json.Jsons; | ||
import io.vavr.control.Either; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class StateMessageHelper { | ||
|
||
public static class AirbyteStateMessageListTypeReference extends TypeReference<List<AirbyteStateMessage>> {} | ||
|
||
/** | ||
* This a takes a json blob state and tries return either a legacy state in the format of a json | ||
* object or a state message with the new format which is a list of airbyte state message. | ||
* | ||
* @param state | ||
* @return Either a json blob (on the left) or a structure state message. | ||
*/ | ||
public static Either<JsonNode, List<AirbyteStateMessage>> getTypedState(JsonNode state) { | ||
if (state == null) { | ||
gosusnp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Either.right(new ArrayList<>()); | ||
} | ||
try { | ||
return Either.right(Jsons.object(state, new AirbyteStateMessageListTypeReference())); | ||
} catch (final IllegalArgumentException e) { | ||
return Either.left(state); | ||
} | ||
} | ||
|
||
} |
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.
what's the use case for this? not obvious to me why we wouldn't always return a
List<AirbyteStateMessage>
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.
It is to make it extra clear that there is a split between legacy and new messages. In legacy we don't have a typed message, the goal here is to be as close as possible to the format of the state: #13772 (comment)
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.
https://github.com/spencerwi/Either.java the first part of the description looks like a similar use case to what we are trying to achieve here which is to let the user of this library that they have to deal with a state in the old format (just a blob) or a state in the new format (List of messages).
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.
Would it make sense to use the
StateWrapper
that you mentioned in the spec?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.
It seems like
Either
is only doing part of the job. In your calling code in the other PR, you are still doing a ton of work to try to figure out what the type of the state message is. I think you want something more descriptive thanEither
. What you mentioned in the spec seems like it would be awesome here. And definitely have a static helper to construct that object from json would be useful!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 really wanted to be as close as possible to how we represent the state in the JsonBlob which is either a blob or a list of state message. Then understanding what is the type of the state would be done somewhere else.
I'll modify it to return the wrapper. @gosusnp Have you introduce the class in your PR?
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.
why?
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.
Yes, I am adding the
StateWrapper
in my PR. I'll share a draft once I updated to the latest version.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 agree with Charles here - rather than having this function turn a JsonNode state into an intermediate
Either
object and then requiring consumers of this to interpret that into the state type, it seems better to make this the one place that knows how to determine the state type completely from the JsonNode. So I thinkStateWrapper
would be great hereThere 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 will update it. I am working with @gosusnp to get it in a PR.