Skip to content

Commit

Permalink
fix: handle custom local names for widened types (#2401)
Browse files Browse the repository at this point in the history
fixes #2397

`pass.Pkg.Imports()` did not always* include the local name alias from
the import statement, so instead we need to do our checks via `PkgName`
which does include it.

*it includes it in the example in our tests, but didn't in the case that
our partner project was hitting...
I've added another test case that previously failed like theirs did.
  • Loading branch information
matt2e authored Aug 19, 2024
1 parent f9214f8 commit 13355eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions go-runtime/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ func TestExtractModuleSchemaTwo(t *testing.T) {
}
actual := schema.Normalise(r.Module)
expected := `module two {
typealias BackoffAlias Any
+typemap go "github.com/jpillora/backoff.Backoff"
typealias ExplicitAliasAlias Any
+typemap kotlin "com.foo.bar.NonFTLType"
+typemap go "github.com/TBD54566975/ftl/go-runtime/schema/testdata.lib.NonFTLType"
Expand Down
6 changes: 4 additions & 2 deletions go-runtime/schema/testdata/two/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ go 1.22.2

replace github.com/TBD54566975/ftl => ../../../..

require github.com/TBD54566975/ftl v0.150.3
require (
github.com/TBD54566975/ftl v0.150.3
github.com/jpillora/backoff v1.0.0
)

require (
connectrpc.com/connect v1.16.2 // indirect
Expand All @@ -26,7 +29,6 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go-runtime/schema/testdata/two/two.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/TBD54566975/ftl/go-runtime/ftl"
lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata"
libbackoff "github.com/jpillora/backoff"

"ftl/builtin"
)
Expand Down Expand Up @@ -103,6 +104,9 @@ type TransitiveAliasAlias = lib.NonFTLType

type TransitiveAlias lib.NonFTLType

//ftl:typealias
type BackoffAlias libbackoff.Backoff

func transitiveVerbCall(ctx context.Context, req Payload[string]) error {
_, err := ftl.Call(ctx, Two, req)
if err != nil {
Expand Down
20 changes: 9 additions & 11 deletions go-runtime/schema/typealias/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ func qualifiedNameFromSelectorExpr(pass *analysis.Pass, node ast.Node) string {
if !ok {
return ""
}
for _, im := range pass.Pkg.Imports() {
if im.Name() != ident.Name {
continue
}
fqName := im.Path()
if parts := strings.Split(im.Path(), "/"); parts[len(parts)-1] != ident.Name {
// if package differs from the directory name, add the package name to the fqName
fqName = fqName + "." + ident.Name
}
return fqName + "." + se.Sel.Name
pkgName, ok := pass.TypesInfo.ObjectOf(ident).(*types.PkgName)
if !ok {
return ""
}
fqName := pkgName.Imported().Path()
if parts := strings.Split(fqName, "/"); parts[len(parts)-1] != pkgName.Imported().Name() {
// if package differs from the directory name, add the package name to the fqName
fqName = fqName + "." + ident.Name
}
return ""
return fqName + "." + se.Sel.Name
}

0 comments on commit 13355eb

Please sign in to comment.