From 30da78551385f7e366e7a49384de75594b501e4f Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Mon, 22 Apr 2024 16:00:11 +1000 Subject: [PATCH] simplify by removing builder --- go-runtime/modulecontext/builder.go | 101 ---------------------- go-runtime/modulecontext/modulecontext.go | 50 +++++++++++ go-runtime/server/server.go | 2 +- 3 files changed, 51 insertions(+), 102 deletions(-) delete mode 100644 go-runtime/modulecontext/builder.go diff --git a/go-runtime/modulecontext/builder.go b/go-runtime/modulecontext/builder.go deleted file mode 100644 index e4ee42285f..0000000000 --- a/go-runtime/modulecontext/builder.go +++ /dev/null @@ -1,101 +0,0 @@ -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" -) - -type dsnEntry struct { - dbType DBType - dsn string -} - -// Builder is used to set up a ModuleContext with configs, secrets and DSNs -// It is able to parse a ModuleContextResponse -type Builder struct { - moduleName string - configs map[string][]byte - secrets map[string][]byte - dsns map[string]dsnEntry -} - -func NewBuilder(moduleName string) *Builder { - return &Builder{ - moduleName: moduleName, - configs: map[string][]byte{}, - secrets: map[string][]byte{}, - dsns: map[string]dsnEntry{}, - } -} - -func NewBuilderFromProto(moduleName string, response *ftlv1.ModuleContextResponse) *Builder { - configs := map[string][]byte{} - for name, bytes := range response.Configs { - configs[name] = bytes - } - secrets := map[string][]byte{} - for name, bytes := range response.Secrets { - secrets[name] = bytes - } - dsns := map[string]dsnEntry{} - for _, d := range response.Databases { - dsns[d.Name] = dsnEntry{dbType: DBType(d.Type), dsn: d.Dsn} - } - return &Builder{ - moduleName: moduleName, - configs: configs, - secrets: secrets, - dsns: dsns, - } -} - -func (b *Builder) Build(ctx context.Context) (*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 := buildConfigOrSecrets[cf.Configuration](ctx, *moduleCtx.configManager, b.configs, b.moduleName); err != nil { - return nil, err - } - if err := buildConfigOrSecrets[cf.Secrets](ctx, *moduleCtx.secretsManager, b.secrets, b.moduleName); err != nil { - return nil, err - } - for name, entry := range b.dsns { - if err = moduleCtx.dbProvider.Add(name, entry.dbType, 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 buildConfigOrSecrets[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 -} diff --git a/go-runtime/modulecontext/modulecontext.go b/go-runtime/modulecontext/modulecontext.go index 64858bc2cc..0f9b2f2441 100644 --- a/go-runtime/modulecontext/modulecontext.go +++ b/go-runtime/modulecontext/modulecontext.go @@ -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 @@ -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 { diff --git a/go-runtime/server/server.go b/go-runtime/server/server.go index f8f8f02ce5..b305d97645 100644 --- a/go-runtime/server/server.go +++ b/go-runtime/server/server.go @@ -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 }