Skip to content

Commit

Permalink
add map key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseli committed Jun 5, 2024
1 parent 73f9f47 commit a0f98b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 16 additions & 2 deletions go-runtime/ftl/ftltest/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package ftltest

import (
"context"
"fmt"
"reflect"
"strings"

"github.com/TBD54566975/ftl/go-runtime/internal"
"github.com/TBD54566975/ftl/internal/modulecontext"
Expand Down Expand Up @@ -31,12 +33,12 @@ func (f *fakeFTL) PublishEvent(ctx context.Context, topic string, event any) err
}

func (f *fakeFTL) addMapMock(ctx context.Context, mapper any, mockMap func(context.Context) (any, error)) {

Check failure on line 35 in go-runtime/ftl/ftltest/fake.go

View workflow job for this annotation

GitHub Actions / Lint

`(*fakeFTL).addMapMock` - `ctx` is unused (unparam)
key := reflect.ValueOf(mapper).Pointer()
key := makeMapKey(mapper)
f.mockMaps[key] = mockMap
}

func (f *fakeFTL) CallMap(ctx context.Context, mapper any, mapImpl func(context.Context) (any, error)) any {
key := reflect.ValueOf(mapper).Pointer()
key := makeMapKey(mapper)
mockMap, ok := f.mockMaps[key]
if ok {
return actuallyCallMap(ctx, mockMap)
Expand All @@ -48,6 +50,18 @@ func (f *fakeFTL) CallMap(ctx context.Context, mapper any, mapImpl func(context.
return nil

Check failure on line 50 in go-runtime/ftl/ftltest/fake.go

View workflow job for this annotation

GitHub Actions / Lint

unreachable: unreachable code (govet)
}

func makeMapKey(mapper any) uintptr {
v := reflect.ValueOf(mapper)
if v.Kind() != reflect.Pointer {
panic("fakeFTL received object that was not a pointer, expected *MapHandle")
}
underlying := v.Elem().Type().Name()
if !strings.HasPrefix(underlying, "MapHandle[") {
panic(fmt.Sprintf("fakeFTL received *%s, expected *MapHandle", underlying))
}
return v.Pointer()
}

func actuallyCallMap(ctx context.Context, impl modulecontext.Map) any {
out, err := impl(ctx)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go-runtime/ftl/testdata/go/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/alecthomas/assert/v2"
)

/*func TestGet(t *testing.T) {
func TestGet(t *testing.T) {
ctx := ftltest.Context(ftltest.WithMapsAllowed())
got := m.Get(ctx)
assert.Equal(t, got, 9)
Expand All @@ -19,7 +19,7 @@ func TestPanicsWithoutExplicitlyAllowingMaps(t *testing.T) {
assert.Panics(t, func() {
m.Get(ctx)
})
}*/
}

func TestMockGet(t *testing.T) {
mockOut := 123
Expand Down

0 comments on commit a0f98b3

Please sign in to comment.