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

Uni with Optional #138

Closed
Meemaw opened this issue May 11, 2020 · 4 comments · Fixed by #139
Closed

Uni with Optional #138

Meemaw opened this issue May 11, 2020 · 4 comments · Fixed by #139
Labels
bug Something isn't working
Milestone

Comments

@Meemaw
Copy link

Meemaw commented May 11, 2020

How to map to Optionals using Unis?

  @Override
  public Uni<Optional<PageDTO>> getPage(UUID pageID, UUID sessionID, UUID deviceID) {
    Tuple values = Tuple.of(pageID, sessionID, deviceID);
    return pgPool
        .preparedQuery(SELECT_PAGE_RAW_SWL, values)
        .map(
            rowSet -> {
              if (!rowSet.iterator().hasNext()) {
                return Optional.empty();
              }
              Row row = rowSet.iterator().next();
              return Optional.of(
                  new PageDTO(
                      row.getString("org_id"),
                      row.getUUID("uid"),
                      row.getString("url"),
                      row.getString("referrer"),
                      row.getString("doctype"),
                      row.getInteger("screen_width"),
                      row.getInteger("screen_height"),
                      row.getInteger("width"),
                      row.getInteger("height"),
                      row.getInteger("compiled_timestamp")));
            })
        .onFailure()
        .invoke(
            throwable -> {
              log.error("Failed to get page id={}", pageID);
              throw new DatabaseException();
            });
  }

This code will fails with the error:

Required type: Uni<Optional<PageDTO>>
Provided: Uni<Optional<?>>
@cescoffier
Copy link
Contributor

At first glance, it looks like a bug. It should work.

@cescoffier cescoffier added the bug Something isn't working label May 11, 2020
@cescoffier cescoffier added this to the 0.5.1 milestone May 11, 2020
@cescoffier
Copy link
Contributor

Ok, actually no it's normal. You need to help JAva a little bit:

.<Optional< PageDTO >>map(rowSet -> {

@cescoffier cescoffier linked a pull request May 11, 2020 that will close this issue
@Meemaw
Copy link
Author

Meemaw commented May 11, 2020

Interestingly this works with the CompletionStage API (different piece of code, but "same typing"):

  @Override
  public CompletionStage<Optional<UserDTO>> findUser(String email) {
    Tuple values = Tuple.of(email);
    return pgPool
        .preparedQuery(FIND_USER_BY_EMAIL_RAW_SQL, values)
        .thenApply(
            pgRowSet -> {
              if (!pgRowSet.iterator().hasNext()) {
                return Optional.empty();
              }
              Row row = pgRowSet.iterator().next();
              return Optional.of(
                  new UserDTO(
                      row.getUUID("id"),
                      row.getString("email"),
                      UserRole.valueOf(row.getString("role")),
                      row.getString("org")));
            });
  }

@cescoffier
Copy link
Contributor

Yes because you don't have the last "stage" (onFailure().invoke), so it is able to deduce the generic type.

This would also work:

Uni<Optional<Integer>> uni = Uni.createFrom().item(Collections.emptyList())
                .map(list -> {
                    if (list.isEmpty()) {
                        return Optional.empty();
                    } else {
                        return Optional.of(12345);
                    }
                })

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

Successfully merging a pull request may close this issue.

2 participants