Skip to content

Commit

Permalink
fix: external types in go genned files (#2861)
Browse files Browse the repository at this point in the history
  • Loading branch information
worstell committed Sep 27, 2024
1 parent 909706c commit 468b2c3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
26 changes: 11 additions & 15 deletions go-runtime/compile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,7 @@ func (b *mainModuleContextBuilder) build(goModVersion, ftlVersion, projectName s
}
ctx.MainCtx.SumTypes = append(ctx.MainCtx.SumTypes, n)
case goExternalType:
if isLocal {
ctx.TypesCtx.ExternalTypes = append(ctx.TypesCtx.ExternalTypes, n)
}
ctx.TypesCtx.ExternalTypes = append(ctx.TypesCtx.ExternalTypes, n)
ctx.MainCtx.ExternalTypes = append(ctx.MainCtx.ExternalTypes, n)
}
return next()
Expand Down Expand Up @@ -627,9 +625,6 @@ func (b *mainModuleContextBuilder) getGoType(module *schema.Module, node schema.
isLocal = b.visitingMainModule(module.Name)
switch n := node.(type) {
case *schema.Ref:
if n.Module != "" && n.Module != b.mainModule.Name {
return optional.None[goType](), isLocal, nil
}
maybeResolved, maybeModule := b.sch.ResolveWithModule(n)
resolved, ok := maybeResolved.Get()
if !ok {
Expand Down Expand Up @@ -682,24 +677,25 @@ func (b *mainModuleContextBuilder) processSumType(module *schema.Module, enum *s
var err error
if !b.visitingMainModule(moduleName) {
nt, err = nativeTypeFromQualifiedName("ftl/" + moduleName + "." + enum.Name)
} else if nn, ok := b.nativeNames[enum]; ok {
nt, err = b.getNativeType(nn)
} else {
if nn, ok := b.nativeNames[enum]; ok {
nt, err = b.getNativeType(nn)
} else {
return goSumType{}, fmt.Errorf("missing native name for enum %s", enum.Name)
}
return goSumType{}, fmt.Errorf("missing native name for enum %s", enum.Name)
}
if err != nil {
return goSumType{}, err
}

variants := make([]goSumTypeVariant, 0, len(enum.Variants))
for _, v := range enum.Variants {
nn, ok := b.nativeNames[v]
if !ok {
return goSumType{}, fmt.Errorf("missing native name for enum variant %s", v.Name)
var vnt nativeType
if !b.visitingMainModule(moduleName) {
vnt, err = nativeTypeFromQualifiedName("ftl/" + moduleName + "." + v.Name)
} else if nn, ok := b.nativeNames[v]; ok {
vnt, err = b.getNativeType(nn)
} else {
return goSumType{}, fmt.Errorf("missing native name for enum variant %s", enum.Name)
}
vnt, err := b.getNativeType(nn)
if err != nil {
return goSumType{}, err
}
Expand Down
4 changes: 4 additions & 0 deletions go-runtime/schema/testdata/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ func OtherFunc(ctx context.Context, req Request) (Response, error) {
type NonFTLType struct {
Message string
}

type AnotherNonFTLType struct {
Message string
}
4 changes: 2 additions & 2 deletions internal/buildengine/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDiscoverModules(t *testing.T) {
DeployDir: ".ftl",
Schema: "schema.pb",
Errors: "errors.pb",
Watch: []string{"**/*.go", "go.mod", "go.sum", "../../../../go-runtime/ftl/**/*.go"},
Watch: []string{"**/*.go", "go.mod", "go.sum", "../../../../go-runtime/ftl/**/*.go", "../../../../go-runtime/schema/testdata/**/*.go"},
},
},
{
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestDiscoverModules(t *testing.T) {
DeployDir: ".ftl",
Schema: "schema.pb",
Errors: "errors.pb",
Watch: []string{"**/*.go", "go.mod", "go.sum", "../../../../go-runtime/ftl/**/*.go"},
Watch: []string{"**/*.go", "go.mod", "go.sum", "../../../../go-runtime/ftl/**/*.go", "../../../../go-runtime/schema/testdata/**/*.go"},
},
},
}
Expand Down
4 changes: 4 additions & 0 deletions internal/buildengine/testdata/another/another.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata"
)

//ftl:enum export
Expand Down Expand Up @@ -48,3 +49,6 @@ type EchoResponse struct {
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}

//ftl:typealias export
type External lib.AnotherNonFTLType
5 changes: 5 additions & 0 deletions internal/buildengine/testdata/other/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/TBD54566975/ftl/go-runtime/ftl" // Import the FTL SDK.
lib "github.com/TBD54566975/ftl/go-runtime/schema/testdata"

"ftl/another"
)
Expand Down Expand Up @@ -76,6 +77,7 @@ type EchoRequest struct {
Name ftl.Option[string] `json:"name"`
ExternalSumType another.TypeEnum
ExternalNestedSumType another.TransitiveTypeEnum
ExternalExternalType another.External
}

type EchoResponse struct {
Expand All @@ -86,3 +88,6 @@ type EchoResponse struct {
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}

//ftl:typealias
type External lib.NonFTLType
8 changes: 8 additions & 0 deletions internal/buildengine/testdata/type_registry_main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 468b2c3

Please sign in to comment.