Skip to content

Instantly share code, notes, and snippets.

@m-debugger
Created October 31, 2018 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m-debugger/fd653f13801f8489de3499cb3879e7a2 to your computer and use it in GitHub Desktop.
Save m-debugger/fd653f13801f8489de3499cb3879e7a2 to your computer and use it in GitHub Desktop.
when (info) {
null -> {
return unauthorized()
}
else -> {
val validInfo = validator.validateInfo(info)
when (validInfo) {
is Either.Right -> {
if (!validInfo.b.authorised) {
// authorised is false so return unauthorised
return unauthorized()
}
// authorised is true, so use the authorised token to create a certificate
val authorisedInfo = validInfo.b
val productService = productServiceBuilder.buildProductService(authorisedInfo)
// TODO add when (productService) here
//
return when (productService) {
is Either.Right -> {
val product = productService.b.getProduct(
authorisedInfo.userId!!)
APIGatewayProxyResponseEvent()
.withBody(jacksonObjectMapper().writeValueAsString(product))
.withStatusCode(200)
}
is Either.Left -> {
systemError()
}
}
}
is Either.Left -> {
return systemError()
}
}
}
}
@pakoito
Copy link

pakoito commented Oct 31, 2018

info.toOption()
  .toEither { unauthorized() }
  .flatMap {
    validator.validateInfo(it)
  }.flatMap {
    if (!it.authorised) {
      unauthorized().left()
    } else {
      productServiceBuilder.buildProductService(it)
       .flatMap {
         val product = it.getProduct(authorisedInfo.userId!!)
         APIGatewayProxyResponseEvent()
           .withBody(jacksonObjectMapper().writeValueAsString(product))
           .withStatusCode(200).right()
       }.mapLeft {
         systemError()
       } // mapLeft
   } // else
} // flatMap

@pakoito
Copy link

pakoito commented Oct 31, 2018

Either.monadError().bindingCatch {
  val one = info.toOption().toEither { unauthorized() }.bind()
  val two = validator.validateInfo(one).bind()
  val result = if (!two.authorised) {
    unauthorized().left()
  } else {
    try {
      val three = productServiceBuilder.buildProductService(two).bind()
      val four = getProduct(authorisedInfo.userId!!).bind()
      APIGatewayProxyResponseEvent()
        .withBody(jacksonObjectMapper().writeValueAsString(product))
        .withStatusCode(200).right()
    } catch (t: Throwable) {
      systemError().left()
    }
  }
  result.bind()
}

@Takhion
Copy link

Takhion commented Nov 2, 2018

what about:

(info ?: return unauthorized())
    .let(validator::validateInfo)
    .flatMap { validatedInfo ->
        val authorisedInfo = validatedInfo.takeIf { it.authorised } ?: return unauthorized()

        productServiceBuilder.buildProductService(authorisedInfo)
            .map { productService ->
                val product = productService.getProduct(authorisedInfo.userId!!)

                APIGatewayProxyResponseEvent()
                    .withBody(jacksonObjectMapper().writeValueAsString(product))
                    .withStatusCode(200)
            }
    }
    .getOrElse(::systemError)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment