Skip to content
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 14 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airbyte-protocol/protocol-models/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
dependencies {
implementation 'javax.validation:validation-api:1.1.0.Final'
implementation 'org.apache.commons:commons-lang3:3.11'
implementation 'io.vavr:vavr:0.10.4'
}

jsonSchema2Pojo {
Expand Down
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) {
Copy link
Contributor

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>

Copy link
Contributor Author

@benmoriceau benmoriceau Jun 15, 2022

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)

Copy link
Contributor Author

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).

Copy link
Contributor

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?

Copy link
Contributor

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 than Either. 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!

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor

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.

Copy link
Contributor

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 think StateWrapper would be great here

Copy link
Contributor Author

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.

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);
}
}

}