resource

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const SourceValue = "$_source_value"

Variables

View Source
var DefaultIdFieldTool = NewIdTool(structs.StructTagDirectiveProvider{"id"})

IdTool is an alias for DefaultIdFieldTool for backward compatibility.

View Source
var RegistryCtxKey = registryCtxKey{}

RegistryCtxKey is the context key for storing/retrieving a Registry.

Functions

func NewIdTool

func NewIdTool(dp structs.DirectiveProvider) *structs.Tool

NewIdTool produces a structs.Applier that will take each of the defined id fields and propagate them to another struct value.

func NewInstance added in v0.2.0

func NewInstance[I Instance[I]](ctx context.Context, sub auth.Subject) (I, gomerr.Gomerr)

NewInstance creates a new instance of type I. Retrieves the Registry from context automatically.

func Register

func Register[I Instance[I]](r *Registry, opts ...Option)

Register registers an instance type with the registry.

Types

type Action

type Action[T any] interface {
	AnyAction
	Pre(context.Context, T) gomerr.Gomerr
	Retry(context.Context, T, gomerr.Gomerr) gomerr.Gomerr
	Do(context.Context, T) gomerr.Gomerr
	OnDoSuccess(context.Context, T) (T, gomerr.Gomerr)
	OnDoFailure(context.Context, T, gomerr.Gomerr) gomerr.Gomerr
}

Action defines an operation that can be performed on a resource.

func CreateAction

func CreateAction[I Instance[I]]() Action[I]

CreateAction returns an action for creating instances.

func DeleteAction

func DeleteAction[I Instance[I]]() Action[I]

DeleteAction returns an action for deleting instances.

func ListAction

func ListAction[I Instance[I]]() Action[*Collection[I]]

ListAction returns an action for listing instances via a collection.

func ReadAction

func ReadAction[I Instance[I]]() Action[I]

ReadAction returns an action for reading instances.

func UpdateAction

func UpdateAction[I Instance[I]](readAction Action[I]) Action[I]

UpdateAction returns an action for updating instances.

type AnyAction added in v0.2.0

type AnyAction interface {
	Name() string
	AppliesToCategory() Category
	FieldAccessPermissions() auth.AccessPermissions
	ExecuteOn(ctx context.Context, resource any) (any, gomerr.Gomerr)
}

type BaseInstance

type BaseInstance[I Instance[I]] struct {
	BaseResource[I]
}

BaseInstance provides the default implementation for Instance[I]. Embed this in concrete instance types.

func (*BaseInstance[I]) Create added in v0.2.0

func (b *BaseInstance[I]) Create(ctx context.Context) (I, gomerr.Gomerr)

func (*BaseInstance[I]) Delete added in v0.2.0

func (b *BaseInstance[I]) Delete(ctx context.Context) (I, gomerr.Gomerr)

func (*BaseInstance[I]) Id

func (b *BaseInstance[I]) Id() string

func (*BaseInstance[I]) Ids added in v0.4.0

func (b *BaseInstance[I]) Ids() []string

func (*BaseInstance[I]) NewQueryable

func (b *BaseInstance[I]) NewQueryable() data.Queryable

NewQueryable creates a Collection for querying instances of this type. Implements data.Persistable.

func (*BaseInstance[I]) PostCreate

