All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kalix.scalasdk.valueentity.ValueEntity.scala Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * Copyright 2021 Lightbend Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kalix.scalasdk.valueentity

import kalix.scalasdk.{ DeferredCall, Metadata, SideEffect }
import kalix.scalasdk.impl.valueentity.ValueEntityEffectImpl
import io.grpc.Status

object ValueEntity {
  object Effect {

    /**
     * Construct the effect that is returned by the command handler. The effect describes next processing actions, such
     * as updating state and sending a reply.
     *
     * @tparam S
     *   The type of the state for this entity.
     */
    trait Builder[S] {

      def updateState(newState: S): OnSuccessBuilder[S]

      /**
       * Delete the entity. No additional updates are allowed afterwards.
       */
      def deleteEntity(): OnSuccessBuilder[S]

      /**
       * Delete the entity. No additional updates are allowed afterwards.
       */
      @deprecated("Renamed to deleteEntity", "1.1.5")
      def deleteState(): OnSuccessBuilder[S]

      /**
       * Create a message reply.
       *
       * @param message
       *   The payload of the reply.
       * @return
       *   A message reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def reply[T](message: T): Effect[T]

      /**
       * Create a message reply.
       *
       * @param message
       *   The payload of the reply.
       * @param metadata
       *   The metadata for the message.
       * @return
       *   A message reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def reply[T](message: T, metadata: Metadata): Effect[T]

      /**
       * Create a forward reply.
       *
       * @param serviceCall
       *   The service call representing the forward.
       * @return
       *   A forward reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def forward[T](serviceCall: DeferredCall[_, T]): Effect[T]

      /**
       * Create an error reply.
       *
       * @param description
       *   The description of the error.
       * @return
       *   An error reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def error[T](description: String): Effect[T]

      /**
       * Create an error reply.
       *
       * @param description
       *   The description of the error.
       * @param statusCode
       *   A gRPC status code.
       * @return
       *   An error reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def error[T](description: String, statusCode: Status.Code): Effect[T]
    }

    trait OnSuccessBuilder[S] {

      /**
       * Reply after for example `updateState`.
       *
       * @param message
       *   The payload of the reply.
       * @return
       *   A message reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def thenReply[T](message: T): Effect[T]

      /**
       * Reply after for example updateState.
       *
       * @param message
       *   The payload of the reply.
       * @param metadata
       *   The metadata for the message.
       * @return
       *   A message reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def thenReply[T](message: T, metadata: Metadata): Effect[T]

      /**
       * Create a forward reply after for example updateState.
       *
       * @param serviceCall
       *   The service call representing the forward.
       * @return
       *   A forward reply.
       * @tparam T
       *   The type of the message that must be returned by this call.
       */
      def thenForward[T](serviceCall: DeferredCall[_, T]): Effect[T]
    }
  }

  /**
   * A return type to allow returning forwards or failures, and attaching effects to messages.
   *
   * @tparam T
   *   The type of the message that must be returned by this call.
   */
  trait Effect[T] {

    /**
     * Attach the given side effects to this reply.
     *
     * @param sideEffects
     *   The effects to attach.
     * @return
     *   A new reply with the attached effects.
     */
    def addSideEffects(sideEffects: Seq[SideEffect]): Effect[T]
  }

}

/** @tparam S The type of the state for this entity. */
abstract class ValueEntity[S] {
  private var _commandContext: Option[CommandContext] = None

  /**
   * Implement by returning the initial empty state object. This object will be passed into the command handlers, until
   * a new state replaces it.
   *
   * 

Also known as "zero state" or "neutral state". * *

null is an allowed value. */ def emptyState: S /** * Additional context and metadata for a command handler. * *

It will throw an exception if accessed from constructor. */ protected final def commandContext(): CommandContext = { try { _commandContext.get } catch { case _: NoSuchElementException => throw new IllegalStateException("CommandContext is only available when handling a command.") } } /** INTERNAL API */ final def _internalSetCommandContext(context: Option[CommandContext]): Unit = { _commandContext = context } protected final def effects: ValueEntity.Effect.Builder[S] = ValueEntityEffectImpl[S]() }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy