exacaster / lighter

REST API for Apache Spark on K8S or YARN
MIT License
91 stars 21 forks source link

Fix create statement #710

Closed jmilkiewicz closed 1 year ago

jmilkiewicz commented 1 year ago

Fixing https://github.com/exacaster/lighter/issues/709

Note on implementation: As of now createStatement can return one of 3 possible options:

To model a function/method which can return one of these 3 options (with each of them having complete different shape) i see/know following approaches:

I decided to go for option 3 but as we are in old java, without kotlin/scala the implementation is pretty exotic as it is based on visitor pattern, ie I return StatementCreationResult which can be visited with StatementCreationResultMapper. Both are abstractions and the overall solution provides:

If it were new java/kotlin the code would be much simpler, sth like:

sealed interface StatementCreationResult

class StatementCreated(statement: Statement) : StatementCreationResult
object NoSessionExists : StatementCreationResult
class SessionInInvalidState(invalidState: ApplicationState) : StatementCreationResult 

and in controller

val result = sessionService.createStatement(id, statement)
when(result) {
is NoSessionExists ->  HttpResponse.notFound()
is SessionInInvalidState -> HttpResponse.badRequest("invalid session state " + invalidState);
is StatementCreated ->  HttpResponse.created(statement);
}

Compiler will force me to handle all possible options in when statement and the code will be more straightforward