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

Support of Future<ReadStream<T>> #954

Open
jponge opened this issue Jul 17, 2024 · 2 comments
Open

Support of Future<ReadStream<T>> #954

jponge opened this issue Jul 17, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@jponge
Copy link
Member

jponge commented Jul 17, 2024

The generator does not properly support the case of Future<ReadStream<T>> methods (e.g., a Vert.x HTTP client response that is eventually wrapped by a JSON stream parser).

The idea is to translate those to Uni<ReadStream<T>> where the ReadStream is a Mutiny bindings shim, so we can call .toMulti() on it.

Given the following interface:

@VertxGen(concrete = false)
public interface AsyncStreams {

    Future<ReadStream<String>> asyncStream();
}

then the generated code is:

@io.smallrye.mutiny.vertx.MutinyGen(io.vertx.core.streams.AsyncStreams.class)
public interface AsyncStreams {

  static final io.smallrye.mutiny.vertx.TypeArg<io.vertx.mutiny.core.streams.ReadStream<java.lang.String>> TYPE_ARG_0 = new TypeArg<io.vertx.mutiny.core.streams.ReadStream<java.lang.String>>(o1 -> io.vertx.mutiny.core.streams.ReadStream.newInstance((io.vertx.core.streams.ReadStream)o1, TypeArg.unknown()), o1 -> o1.getDelegate());
  io.vertx.core.streams.AsyncStreams getDelegate();

  public io.smallrye.mutiny.Uni<io.vertx.mutiny.core.streams.ReadStream<String>> asyncStream();

  public static  AsyncStreams newInstance(io.vertx.core.streams.AsyncStreams arg) {
    return arg != null ? new AsyncStreamsImpl(arg) : null;
  }

}

class AsyncStreamsImpl implements AsyncStreams {
  private final io.vertx.core.streams.AsyncStreams delegate;
  
  public io.vertx.core.streams.AsyncStreams getDelegate() {
    return delegate;
  }

  /**
   * Empty constructor used by CDI, do not use this constructor directly.
   **/
  AsyncStreamsImpl() {
    this.delegate = null;
  }

  public AsyncStreamsImpl(io.vertx.core.streams.AsyncStreams delegate) {
    this.delegate = delegate;
  }

  @CheckReturnValue
  public io.smallrye.mutiny.Uni<io.vertx.mutiny.core.streams.ReadStream<String>> asyncStream() { 
    return io.smallrye.mutiny.vertx.UniHelper.toUni(delegate.asyncStream().map(x -> ReadStream<String>.newInstance(x)));}

  public io.vertx.mutiny.core.streams.ReadStream<String> asyncStreamAndAwait() { 
    return asyncStream().await().indefinitely();
  }


  public void asyncStreamAndForget() { 
    asyncStream().subscribe().with(io.smallrye.mutiny.vertx.UniHelper.NOOP);
  }


}

where the following method:

@CheckReturnValue
  public io.smallrye.mutiny.Uni<io.vertx.mutiny.core.streams.ReadStream<String>> asyncStream() { 
    return io.smallrye.mutiny.vertx.UniHelper.toUni(delegate.asyncStream().map(x -> ReadStream<String>.newInstance(x)));}

does not compile:

AsyncStreams.java:[46,115] illegal start of type
AsyncStreams.java:[46,116] not a statement
AsyncStreams.java:[46,117] ';' expected
image
@jponge jponge added the bug Something isn't working label Jul 17, 2024
@tsegismont
Copy link
Contributor

As a workaround, you can create a Vert.x API object that extends ReadStream, like the Cassandra client does:

  /**
   * Executes the given SQL <code>SELECT</code> statement which returns the results of the query as a read stream.
   *
   * @param sql              the SQL to execute. For example <code>SELECT * FROM table ...</code>.
   * @return a future of the result
   */
  Future<CassandraRowStream> queryStream(String sql);

@jponge
Copy link
Member Author

jponge commented Oct 14, 2024

Nice trick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants