modulecontext

package
v0.362.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

Variables

This section is empty.

Functions

func DatabasesFromSecrets added in v0.214.0

func DatabasesFromSecrets(ctx context.Context, module string, secrets map[string][]byte) (map[string]Database, error)

DatabasesFromSecrets finds DSNs in secrets and creates a map of databases.

Secret keys should be in the format FTL_DSN_<MODULENAME>_<DBNAME>

Types

type Builder

type Builder ModuleContext

Builder is used to build a ModuleContext

func NewBuilder

func NewBuilder(module string) *Builder

NewBuilder creates a new blank Builder for the given module.

func (*Builder) AddConfigs

func (b *Builder) AddConfigs(configs map[string][]byte) *Builder

AddConfigs adds configuration values (as bytes) to the builder

func (*Builder) AddDatabases

func (b *Builder) AddDatabases(databases map[string]Database) *Builder

AddDatabases adds databases to the builder

func (*Builder) AddSecrets

func (b *Builder) AddSecrets(secrets map[string][]byte) *Builder

AddSecrets adds configuration values (as bytes) to the builder

func (*Builder) Build

func (b *Builder) Build() ModuleContext

func (*Builder) UpdateForTesting

func (b *Builder) UpdateForTesting(mockVerbs map[schema.RefKey]Verb, allowDirectVerbBehavior bool, leaseClient LeaseClient) *Builder

UpdateForTesting marks the builder as part of a test environment and adds mock verbs and flags for other test features.

type DBType

func (DBType) String

func (x DBType) String() string

type Database

type Database struct {
	DSN    string
	DBType DBType
	// contains filtered or unexported fields
}

Database represents a database connection based on a DSN It holds a private field for the database which is accessible through moduleCtx.GetDatabase(name)

func NewDatabase

func NewDatabase(dbType DBType, dsn string) (Database, error)

NewDatabase creates a Database that can be added to ModuleContext

func NewTestDatabase added in v0.209.0

func NewTestDatabase(dbType DBType, dsn string) (Database, error)

NewTestDatabase creates a Database that can be added to ModuleContext

Test databases can be used within module tests

type DirectBehavior

type DirectBehavior struct{}

DirectBehavior indicates that the verb should be executed by calling the function directly (for testing)

func (DirectBehavior) Call added in v0.202.1

func (DirectBehavior) Call(ctx context.Context, verb Verb, req any) (any, error)

type DynamicModuleContext added in v0.264.2

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

DynamicModuleContext provides up-to-date ModuleContext instances supplied by the controller

func FromContext

func FromContext(ctx context.Context) *DynamicModuleContext

FromContext returns the DynamicModuleContext attached to a context.

func NewDynamicContext added in v0.264.2

func NewDynamicContext(ctx context.Context, supplier ModuleContextSupplier, moduleName string) (*DynamicModuleContext, error)

NewDynamicContext creates a new DynamicModuleContext. This operation blocks until the first ModuleContext is supplied by the controller.

The DynamicModuleContext will continually update as updated ModuleContext's are streamed from the controller. This operation may time out if the first module context is not supplied quickly enough (fixed at 5 seconds).

func (*DynamicModuleContext) ApplyToContext added in v0.264.2

func (m *DynamicModuleContext) ApplyToContext(ctx context.Context) context.Context

ApplyToContext returns a Go context.Context with DynamicModuleContext added.

func (*DynamicModuleContext) CurrentContext added in v0.264.2

func (m *DynamicModuleContext) CurrentContext() ModuleContext

CurrentContext immediately returns the most recently updated ModuleContext

type LeaseClient added in v0.224.0

type LeaseClient interface {
	// Returns ResourceExhausted if the lease is held.
	Acquire(ctx context.Context, module string, key []string, ttl time.Duration) error
	Heartbeat(ctx context.Context, module string, key []string, ttl time.Duration) error
	Release(ctx context.Context, key []string) error
}

LeaseClient is the interface for acquiring, heartbeating and releasing leases

type MockBehavior

type MockBehavior struct {
	Mock Verb
}

MockBehavior indicates the verb has a mock implementation

func (MockBehavior) Call added in v0.202.1

func (b MockBehavior) Call(ctx context.Context, _ Verb, req any) (any, error)

type ModuleContext

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

ModuleContext holds the context needed for a module, including configs, secrets and DSNs

ModuleContext is immutable

func Empty added in v0.248.0

func Empty(module string) ModuleContext

func FromProto

func FromProto(response *ftlv1.ModuleContextResponse) (ModuleContext, error)

func (ModuleContext) BehaviorForVerb

func (m ModuleContext) BehaviorForVerb(ref schema.Ref) (optional.Option[VerbBehavior], error)

BehaviorForVerb returns what to do to execute a verb

This allows module context to dictate behavior based on testing options Returning optional.Nil indicates the verb should be executed normally via the controller

func (ModuleContext) GetConfig

func (m ModuleContext) GetConfig(name string, value any) error

GetConfig reads a configuration value for the module.

"value" must be a pointer to a Go type that can be unmarshalled from JSON.

func (ModuleContext) GetDatabase

func (m ModuleContext) GetDatabase(name string, dbType DBType) (string, error)

GetDatabase gets a database DSN by name and type.

Returns an error if no database with that name is found or it is not the expected type. When in a testing context (via ftltest), an error is returned if the database is not a test database.

func (ModuleContext) GetSecret

func (m ModuleContext) GetSecret(name string, value any) error

GetSecret reads a secret value for the module.

"value" must be a pointer to a Go type that can be unmarshalled from JSON.

func (ModuleContext) MockLeaseClient added in v0.224.0

func (m ModuleContext) MockLeaseClient() optional.Option[LeaseClient]

MockLeaseClient provides a mock lease client when testing

func (ModuleContext) ToProto

ToProto converts a ModuleContext to a proto response.

type ModuleContextSupplier added in v0.264.2

type ModuleContextSupplier interface {
	Subscribe(ctx context.Context, moduleName string, sink func(ctx context.Context, moduleContext ModuleContext), errorRetryCallback func(err error) bool)
}

func NewModuleContextSupplier added in v0.264.2

func NewModuleContextSupplier(client ftlv1connect.ModuleServiceClient) ModuleContextSupplier

type Verb added in v0.202.1

type Verb func(ctx context.Context, req any) (resp any, err error)

Verb is a function that takes a request and returns a response but is not constrained by request/response type like ftl.Verb

It is used for definitions of mock verbs as well as real implementations of verbs to directly execute

type VerbBehavior

type VerbBehavior interface {
	Call(ctx context.Context, verb Verb, request any) (any, error)
}

VerbBehavior indicates how to execute a verb

Jump to

Keyboard shortcuts

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