Skip to content

Commit

Permalink
fix: WhenMap() now typesafe, ftl.Map returns ptr (#1777)
Browse files Browse the repository at this point in the history
- `ftltest.WhenMap(...)` included a func that returned `any` when it
should be strongly types with the map's return type
- `ftl.Map` was returning `MapHandle` where as `handle.Get()` was
defined on `*MapHandle` and `ftltest.WhenMap(...)` expected a pointer as
well. I've made it all consistently use `*MapHandle` so you don't need
to do `&handle` anywhere
  • Loading branch information
matt2e authored Jun 14, 2024
1 parent eca29df commit 8245b90
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions go-runtime/ftl/ftltest/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/common/configuration"
"github.com/TBD54566975/ftl/go-runtime/ftl"
"github.com/TBD54566975/ftl/go-runtime/internal"
"github.com/alecthomas/types/optional"
)
Expand Down Expand Up @@ -119,9 +120,11 @@ func (f *fakeFTL) FSMSend(ctx context.Context, fsm string, instance string, even
//
// mockMap provides the whole mock implemention, so it gets called in place of both `fn`
// and `getter` in ftl.Map.
func (f *fakeFTL) addMapMock(mapper any, mockMap func(context.Context) (any, error)) {
func addMapMock[T, U any](f *fakeFTL, mapper *ftl.MapHandle[T, U], mockMap func(context.Context) (U, error)) {
key := makeMapKey(mapper)
f.mockMaps[key] = mockMap
f.mockMaps[key] = func(ctx context.Context) (any, error) {
return mockMap(ctx)
}
}

func (f *fakeFTL) startAllowingMapCalls() {
Expand Down
4 changes: 2 additions & 2 deletions go-runtime/ftl/ftltest/ftltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,10 @@ func WithCallsAllowedWithinModule() Option {
// }),
// // ... other options
// )
func WhenMap[T, U any](mapper *ftl.MapHandle[T, U], fake func(context.Context) (any, error)) Option {
func WhenMap[T, U any](mapper *ftl.MapHandle[T, U], fake func(context.Context) (U, error)) Option {
return func(ctx context.Context, state *OptionsState) error {
fftl := internal.FromContext(ctx).(*fakeFTL) //nolint:forcetypeassert
fftl.addMapMock(mapper, fake)
addMapMock(fftl, mapper, fake)
return nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions go-runtime/ftl/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (mh *MapHandle[T, U]) Get(ctx context.Context) U {
}

// Map an FTL resource type to a new type.
func Map[T, U any](getter Handle[T], fn func(context.Context, T) (U, error)) MapHandle[T, U] {
return MapHandle[T, U]{
func Map[T, U any](getter Handle[T], fn func(context.Context, T) (U, error)) *MapHandle[T, U] {
return &MapHandle[T, U]{
fn: fn,
handle: getter,
}
Expand Down
2 changes: 1 addition & 1 deletion go-runtime/ftl/testdata/go/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPanicsWithoutExplicitlyAllowingMaps(t *testing.T) {

func TestMockGet(t *testing.T) {
mockOut := 123
ctx := ftltest.Context(ftltest.WhenMap(&m, func(ctx context.Context) (any, error) {
ctx := ftltest.Context(ftltest.WhenMap(m, func(ctx context.Context) (int, error) {
return mockOut, nil
}))
got := m.Get(ctx)
Expand Down

0 comments on commit 8245b90

Please sign in to comment.