modulecontext

package
v0.217.8 Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

Variables

This section is empty.

Functions

func DSNSecretKey added in v0.214.0

func DSNSecretKey(module, name string) string

DSNSecretKey returns the key for the secret that is expected to hold the DSN for a database.

The format is FTL_DSN_<MODULE>_<DBNAME>

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>

func GetDSNFromSecret added in v0.214.0

func GetDSNFromSecret(module, name string, secrets map[string][]byte) (string, error)

GetDSNFromSecret returns the DSN for a database from the relevant secret

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) *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 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 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 FromContext

func FromContext(ctx context.Context) ModuleContext

FromContext returns the ModuleContext attached to a context.

func FromProto

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

func (ModuleContext) ApplyToContext

func (m ModuleContext) ApplyToContext(ctx context.Context) context.Context

ApplyToContext returns a Go context.Context with ModuleContext added.

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) (*sql.DB, error)

GetDatabase gets a database connection

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) ToProto

ToProto converts a ModuleContext to a proto response.

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