commonMain.Operation.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spine Show documentation
Show all versions of spine Show documentation
Multiplatform API declaration
The newest version!
package opensavvy.spine
import arrow.core.raise.Raise
import opensavvy.state.arrow.out
import kotlin.js.JsName
typealias OperationValidator = suspend Operation.ValidatorScope.() -> Unit
class Operation(
val resource: ResourceGroup.AbstractResource,
val kind: Kind,
val route: Route? = null,
@JsName("_validate") private val validate: OperationValidator,
) {
/**
* Instantiates an [Id] for the resource this operation is based on.
*
* This is simply syntax sugar for calling [resource].[idOf][ResourceGroup.AbstractResource.idOf].
*/
fun idOf(vararg dynamic: String) = resource.idOf(*dynamic)
suspend fun validate(id: Id, body: In, parameters: Params, context: Context) = out, Unit> {
val scope = ValidatorScope(this, id, body, parameters, context)
scope.validate()
}
/**
* The various kinds of operations that can be executed on a [ResourceGroup.AbstractResource] instance.
*
* Various operations are differentiated by their semantics: whether they allow caching, whether they are idempotent and what modifications they allow.
*/
enum class Kind {
/**
* Reads some information about a resource.
*
* This operation cannot lead to any change of state on the whole system.
*/
Read,
/**
* Creates a new resource.
*
* This operation is not idempotent: two [Create] in a row with the same payload will create two different values.
*/
Create,
/**
* Edits an existing resource.
*
* This operation may or may not be idempotent.
*/
Edit,
/**
* Executes an arbitrary action on a resource.
*
* This operation should not be confused with [Create], [Edit] and [Delete].
*
* This operation may or may not be idempotent.
*/
Action,
/**
* Deletes an existing resource.
*/
Delete,
}
class ValidatorScope internal constructor(
private val scope: Raise>,
val id: Id,
val body: In,
val parameters: Params,
val context: Context,
) : Raise> by scope
}