{"modulePath":"github.com/get-eventually/go-eventually","version":"v0.4.4","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package aggregate\n\nPackage aggregate defines interfaces and types necessary to allow users to define their own Aggregate types.\n\n## Variables\n\n```go\nvar ErrRootNotFound = errors.New(\"aggregate: root not found\")\n```\nErrRootNotFound is returned when the Aggregate Root requested through a Repository was not found.\n\n## Functions\n\n```go\nfunc RecordThat[I ID](root Root[I], events ...event.Envelope) error\n```\nRecordThat records the Domain Event for the specified Aggregate Root.\n\nAn error is typically returned if applying the Domain Event on the Aggregate Root instance fails with an error.\n\n```go\nfunc RehydrateFromEvents[I ID](root Root[I], stream *event.Stream) error\n```\nRehydrateFromEvents rehydrates an Aggregate Root from a Stream of persisted Domain Events.\n\nThe Stream is iterated to completion or until the Aggregate Root's Apply method returns an error. After iteration, the stream's terminal error (if any) is checked via Stream.Err.\n\n```go\nfunc RehydrateFromState[I ID, Src Root[I], Dst any](\n\tv version.Version,\n\tdst Dst,\n\tdeserializer serde.Deserializer[Src, Dst],\n) (Src, error)\n```\nRehydrateFromState rehydrates an aggregate.Root instance using a state type, typically coming from an external state type (e.g. Protobuf type) and aggregate.Repository implementation (e.g. postgres.AggregateRepository).\n\n## Types\n\n```go\ntype Aggregate interface {\n\t// Apply applies the specified Event to the Aggregate Root,\n\t// by causing a state change in the Aggregate Root instance.\n\t//\n\t// Since this method cause a state change, implementors should make sure\n\t// to use pointer semantics on their Aggregate Root method receivers.\n\t//\n\t// Please note, this method should not perform any kind of external request\n\t// and should be, save for the Aggregate Root state mutation, free of side effects.\n\t// For this reason, this method does not include a context.Context instance\n\t// in the input parameters.\n\tApply(event.Event) error\n}\n```\nAggregate is the segregated interface, part of the Aggregate Root interface, that describes the left-folding behavior of Domain Events to update the Aggregate Root state.\n\n```go\ntype BaseRoot struct {\n\t// contains filtered or unexported fields\n}\n```\nBaseRoot segregates and completes the aggregate.Root interface implementation when embedded to a user-defined Aggregate Root type.\n\nBaseRoot provides some common traits, such as tracking the current Aggregate Root version, and the recorded-but-uncommitted Domain Events, through the aggregate.RecordThat function.\n\n```go\nfunc (br *BaseRoot) FlushRecordedEvents() []event.Envelope\n```\nFlushRecordedEvents returns the list of uncommitted, recorded Domain Events through the Aggregate Root.\n\nThe internal list kept by aggregate.BaseRoot is reset.\n\n```go\nfunc (br *BaseRoot) Version() version.Version\n```\nVersion returns the current version of the Aggregate Root instance.\n\n```go\ntype EventSourcedRepository[I ID, T Root[I]] struct {\n\t// contains filtered or unexported fields\n}\n```\nEventSourcedRepository provides an aggregate.Repository interface implementation that uses an event.Store to store and load the state of the Aggregate Root.\n\n```go\nfunc NewEventSourcedRepository[I ID, T Root[I]](eventStore event.Store, typ Type[I, T]) EventSourcedRepository[I, T]\n```\nNewEventSourcedRepository returns a new EventSourcedRepository implementation to store and load Aggregate Roots, specified by the aggregate.Type, using the provided event.Store implementation.\n\n```go\nfunc (repo EventSourcedRepository[I, T]) Get(ctx context.Context, id I) (T, error)\n```\nGet returns the Aggregate Root with the specified id.\n\naggregate.ErrRootNotFound is returned if no Aggregate Root was found with that id.\n\nAn error is returned if the underlying Event Store fails, or if an error occurs while trying to rehydrate the Aggregate Root state from its Event Stream.\n\n```go\nfunc (repo EventSourcedRepository[I, T]) Save(ctx context.Context, root T) error\n```\nSave stores the Aggregate Root to the Event Store, by adding the new, uncommitted Domain Events recorded through the Root, if any.\n\nAn error is returned if the underlying Event Store fails.\n\n```go\ntype Factory[I ID, T Root[I]] func() T\n```\nFactory is a function that creates new zero-valued instances of an aggregate.Root implementation.\n\n```go\ntype FusedRepository[I ID, T Root[I]] struct {\n\tGetter[I, T]\n\tSaver[I, T]\n}\n```\nFusedRepository is a convenience type that can be used to fuse together different implementations for the Getter and Saver Repository interface components.\n\n```go\ntype Getter[I ID, T Root[I]] interface {\n\tGet(ctx context.Context, id I) (T, error)\n}\n```\nGetter is an Aggregate Repository interface component, that can be used for retrieving Aggregate Roots from some storage.\n\n```go\ntype ID interface {\n\tfmt.Stringer\n}\n```\nID represents an Aggregate ID type.\n\nAggregate IDs should be able to be marshaled into a string format, in order to be saved onto a named Event Stream.\n\n```go\ntype Internal interface {\n\tFlushRecordedEvents() []event.Envelope\n}\n```\nInternal contains some Aggregate Root methods that are used by internal packages and modules for this library.\n\nDirect usage of these methods are discouraged.\n\n```go\ntype Repository[I ID, T Root[I]] interface {\n\tGetter[I, T]\n\tSaver[I, T]\n}\n```\nRepository is an interface used to get Aggregate Roots from and save them to some kind of storage, depending on the implementation.\n\n```go\ntype Root[I ID] interface {\n\tAggregate\n\tInternal\n\n\t// AggregateID returns the Aggregate Root identifier.\n\tAggregateID() I\n\n\t// Version returns the current Aggregate Root version.\n\t// The version gets updated each time a new event is recorded\n\t// through the aggregate.RecordThat function.\n\tVersion() version.Version\n\t// contains filtered or unexported methods\n}\n```\nRoot is the interface describing an Aggregate Root instance.\n\nThis interface should be implemented by your Aggregate Root types. Make sure your Aggregate Root types embed the aggregate.BaseRoot type to complete the implementation of this interface.\n\n```go\ntype Saver[I ID, T Root[I]] interface {\n\tSave(ctx context.Context, root T) error\n}\n```\nSaver is an Aggregate Repository interface component, that can be used for storing Aggregate Roots in some storage.\n\n```go\ntype ScenarioGiven[I ID, T Root[I]] struct {\n\t// contains filtered or unexported fields\n}\n```\nScenarioGiven is the state of the scenario once the Aggregate Root preconditions have been set through the Scenario().Given() method.\n\nThis state gives access to the When() method to specify the aggregate method to test using the desired Aggregate Root.\n\n```go\nfunc (sc ScenarioGiven[I, T]) When(fn func(T) error) ScenarioWhen[I, T]\n```\nWhen allows to call the aggregate method method on the Aggregate Root instance provided by the previous Scenario().Given() call.\n\nThe aggregate method must be called inside the required closure parameter.\n\n```go\ntype ScenarioInit[I ID, T Root[I]] struct {\n\t// contains filtered or unexported fields\n}\n```\nScenarioInit is the entrypoint of the Aggregate Root scenario API.\n\nAn Aggregate Root 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[I ID, T Root[I]](typ Type[I, T]) ScenarioInit[I, T]\n```\nScenario is a scenario type to test the result of methods called on an Aggregate Root and their effects.\n\nThese methods are meant to produce side-effects in the Aggregate Root state, and thus in the overall system, enforcing the aggregate invariants represented by the Aggregate Root itself.\n\n```go\nfunc (sc ScenarioInit[I, T]) Given(events ...event.Persisted) ScenarioGiven[I, T]\n```\nGiven allows to set an Aggregate Root state as precondition to the scenario test, by specifying ordered Domain Events.\n\n```go\nfunc (sc ScenarioInit[I, T]) When(fn func() (T, error)) ScenarioWhen[I, T]\n```\nWhen allows to call for a aggregate method/function that creates a new Aggregate Root instance.\n\nThis method requires a closure that return said new Aggregate Root instance (hence why no input parameter) or an error.\n\n```go\ntype ScenarioThen[I ID, T Root[I]] struct {\n\t// contains filtered or unexported fields\n}\n```\nScenarioThen is the state of the scenario where all parameters have been set and it's ready to be executed using a testing.T instance.\n\nUse the AssertOn method to run the test scenario.\n\n```go\nfunc (sc ScenarioThen[I, T]) AssertOn(t *testing.T)\n```\nAssertOn runs the test scenario using the specified testing.T instance.\n\n```go\ntype ScenarioWhen[I ID, T Root[I]] struct {\n\t// contains filtered or unexported fields\n}\n```\nScenarioWhen is the state of the scenario once the aggregate method to test has been provided through either Scenario().When() or Scenario().Given().When() paths.\n\nThis state allows to specify the expected outcome on the scenario using either Then(), ThenFails(), ThenError() or ThenErrors() methods.\n\n```go\nfunc (sc ScenarioWhen[I, T]) Then(v version.Version, events ...event.Envelope) ScenarioThen[I, T]\n```\nThen specifies a successful outcome of the scenario, allowing to assert the expected new Aggregate Root version and Domain Events recorded during the aggregate method execution.\n\n```go\nfunc (sc ScenarioWhen[I, T]) ThenError(err error) ScenarioThen[I, T]\n```\nThenError specifies an unsuccessful outcome of the scenario, where the aggregate method execution fails with an error.\n\nUse this method when you want to assert that the error retured by the aggregate method execution is of a specific type or value.\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 aggregate method 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 aggregate method matches ALL of the errors specified.\n\n```go\nfunc (sc ScenarioWhen[I, T]) ThenFails() ScenarioThen[I, T]\n```\nThenFails specifies an unsuccessful outcome of the scenario, where the aggregate method execution fails with an error.\n\nUse this method when there is no need to assert the error returned by the aggregate method is of a specific type or value.\n\n```go\ntype Type[I ID, T Root[I]] struct {\n\tName    string\n\tFactory func() T\n}\n```\nType represents the type of an Aggregate, which will expose the name of the Aggregate (used as Event Store type).\n\nIf your Aggregate implementation uses pointers, use the factory to return a non-nil instance of the type.\n\n","path":"github.com/get-eventually/go-eventually/aggregate","name":"aggregate","synopsis":"Package aggregate defines interfaces and types necessary to allow users to define their own Aggregate types.","isRedistributable":true}
