Generate the RpcCaller functional interface
(packageName: string, packageDir: string)
| 2007 | |
| 2008 | /** Generate the RpcCaller functional interface */ |
| 2009 | async function generateRpcCallerInterface(packageName: string, packageDir: string): Promise<void> { |
| 2010 | const lines: string[] = []; |
| 2011 | lines.push(COPYRIGHT); |
| 2012 | lines.push(``); |
| 2013 | lines.push(AUTO_GENERATED_HEADER); |
| 2014 | lines.push(GENERATED_FROM_API); |
| 2015 | lines.push(``); |
| 2016 | lines.push(`package ${packageName};`); |
| 2017 | lines.push(``); |
| 2018 | lines.push(`import com.fasterxml.jackson.databind.JavaType;`); |
| 2019 | lines.push(`import com.fasterxml.jackson.databind.JsonNode;`); |
| 2020 | lines.push(`import java.util.concurrent.CompletableFuture;`); |
| 2021 | lines.push(`import java.util.concurrent.CompletionException;`); |
| 2022 | lines.push(`import javax.annotation.processing.Generated;`); |
| 2023 | lines.push(``); |
| 2024 | lines.push(`/**`); |
| 2025 | lines.push(` * Interface for invoking JSON-RPC methods with typed responses.`); |
| 2026 | lines.push(` * <p>`); |
| 2027 | lines.push(` * Implementations delegate to the underlying transport layer`); |
| 2028 | lines.push(` * (e.g., a {@code JsonRpcClient} instance). A method reference is typically the clearest`); |
| 2029 | lines.push(` * way to adapt a generic {@code invoke} method to this interface:`); |
| 2030 | lines.push(` * <pre>{@code`); |
| 2031 | lines.push(` * RpcCaller caller = jsonRpcClient::invoke;`); |
| 2032 | lines.push(` * }</pre>`); |
| 2033 | lines.push(` *`); |
| 2034 | lines.push(` * @since 1.0.0`); |
| 2035 | lines.push(` */`); |
| 2036 | lines.push(GENERATED_ANNOTATION); |
| 2037 | lines.push(`public interface RpcCaller {`); |
| 2038 | lines.push(``); |
| 2039 | lines.push(` /**`); |
| 2040 | lines.push(` * Invokes a JSON-RPC method and returns a future for the typed response.`); |
| 2041 | lines.push(` *`); |
| 2042 | lines.push(` * @param <T> the expected response type`); |
| 2043 | lines.push(` * @param method the JSON-RPC method name`); |
| 2044 | lines.push(` * @param params the request parameters (may be a {@code Map}, DTO record, or {@code JsonNode})`); |
| 2045 | lines.push(` * @param resultType the {@link Class} of the expected response type`); |
| 2046 | lines.push(` * @return a {@link CompletableFuture} that completes with the deserialized result`); |
| 2047 | lines.push(` */`); |
| 2048 | lines.push(` <T> CompletableFuture<T> invoke(String method, Object params, Class<T> resultType);`); |
| 2049 | lines.push(``); |
| 2050 | lines.push(` /**`); |
| 2051 | lines.push(` * Invokes a JSON-RPC method and returns a future for the typed response.`); |
| 2052 | lines.push(` *`); |
| 2053 | lines.push(` * @param <T> the expected response type`); |
| 2054 | lines.push(` * @param method the JSON-RPC method name`); |
| 2055 | lines.push(` * @param params the request parameters (may be a {@code Map}, DTO record, or {@code JsonNode})`); |
| 2056 | lines.push(` * @param resultType the Jackson {@link JavaType} of the expected response type`); |
| 2057 | lines.push(` * @return a {@link CompletableFuture} that completes with the deserialized result`); |
| 2058 | lines.push(` */`); |
| 2059 | lines.push(` default <T> CompletableFuture<T> invoke(String method, Object params, JavaType resultType) {`); |
| 2060 | lines.push(` if (resultType.hasRawClass(Void.class) || resultType.hasRawClass(Void.TYPE)) {`); |
| 2061 | lines.push(` return invoke(method, params, Void.class).thenApply(ignored -> null);`); |
| 2062 | lines.push(` }`); |
| 2063 | lines.push(` return invoke(method, params, JsonNode.class).thenApply(result -> {`); |
| 2064 | lines.push(` try {`); |
| 2065 | lines.push(` return RpcMapper.INSTANCE.readerFor(resultType).readValue(result);`); |
| 2066 | lines.push(` } catch (java.io.IOException e) {`); |
no test coverage detected
searching dependent graphs…