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

Future control flow using predicates #5118

Merged
merged 1 commit into from
May 7, 2024
Merged

Future control flow using predicates #5118

merged 1 commit into from
May 7, 2024

Conversation

vietj
Copy link
Member

@vietj vietj commented Feb 15, 2024

Future control flow using predicates.

Future has already synchronous operations modifying the control flow

  • map : transforms a mapper failure into a failed future (e.g. future.map(val -> throw new VertxException()))
  • otherwise : transforms a failure into a success

This contribution aims to transform a success into a failure based on the result of a predicate.

Goals

  • capture useful predicates that modify the control flow, e.g. a predicate that checks the HTTP client response (status code, headers, etc...)
  • provide inline guards using lambda expressions

Vert.x Web Client has something close to this with HTTP expectations:

    client
      .get(8080, "myserver.mycompany.com", "/some-uri")
      .expect(ResponsePredicate.SC_SUCCESS)
      .expect(ResponsePredicate.JSON)
      .send()
      .onSuccess(res -> {
        // Safely decode the body as a json object
        JsonObject body = res.bodyAsJsonObject();
        System.out.println(
          "Received response with status code" +
            res.statusCode() +
            " with body " +
            body);
      })
      .onFailure(err ->
        System.out.println("Something went wrong " + err.getMessage()));

A predicate like interface Expectation: a synchronous predicate that checks a boolean value and has a method to create a meaningful exception.

Future<Buffer> body = client.request(options)
  .compose(req -> req
    .send()
    .expecting(resp -> resp.statusCode() == 200)
    .compose(resp -> resp.body()); 

The HTTP response status code can be captured so it can be reused.

Future<Buffer> body = client.request(options)
  .compose(req -> req
    .send()
    .expecting(HttpResponseExpectation.SC_OK)
    .compose(resp -> resp.body()); 

@vietj vietj self-assigned this Feb 15, 2024
@vietj vietj marked this pull request as draft February 15, 2024 10:07
@vietj vietj added this to the 5.0.0 milestone Feb 15, 2024
@vietj vietj force-pushed the future-expectation branch 5 times, most recently from c674471 to 8a1e001 Compare February 22, 2024 11:20
@vietj
Copy link
Member Author

vietj commented Feb 22, 2024

Further work : for now dropping the assertion part and instead try to develop a test construct to handle this through andThen/onComplete

@vietj vietj force-pushed the future-expectation branch 2 times, most recently from 2a2bd73 to f75ee66 Compare March 4, 2024 09:54
@vietj vietj marked this pull request as ready for review March 4, 2024 10:10
@vietj vietj force-pushed the future-expectation branch 8 times, most recently from 29e4f98 to 0a744c1 Compare March 7, 2024 22:34
@vietj vietj force-pushed the future-expectation branch from 0a744c1 to c85e26e Compare March 12, 2024 10:06
@vietj vietj force-pushed the future-expectation branch from c85e26e to 0e32f4a Compare March 28, 2024 07:54
@vietj vietj force-pushed the future-expectation branch from 0e32f4a to e50f028 Compare April 15, 2024 19:12
@vietj vietj force-pushed the future-expectation branch 2 times, most recently from 92fa954 to 37415d7 Compare May 7, 2024 10:41
@vietj vietj force-pushed the future-expectation branch from 37415d7 to 61803e1 Compare May 7, 2024 10:43
@vietj vietj merged commit f4847cb into master May 7, 2024
7 checks passed
@vietj vietj deleted the future-expectation branch May 7, 2024 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants