Skip to content

Commit

Permalink
simplify by removing builder
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Apr 22, 2024
1 parent 3e761c3 commit 30da785
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 102 deletions.
101 changes: 0 additions & 101 deletions go-runtime/modulecontext/builder.go

This file was deleted.

50 changes: 50 additions & 0 deletions go-runtime/modulecontext/modulecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package modulecontext
import (
"context"

ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
cf "github.com/TBD54566975/ftl/common/configuration"
"github.com/alecthomas/types/optional"
)

// ModuleContext holds the context needed for a module, including configs, secrets and DSNs
Expand All @@ -13,6 +15,54 @@ type ModuleContext struct {
dbProvider *DBProvider
}

func NewFromProto(ctx context.Context, moduleName string, response *ftlv1.ModuleContextResponse) (*ModuleContext, error) {
cm, err := newInMemoryConfigManager[cf.Configuration](ctx)
if err != nil {
return nil, err
}
sm, err := newInMemoryConfigManager[cf.Secrets](ctx)
if err != nil {
return nil, err
}
moduleCtx := &ModuleContext{
configManager: cm,
secretsManager: sm,
dbProvider: NewDBProvider(),
}

if err := addConfigOrSecrets[cf.Configuration](ctx, *moduleCtx.configManager, response.Configs, moduleName); err != nil {
return nil, err
}
if err := addConfigOrSecrets[cf.Secrets](ctx, *moduleCtx.secretsManager, response.Secrets, moduleName); err != nil {
return nil, err
}
for _, entry := range response.Databases {
if err = moduleCtx.dbProvider.Add(entry.Name, DBType(entry.Type), entry.Dsn); err != nil {
return nil, err
}
}
return moduleCtx, nil
}

func newInMemoryConfigManager[R cf.Role](ctx context.Context) (*cf.Manager[R], error) {
provider := cf.NewInMemoryProvider[R]()
resolver := cf.NewInMemoryResolver[R]()
manager, err := cf.New(ctx, resolver, []cf.Provider[R]{provider})
if err != nil {
return nil, err
}
return manager, nil
}

func addConfigOrSecrets[R cf.Role](ctx context.Context, manager cf.Manager[R], valueMap map[string][]byte, moduleName string) error {
for name, data := range valueMap {
if err := manager.SetData(ctx, cf.Ref{Module: optional.Some(moduleName), Name: name}, data); err != nil {
return err
}
}
return nil
}

// ApplyToContext sets up the context so that configurations, secrets and DSNs can be retreived
// Each of these components have accessors to get a manager back from the context
func (m *ModuleContext) ApplyToContext(ctx context.Context) context.Context {
Expand Down
2 changes: 1 addition & 1 deletion go-runtime/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewUserVerbServer(moduleName string, handlers ...Handler) plugin.Constructo
if err != nil {
return nil, nil, fmt.Errorf("could not get config: %w", err)
}
moduleCtx, err := modulecontext.NewBuilderFromProto(moduleName, resp.Msg).Build(ctx)
moduleCtx, err := modulecontext.NewFromProto(ctx, moduleName, resp.Msg)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 30da785

Please sign in to comment.