From f8ce99dfbb1598f90dc98fd9dde9cdda3708027f Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Mon, 29 Apr 2024 09:22:30 +1000 Subject: [PATCH] refactor: replace inmemoryprovider with inlineprovider (#1348) In-memory provider was being used to hold config and secrets in memory, but inline provider already solved that. Removing to simplify things --- common/configuration/in_memory_provider.go | 43 ------------------- go-runtime/modulecontext/from_proto.go | 4 +- .../modulecontext/module_context_test.go | 8 +++- 3 files changed, 9 insertions(+), 46 deletions(-) delete mode 100644 common/configuration/in_memory_provider.go diff --git a/common/configuration/in_memory_provider.go b/common/configuration/in_memory_provider.go deleted file mode 100644 index 8a4ad73b1c..0000000000 --- a/common/configuration/in_memory_provider.go +++ /dev/null @@ -1,43 +0,0 @@ -package configuration - -import ( - "context" - "net/url" -) - -// InMemoryProvider is a configuration provider that keeps values in memory -type InMemoryProvider[R Role] struct { - values map[Ref][]byte -} - -var _ MutableProvider[Configuration] = &InMemoryProvider[Configuration]{} - -func NewInMemoryProvider[R Role]() *InMemoryProvider[R] { - return &InMemoryProvider[R]{values: map[Ref][]byte{}} -} - -func (p *InMemoryProvider[R]) Role() R { var r R; return r } -func (p *InMemoryProvider[R]) Key() string { return "inmemory" } - -func (p *InMemoryProvider[R]) Load(ctx context.Context, ref Ref, key *url.URL) ([]byte, error) { - if bytes, found := p.values[ref]; found { - return bytes, nil - } - return nil, ErrNotFound -} - -func (p *InMemoryProvider[R]) Writer() bool { - return true -} - -// Store a configuration value and return its key. -func (p *InMemoryProvider[R]) Store(ctx context.Context, ref Ref, value []byte) (*url.URL, error) { - p.values[ref] = value - return &url.URL{Scheme: p.Key()}, nil -} - -// Delete a configuration value. -func (p *InMemoryProvider[R]) Delete(ctx context.Context, ref Ref) error { - delete(p.values, ref) - return nil -} diff --git a/go-runtime/modulecontext/from_proto.go b/go-runtime/modulecontext/from_proto.go index 759e09e049..4271c16b2f 100644 --- a/go-runtime/modulecontext/from_proto.go +++ b/go-runtime/modulecontext/from_proto.go @@ -33,7 +33,9 @@ func FromProto(ctx context.Context, response *ftlv1.ModuleContextResponse) (*Mod } func newInMemoryConfigManager[R cf.Role](ctx context.Context, config map[string][]byte) (*cf.Manager[R], error) { - provider := cf.NewInMemoryProvider[R]() + provider := cf.InlineProvider[R]{ + Inline: true, + } refs := map[cf.Ref]*url.URL{} for name, data := range config { ref := cf.Ref{Name: name} diff --git a/go-runtime/modulecontext/module_context_test.go b/go-runtime/modulecontext/module_context_test.go index a3d1fb1cb6..0d28919923 100644 --- a/go-runtime/modulecontext/module_context_test.go +++ b/go-runtime/modulecontext/module_context_test.go @@ -21,13 +21,17 @@ func TestConfigPriority(t *testing.T) { moduleName := "test" - cp := cf.NewInMemoryProvider[cf.Configuration]() + cp := cf.InlineProvider[cf.Configuration]{ + Inline: true, + } cr := cf.NewInMemoryResolver[cf.Configuration]() cm, err := cf.New(ctx, cr, []cf.Provider[cf.Configuration]{cp}) assert.NoError(t, err) ctx = cf.ContextWithConfig(ctx, cm) - sp := cf.NewInMemoryProvider[cf.Secrets]() + sp := cf.InlineProvider[cf.Secrets]{ + Inline: true, + } sr := cf.NewInMemoryResolver[cf.Secrets]() sm, err := cf.New(ctx, sr, []cf.Provider[cf.Secrets]{sp}) assert.NoError(t, err)