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

Migrate some logic from generated code into the runtime library #475

Merged
merged 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

package com.palantir.dialogue.example;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.palantir.dialogue.Channel;
import com.palantir.dialogue.ConjureRuntime;
import com.palantir.dialogue.Deserializer;
import com.palantir.dialogue.Endpoint;
import com.palantir.dialogue.HttpMethod;
import com.palantir.dialogue.PathTemplate;
import com.palantir.dialogue.PlainSerDe;
import com.palantir.dialogue.RemoteExceptions;
import com.palantir.dialogue.Request;
import com.palantir.dialogue.Response;
import com.palantir.dialogue.Serializer;
import com.palantir.dialogue.TypeMarker;
import com.palantir.dialogue.UrlBuilder;
Expand Down Expand Up @@ -138,25 +134,14 @@ public SampleObject objectToObject(
.putAllQueryParams("queryKey", plainSerDe.serializeRidList(query))
.body(sampleObjectToSampleObjectSerializer.serialize(body))
.build();

ListenableFuture<Response> call = channel.execute(STRING_TO_STRING, request);
ListenableFuture<SampleObject> response = Futures.transform(
call,
r -> sampleObjectToSampleObjectDeserializer.deserialize(r),
MoreExecutors.directExecutor());

return RemoteExceptions.getUnchecked(response);
return runtime.clients()
.blocking(channel, STRING_TO_STRING, request, sampleObjectToSampleObjectDeserializer);
}

@Override
public void voidToVoid() {
Request request = Request.builder().build();

ListenableFuture<Response> call = channel.execute(VOID_TO_VOID, request);
ListenableFuture<Void> response = Futures.transform(
call, r -> voidToVoidDeserializer.deserialize(r), MoreExecutors.directExecutor());

RemoteExceptions.getUnchecked(response);
runtime.clients().blocking(channel, VOID_TO_VOID, request, voidToVoidDeserializer);
}
};
}
Expand Down Expand Up @@ -188,21 +173,14 @@ public ListenableFuture<SampleObject> stringToString(
.putAllQueryParams("queryKey", plainSerDe.serializeRidList(query))
.body(sampleObjectToSampleObjectSerializer.serialize(body))
.build();

ListenableFuture<Response> call = channel.execute(STRING_TO_STRING, request);
return Futures.transform(
call,
response -> sampleObjectToSampleObjectDeserializer.deserialize(response),
MoreExecutors.directExecutor());
return runtime.clients()
.call(channel, STRING_TO_STRING, request, sampleObjectToSampleObjectDeserializer);
}

@Override
public ListenableFuture<Void> voidToVoid() {
Request request = Request.builder().build();

ListenableFuture<Response> call = channel.execute(VOID_TO_VOID, request);
return Futures.transform(
call, response -> voidToVoidDeserializer.deserialize(response), MoreExecutors.directExecutor());
return runtime.clients().call(channel, VOID_TO_VOID, request, voidToVoidDeserializer);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.conjure.java.dialogue.serde;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the serde package+module don't make a great deal of sense for the entire runtime object, but I'm not going to couple changing that with this PR.


import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.palantir.dialogue.Channel;
import com.palantir.dialogue.Clients;
import com.palantir.dialogue.Deserializer;
import com.palantir.dialogue.Endpoint;
import com.palantir.dialogue.RemoteExceptions;
import com.palantir.dialogue.Request;

enum DefaultClients implements Clients {
INSTANCE;

@Override
public <T> ListenableFuture<T> call(
Channel channel, Endpoint endpoint, Request request, Deserializer<T> deserializer) {
return Futures.transform(
channel.execute(endpoint, request), deserializer::deserialize, MoreExecutors.directExecutor());
}

@Override
public <T> T blocking(Channel channel, Endpoint endpoint, Request request, Deserializer<T> deserializer) {
ListenableFuture<T> call = call(channel, endpoint, request, deserializer);
return RemoteExceptions.getUnchecked(call);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The RemoteExceptions utility can be moved out of the compilation target (break for older generated code).

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure lets do 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.

I'll plan to move this after we've cut a release where generated code uses the new methods, that way we have a window for libraries to safely upgrade.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.palantir.dialogue.BodySerDe;
import com.palantir.dialogue.Clients;
import com.palantir.dialogue.ConjureRuntime;
import com.palantir.dialogue.PlainSerDe;
import com.palantir.logsafe.Preconditions;
Expand Down Expand Up @@ -52,6 +53,11 @@ public PlainSerDe plainSerDe() {
return ConjurePlainSerDe.INSTANCE;
}

@Override
public Clients clients() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thoughts on naming?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like it!

return DefaultClients.INSTANCE;
}

public static final class Builder {

private final List<Encoding> encodings = new ArrayList<>();
Expand Down
26 changes: 26 additions & 0 deletions dialogue-target/src/main/java/com/palantir/dialogue/Clients.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.dialogue;

import com.google.common.util.concurrent.ListenableFuture;

public interface Clients {

<T> ListenableFuture<T> call(Channel channel, Endpoint endpoint, Request request, Deserializer<T> deserializer);

<T> T blocking(Channel channel, Endpoint endpoint, Request request, Deserializer<T> deserializer);
Copy link
Contributor

Choose a reason for hiding this comment

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

this interface doesn't quite work with the existing codegen. It would force us to duplicate the code for building the request in the blocking/async impls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah doh, our sample services are no longer representative

Copy link
Contributor Author

@carterkozak carterkozak Mar 3, 2020

Choose a reason for hiding this comment

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

I could go either way naming <T> T blocking(ListenableFuture<T>). Preference on return clients().block(future) vs return clients().blocking(future);

Copy link
Contributor

Choose a reason for hiding this comment

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

I think i prefer block so that both methods are verbs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 thanks!

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public interface ConjureRuntime {

/** Provides the {@link PlainSerDe} used to parse request path, query, and header parameters. */
PlainSerDe plainSerDe();

/** Provides the {@link Clients} used to facilitate making requests. */
Clients clients();
}