{"modulePath":"github.com/get-eventually/go-eventually","version":"v0.4.4","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package command\n\nPackage command contains types and interfaces for implementing Command Handlers, necessary for producing side effects in your Aggregates and system, and implement your Domain's business logic.\n\n## Types\n\n```go\ntype Command message.Message\n```\nCommand is a specific kind of Message that represents an intent. Commands should be phrased in the present, imperative tense, such as \"ActivateUser\" or \"CreateOrder\".\n\n```go\ntype Envelope[T Command] message.Envelope[T]\n```\nEnvelope carries both a Command and some optional Metadata attached to it.\n\n```go\nfunc FromGenericEnvelope[T Command](cmd GenericEnvelope) (Envelope[T], bool)\n```\nFromGenericEnvelope attempts to type-cast a GenericEnvelope instance into a strongly-typed Command Envelope.\n\nA boolean guard is returned to signal whether the type-casting was successful or not.\n\n```go\nfunc ToEnvelope[T Command](cmd T) Envelope[T]\n```\nToEnvelope is a convenience function that wraps the provided Command type into an Envelope, with no metadata attached to it.\n\n```go\nfunc (cmd Envelope[T]) ToGenericEnvelope() GenericEnvelope\n```\nToGenericEnvelope returns a GenericEnvelope version of the current Envelope instance.\n\n```go\ntype GenericEnvelope Envelope[Command]\n```\nGenericEnvelope is a Command Envelope that depends solely on the Command interface, not a specific generic Command type.\n\n```go\ntype Handler[T Command] interface {\n\tHandle(ctx context.Context, cmd Envelope[T]) error\n}\n```\nHandler is the interface that defines a Command Handler, a component that receives a specific kind of Command and executes the business logic related to that particular Command.\n\n```go\ntype HandlerFunc[T Command] func(context.Context, Envelope[T]) error\n```\nHandlerFunc is a functional type that implements the Handler interface. Useful for testing and stateless Handlers.\n\n```go\nfunc (fn HandlerFunc[T]) Handle(ctx context.Context, cmd Envelope[T]) error\n```\nHandle handles the provided Command through the functional Handler.\n\n```go\ntype ScenarioGiven[Cmd Command, T Handler[Cmd]] struct {\n\t// contains filtered or unexported fields\n}\n```\nScenarioGiven is the state of the scenario once a set of Domain Events have been provided using Given(), to represent the state of the system at the time of evaluating a Command.\n\n```go\nfunc (sc ScenarioGiven[Cmd, T]) When(cmd Envelope[Cmd]) ScenarioWhen[Cmd, T]\n```\nWhen provides the Command to evaluate.\n\n```go\ntype ScenarioInit[Cmd Command, T Handler[Cmd]] struct{}\n```\nScenarioInit is the entrypoint of the Command Handler scenario API.\n\nA Command Handler scenario can either set the current evaluation context by using Given(), or test a \"clean-slate\" scenario by using When() directly.\n\n```go\nfunc Scenario[Cmd Command, T Handler[Cmd]]() ScenarioInit[Cmd, T]\n```\nScenario is a scenario type to test the result of Commands being handled by a Command Handler.\n\nCommand Handlers in Event-sourced systems produce side effects by means of Domain Events. This scenario API helps you with testing the Domain Events produced by a Command Handler when handling a specific Command.\n\n```go\nfunc (sc ScenarioInit[Cmd, T]) Given(events ...event.Persisted) ScenarioGiven[Cmd, T]\n```\nGiven sets the Command Handler scenario preconditions.\n\nDomain Events are used in Event-sourced systems to represent a side effect that has taken place in the system. In order to set a given state for the system to be in while testing a specific Command evaluation, you should specify the Domain Events that have happened thus far.\n\nWhen you're testing Commands with a clean-slate system, you should either specify no Domain Events, or skip directly to When().\n\n```go\nfunc (sc ScenarioInit[Cmd, T]) When(cmd Envelope[Cmd]) ScenarioWhen[Cmd, T]\n```\nWhen provides the Command to evaluate.\n\n```go\ntype ScenarioThen[Cmd Command, T Handler[Cmd]] struct {\n\tScenarioWhen[Cmd, T]\n\t// contains filtered or unexported fields\n}\n```\nScenarioThen is the state of the scenario once the preconditions and expectations have been fully specified.\n\n```go\nfunc (sc ScenarioThen[Cmd, T]) AssertOn(\n\tt *testing.T,\n\thandlerFactory func(event.Store) T,\n)\n```\nAssertOn performs the specified expectations of the scenario, using the Command Handler instance produced by the provided factory function.\n\nA Command Handler should only use a single Aggregate type, to ensure that the side effects happen in a well-defined transactional boundary. If your Command Handler needs to modify more than one Aggregate, you might be doing something wrong in your domain model.\n\nThe type of the Aggregate used to evaluate the Command must be specified, so that the Event-sourced Repository instance can be provided to the factory function to build the desired Command Handler.\n\n```go\ntype ScenarioWhen[Cmd Command, T Handler[Cmd]] struct {\n\tScenarioGiven[Cmd, T]\n\t// contains filtered or unexported fields\n}\n```\nScenarioWhen is the state of the scenario once the state of the system and the Command to evaluate have been provided.\n\n```go\nfunc (sc ScenarioWhen[Cmd, T]) Then(events ...event.Persisted) ScenarioThen[Cmd, T]\n```\nThen sets a positive expectation on the scenario outcome, to produce the Domain Events provided in input.\n\nThe list of Domain Events specified should be ordered as the expected order of recording by the Command Handler.\n\n```go\nfunc (sc ScenarioWhen[Cmd, T]) ThenError(err error) ScenarioThen[Cmd, T]\n```\nThenError sets a negative expectation on the scenario outcome, to produce an error value that is similar to the one provided in input.\n\nError assertion happens using errors.Is(), so the error returned by the Command Handler is unwrapped until the cause error to match the provided expectation.\n\n```go\nfunc (sc ScenarioWhen[I, T]) ThenErrors(errs ...error) ScenarioThen[I, T]\n```\nThenErrors specifies an unsuccessful outcome of the scenario, where the domain command execution fails with a specific error that wraps multiple error types (e.g. through \\`errors.Join\\`).\n\nUse this method when you want to assert that the error returned by the domain command matches ALL of the errors specified.\n\n```go\nfunc (sc ScenarioWhen[Cmd, T]) ThenFails() ScenarioThen[Cmd, T]\n```\nThenFails sets a negative expectation on the scenario outcome, to fail the Command execution with no particular assertion on the error returned.\n\nThis is useful when the error returned is not important for the Command you're trying to test.\n\n","path":"github.com/get-eventually/go-eventually/command","name":"command","synopsis":"Package command contains types and interfaces for implementing Command Handlers, necessary for producing side effects in your Aggregates and system, and implement your Domain's business logic.","isRedistributable":true}
