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

Add support for Destination Middleware #664

Merged
merged 12 commits into from
May 29, 2020
Merged

Conversation

prayansh
Copy link
Contributor

@prayansh prayansh commented May 28, 2020

Summary

This PR implements destination specific middleware that function similar to source middleware, but instead run only for the specified integration.
These can be configured via the Analytics.Builder.
Example

builder
    .useDestinationMiddleware("Segment.io",
      new Middleware() {
          @Override
          public void intercept(Chain chain) {
            // ...transform payload
            chain.proceed(chain.payload()); // send new payload to the chain
          }
    )

This PR also deprecates middleware for sourceMiddleware which are middleware that run for all integrations.

Ref: LIBMOBILE-38

Test Cases

  • single destination middleware runs (DestinationMiddlewareTest.middlewareRuns)
  • destination middleware only runs for specified destination (DestinationMiddlewareTest.middlewareDoesNotRunForOtherIntegration)
  • same middleware can be used for multiple integrations (DestinationMiddlewareTest.middlewareWillRunForMultipleIntegrations)
  • Middleware can drop event for specified integration
    (DestinationMiddlewareTest.middlewareCanShortCircuit)
  • Middleware can chain
    (DestinationMiddlewareTest.middlewareCanChain)
  • Middleware can transform the payload
    (DestinationMiddlewareTest.middlewareCanTransform)

@prayansh prayansh requested a review from bsneed May 28, 2020 23:03
@@ -235,6 +235,7 @@ public static String assertNotNullOrEmpty(String text, @Nullable String name) {
/** Returns an immutable copy of the provided map. */
@NonNull
public static <K, V> Map<K, V> immutableCopyOf(@NonNull Map<K, V> map) {
// TODO (major version change) change this out to allow @Nullable input and guard against it
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ideally this would become

@NonNull
  public static <K, V> Map<K, V> immutableCopyOf(@Nullable Map<K, V> map) {
    if (isNullOrEmpty(map)) {
      return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(new LinkedHashMap<>(map));
  }

but dont wanna do a breaking change in this version.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to leave that in the TODO comment in the code itself as well.

@@ -361,4 +366,44 @@ public void eventPlanOverridesSchemaDefault() throws IOException {
+ "}"));
verify(integration, never()).track(payload);
}

@Test
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests have nothing to do with destination middleware, but felt good to have as unit tests

* Add a {@link Middleware} custom source middleware. This will be run before sending to all
* integrations
*/
public Builder useSourceMiddleware(Middleware middleware) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

drop-in replacement for middleware

Copy link
Contributor

Choose a reason for hiding this comment

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

perfect

@prayansh prayansh changed the title Add support for Destination Middleware [WIP] Add support for Destination Middleware May 29, 2020
* Add a {@link Middleware} custom source middleware. This will be run before sending to all
* integrations
*/
public Builder useSourceMiddleware(Middleware middleware) {
Copy link
Contributor

Choose a reason for hiding this comment

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

perfect

List<Middleware> applicableMiddleware = getMiddlewareList(destinationMiddleware, key);
runMiddlewareChain(
payload,
applicableMiddleware,
Copy link
Contributor

Choose a reason for hiding this comment

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

the isIntegrationEnabled bit that seems to have been removed here seems like it might be important. If an integration is disabled on segment.com, there's no need to run it's middleware right?

Copy link
Contributor

Choose a reason for hiding this comment

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

never mind, I see it down below. however, it's repeated. seems like that could be placed here instead and checked once?

@@ -235,6 +235,7 @@ public static String assertNotNullOrEmpty(String text, @Nullable String name) {
/** Returns an immutable copy of the provided map. */
@NonNull
public static <K, V> Map<K, V> immutableCopyOf(@NonNull Map<K, V> map) {
// TODO (major version change) change this out to allow @Nullable input and guard against it
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -235,6 +235,7 @@ public static String assertNotNullOrEmpty(String text, @Nullable String name) {
/** Returns an immutable copy of the provided map. */
@NonNull
public static <K, V> Map<K, V> immutableCopyOf(@NonNull Map<K, V> map) {
// TODO (major version change) change this out to allow @Nullable input and guard against it
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to leave that in the TODO comment in the code itself as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants