Skip to content

Commit

Permalink
chore: simplify moduleContexttoProto
Browse files Browse the repository at this point in the history
It's preferable to pass only exactly what's needed into a function. In
this case it was receiving a full slice of modules, and a module name,
and only allowing a single entry.
  • Loading branch information
alecthomas committed Apr 24, 2024
1 parent bbc214d commit f770f24
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
6 changes: 5 additions & 1 deletion backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,11 @@ func (s *Service) GetModuleContext(ctx context.Context, req *connect.Request[ftl
if err != nil {
return nil, err
}
return moduleContextToProto(ctx, req.Msg.Module, schemas)
schema, ok := slices.Find(schemas, func(s *schema.Module) bool { return s.Name == req.Msg.Module })
if !ok {
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("module %q not found", req.Msg.Module))
}
return moduleContextToProto(ctx, schema)
}

func (s *Service) Call(ctx context.Context, req *connect.Request[ftlv1.CallRequest]) (*connect.Response[ftlv1.CallResponse], error) {
Expand Down
20 changes: 5 additions & 15 deletions backend/controller/module_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,31 @@ import (
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/schema"
cf "github.com/TBD54566975/ftl/common/configuration"
"github.com/TBD54566975/ftl/internal/slices"
)

func moduleContextToProto(ctx context.Context, name string, schemas []*schema.Module) (*connect.Response[ftlv1.ModuleContextResponse], error) {
schemas = slices.Filter(schemas, func(s *schema.Module) bool {
return s.Name == name
})
if len(schemas) == 0 {
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("no schema found for module %q", name))
} else if len(schemas) > 1 {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("multiple schemas found for module %q", name))
}

func moduleContextToProto(ctx context.Context, module *schema.Module) (*connect.Response[ftlv1.ModuleContextResponse], error) {
// configs
configManager := cf.ConfigFromContext(ctx)
configMap, err := bytesMapFromConfigManager(ctx, configManager, name)
configMap, err := bytesMapFromConfigManager(ctx, configManager, module.Name)
if err != nil {
return nil, err
}

// secrets
secretsManager := cf.SecretsFromContext(ctx)
secretsMap, err := bytesMapFromConfigManager(ctx, secretsManager, name)
secretsMap, err := bytesMapFromConfigManager(ctx, secretsManager, module.Name)
if err != nil {
return nil, err
}

// DSNs
dsnProtos := []*ftlv1.ModuleContextResponse_DSN{}
for _, decl := range schemas[0].Decls {
for _, decl := range module.Decls {
dbDecl, ok := decl.(*schema.Database)
if !ok {
continue
}
key := fmt.Sprintf("FTL_POSTGRES_DSN_%s_%s", strings.ToUpper(name), strings.ToUpper(dbDecl.Name))
key := fmt.Sprintf("FTL_POSTGRES_DSN_%s_%s", strings.ToUpper(module.Name), strings.ToUpper(dbDecl.Name))
dsn, ok := os.LookupEnv(key)
if !ok {
return nil, fmt.Errorf("missing environment variable %q", key)
Expand Down
11 changes: 4 additions & 7 deletions backend/controller/module_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"testing"

"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/schema"
cf "github.com/TBD54566975/ftl/common/configuration"
"github.com/TBD54566975/ftl/internal/log"
"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types/optional"
)

func TestModuleContextProto(t *testing.T) {
Expand Down Expand Up @@ -41,11 +42,7 @@ func TestModuleContextProto(t *testing.T) {
assert.NoError(t, cm.Set(ctx, cf.Ref{Module: optional.None[string](), Name: key}, globalStrValue))
}

response, err := moduleContextToProto(ctx, moduleName, []*schema.Module{
{
Name: moduleName,
},
})
response, err := moduleContextToProto(ctx, &schema.Module{Name: moduleName})
assert.NoError(t, err)

for i := range 50 {
Expand Down
10 changes: 10 additions & 0 deletions internal/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ func FlatMap[T, U any](slice []T, fn func(T) []U) []U {
}
return result
}

func Find[T any](slice []T, fn func(T) bool) (T, bool) {
for _, v := range slice {
if fn(v) {
return v, true
}
}
var zero T
return zero, false
}

0 comments on commit f770f24

Please sign in to comment.