func (*BaseInstance[I]) PostCreate(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PostDelete

func (*BaseInstance[I]) PostDelete(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PostRead

func (*BaseInstance[I]) PostRead(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PostUpdate

func (*BaseInstance[I]) PostUpdate(context.Context, I) gomerr.Gomerr

func (*BaseInstance[I]) PreCreate

func (*BaseInstance[I]) PreCreate(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PreDelete

func (*BaseInstance[I]) PreDelete(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PreList added in v0.4.0

func (*BaseInstance[I]) PreList(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PreRead

func (*BaseInstance[I]) PreRead(context.Context) gomerr.Gomerr

func (*BaseInstance[I]) PreUpdate

func (*BaseInstance[I]) PreUpdate(context.Context, I) gomerr.Gomerr

func (*BaseInstance[I]) Read added in v0.2.0

func (b *BaseInstance[I]) Read(ctx context.Context) (I, gomerr.Gomerr)

func (*BaseInstance[I]) RetryCreate added in v0.3.1

func (*BaseInstance[I]) RetryCreate(_ context.Context, ge gomerr.Gomerr) gomerr.Gomerr

func (*BaseInstance[I]) RetryDelete added in v0.3.1

func (*BaseInstance[I]) RetryDelete(_ context.Context, ge gomerr.Gomerr) gomerr.Gomerr

func (*BaseInstance[I]) RetryRead added in v0.3.1

func (*BaseInstance[I]) RetryRead(_ context.Context, ge gomerr.Gomerr) gomerr.Gomerr

func (*BaseInstance[I]) RetryUpdate added in v0.3.1

func (*BaseInstance[I]) RetryUpdate(_ context.Context, _ I, ge gomerr.Gomerr) gomerr.Gomerr

func (*BaseInstance[I]) TypeName

func (b *BaseInstance[I]) TypeName() string

func (*BaseInstance[I]) Update added in v0.2.0

func (b *BaseInstance[I]) Update(ctx context.Context) (I, gomerr.Gomerr)

type BaseResource

type BaseResource[T Resource[T]] struct {
	// contains filtered or unexported fields
}

BaseResource provides the default implementation for Resource[T]. Embed this in concrete resource types.

func (*BaseResource[T]) DoAction

func (b *BaseResource[T]) DoAction(ctx context.Context, action Action[T]) (T, gomerr.Gomerr)

func (*BaseResource[T]) RegisteredType added in v0.4.0

func (b *BaseResource[T]) RegisteredType() RegisteredType

func (*BaseResource[T]) Self added in v0.4.0

func (b *BaseResource[T]) Self() T

func (*BaseResource[T]) Subject

func (b *BaseResource[T]) Subject() auth.Subject

type Category

type Category string

Category indicates whether a resource is an Instance or Collection.

const (
	InstanceCategory   Category = "Instance"
	CollectionCategory Category = "Collection"
)

type Collectible

type Collectible interface {
	OnCollect(ctx context.Context, r any) gomerr.Gomerr
}

Collectible is implemented by instances that want to be notified when collected.

type Collection

type Collection[I Instance[I]] struct {
	Resource[*Collection[I]]

	Items      []I     `out:"+,includeempty"`
	NextToken  *string `in:"query.next_token" out:"+"`
	MaxResults int     `in:"query.max_results" validate:"intbetween(1,100)"` // TODO:  "intbetween(1,$.MaximumPageSize())
	// contains filtered or unexported fields
}

Collection holds a set of instances resulting from a query. Unlike Instance, Collection is parameterized only by its item type, delegating resource identity to the instance type.

func NewCollection added in v0.2.0

func NewCollection[I Instance[I]](proto I) *Collection[I]

func (*Collection[I]) ConsistencyType added in v0.2.0

func (c *Collection[I]) ConsistencyType() dynamodb.ConsistencyType

func (*Collection[I]) DoAction added in v0.2.0

func (c *Collection[I]) DoAction(ctx context.Context, action Action[*Collection[I]]) (*Collection[I], gomerr.Gomerr)

func (*Collection[I]) ItemTemplate added in v0.2.0

func (c *Collection[I]) ItemTemplate() any

func (*Collection[I]) MaximumPageSize added in v0.2.0

func (c *Collection[I]) MaximumPageSize() int

func (*Collection[I]) NextPageToken added in v0.2.0

func (c *Collection[I]) NextPageToken() *string

func (*Collection[I]) PostList added in v0.2.0

func (*Collection[I]) PostList(_ context.Context) gomerr.Gomerr

func (*Collection[I]) PreList added in v0.2.0

func (*Collection[I]) PreList(_ context.Context) gomerr.Gomerr

func (*Collection[I]) Query added in v0.2.0

func (c *Collection[I]) Query(ctx context.Context) gomerr.Gomerr

func (*Collection[I]) Results added in v0.2.0

func (c *Collection[I]) Results() []any

Results implements data.Queryable with []any.

func (*Collection[I]) RetryList added in v0.3.1

func (*Collection[I]) RetryList(_ context.Context, ge gomerr.Gomerr) gomerr.Gomerr

func (*Collection[I]) SetConsistencyType added in v0.2.0

func (c *Collection[I]) SetConsistencyType(consistencyType dynamodb.ConsistencyType)

func (*Collection[I]) SetNextPageToken added in v0.2.0

func (c *Collection[I]) SetNextPageToken(token *string)

func (*Collection[I]) SetResults added in v0.2.0

func (c *Collection[I]) SetResults(items []any)

SetResults implements data.Queryable with []any.

func (*Collection[I]) Subject added in v0.2.0

func (c *Collection[I]) Subject() auth.Subject

func (*Collection[I]) TypeName added in v0.2.0

func (c *Collection[I]) TypeName() string

type Instance

type Instance[I Resource[I]] interface {
	Resource[I]
	data.Persistable // TypeName() string, NewQueryable() data.Queryable
	Id() string
	PreCreate(context.Context) gomerr.Gomerr
	RetryCreate(context.Context, gomerr.Gomerr) gomerr.Gomerr
	PostCreate(context.Context) gomerr.Gomerr
	PreRead(context.Context) gomerr.Gomerr
	RetryRead(context.Context, gomerr.Gomerr) gomerr.Gomerr
	PostRead(context.Context) gomerr.Gomerr
	PreUpdate(context.Context, I) gomerr.Gomerr
	RetryUpdate(context.Context, I, gomerr.Gomerr) gomerr.Gomerr
	PostUpdate(context.Context, I) gomerr.Gomerr
	PreDelete(context.Context) gomerr.Gomerr
	RetryDelete(context.Context, gomerr.Gomerr) gomerr.Gomerr
	PostDelete(context.Context) gomerr.Gomerr
	PreList(context.Context) gomerr.Gomerr
}

Instance extends Resource for individual entities. Instances have an identity (Id) and support CRUD operations.

type NoOpAction

type NoOpAction[T any] struct{}

NoOpAction is an action that does nothing.

func (NoOpAction[T]) AppliesToCategory added in v0.2.0

func (NoOpAction[T]) AppliesToCategory() Category

func (NoOpAction[T]) Do

func (NoOpAction[T]) Do(_ context.Context, _ T) gomerr.Gomerr

func (NoOpAction[T]) FieldAccessPermissions

func (NoOpAction[T]) FieldAccessPermissions() auth.AccessPermissions

func (NoOpAction[T]) Name

func (NoOpAction[T]) Name() string

func (NoOpAction[T]) OnDoFailure

func (NoOpAction[T]) OnDoFailure(_ context.Context, _ T, ge gomerr.Gomerr) gomerr.Gomerr

func (NoOpAction[T]) OnDoSuccess

func (NoOpAction[T]) OnDoSuccess(_ context.Context, r T) (T, gomerr.Gomerr)

func (NoOpAction[T]) Pre

func (NoOpAction[T]) Pre(_ context.Context, _ T) gomerr.Gomerr

func (NoOpAction[T]) Retry added in v0.3.1

type OnCreateFailer

type OnCreateFailer[I Instance[I]] interface {
	OnCreateFailure(context.Context, gomerr.Gomerr) gomerr.Gomerr
}

OnCreateFailer is implemented by instances that want custom failure handling for create.

type OnDeleteFailer

type OnDeleteFailer[I Instance[I]] interface {
	OnDeleteFailure(context.Context, gomerr.Gomerr) gomerr.Gomerr
}

OnDeleteFailer is implemented by instances that want custom failure handling for delete.

type OnListFailer

type OnListFailer[I Instance[I]] interface {
	OnListFailure(context.Context, gomerr.Gomerr) gomerr.Gomerr
}

OnListFailer is implemented by collections that want custom failure handling for list.

type OnReadFailer

type OnReadFailer[I Instance[I]] interface {
	OnReadFailure(context.Context, gomerr.Gomerr) gomerr.Gomerr
}

OnReadFailer is implemented by instances that want custom failure handling for read.

type OnUpdateFailer

type OnUpdateFailer[I Instance[I]] interface {
	OnUpdateFailure(context.Context, gomerr.Gomerr) gomerr.Gomerr
}

OnUpdateFailer is implemented by instances that want custom failure handling for update.

type Option added in v0.2.0

type Option func(*registeredType)

func WithActions added in v0.2.0

func WithActions(actions map[any]func() AnyAction) Option

WithActions specifies the actions available for this resource.

func WithCollectionName added in v0.2.0

func WithCollectionName(name string) Option

WithCollectionName overrides the plural/collection path name.

func WithInstanceName added in v0.2.0

func WithInstanceName(name string) Option

WithInstanceName overrides the singular path name derived from the type name.

func WithParent added in v0.2.0

func WithParent[P Instance[P]]() Option

WithParent establishes a parent-child relationship with the specified resource type. Parents must be registered before their children.

func WithStore added in v0.2.0

func WithStore(store data.Store) Option

WithStore specifies the data store for this resource. If not provided, inherits from parent (if any).

type RegisteredType added in v0.2.0

type RegisteredType interface {
	InstanceName() string
	CollectionName() string
	Actions() map[any]func() AnyAction
	Parent() RegisteredType
	Children() []RegisteredType
	NewInstance(auth.Subject) any
	NewCollection(proto any) any
	Store() data.Store
}

type Registry added in v0.2.0

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds all registered resource types.

func NewRegistry added in v0.2.0

func NewRegistry() *Registry

NewRegistry creates a new registry for resource registration.

func (*Registry) RootTypes added in v0.2.0

func (r *Registry) RootTypes() []RegisteredType

RootTypes returns all root-level registered resources.

type Resource

type Resource[T any] interface {
	Subject() auth.Subject
	DoAction(context.Context, Action[T]) (T, gomerr.Gomerr)
	RegisteredType() RegisteredType
	// contains filtered or unexported methods
}

Resource is the base interface for all domain resources. The type parameter T is the concrete type implementing the interface (F-bounded polymorphism).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL