Skip to content

Commit

Permalink
fix: ignore non-exported decls in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman committed May 2, 2024
1 parent 4dc44df commit ec09a41
Show file tree
Hide file tree
Showing 17 changed files with 320 additions and 50 deletions.
29 changes: 19 additions & 10 deletions buildengine/build_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,52 @@ func TestGenerateGoModule(t *testing.T) {
schema.Builtins(),
{Name: "other", Decls: []schema.Decl{
&schema.Enum{
Name: "Color",
Type: &schema.String{},
Name: "Color",
Export: true,
Type: &schema.String{},
Variants: []*schema.EnumVariant{
{Name: "Red", Value: &schema.StringValue{Value: "Red"}},
{Name: "Blue", Value: &schema.StringValue{Value: "Blue"}},
{Name: "Green", Value: &schema.StringValue{Value: "Green"}},
},
},
&schema.Enum{
Name: "ColorInt",
Type: &schema.Int{},
Name: "ColorInt",
Export: true,
Type: &schema.Int{},
Variants: []*schema.EnumVariant{
{Name: "RedInt", Value: &schema.IntValue{Value: 0}},
{Name: "BlueInt", Value: &schema.IntValue{Value: 1}},
{Name: "GreenInt", Value: &schema.IntValue{Value: 2}},
},
},
&schema.Data{Name: "EchoRequest"},
&schema.Data{Name: "EchoResponse"},
&schema.Data{Name: "InternalRequest"},
&schema.Data{Name: "InternalResponse"},
&schema.Data{Name: "EchoRequest", Export: true},
&schema.Data{Name: "EchoResponse", Export: true},
&schema.Verb{
Name: "echo",
Export: true,
Request: &schema.Ref{Name: "EchoRequest"},
Response: &schema.Ref{Name: "EchoResponse"},
},
&schema.Data{Name: "SinkReq"},
&schema.Data{Name: "SinkReq", Export: true},
&schema.Verb{
Name: "sink",
Export: true,
Request: &schema.Ref{Name: "SinkReq"},
Response: &schema.Unit{},
},
&schema.Data{Name: "SourceResp"},
&schema.Data{Name: "SourceResp", Export: true},
&schema.Verb{
Name: "source",
Export: true,
Request: &schema.Unit{},
Response: &schema.Ref{Name: "SourceResp"},
},
&schema.Verb{
Name: "nothing",
Export: true,
Request: &schema.Unit{},
Response: &schema.Unit{},
},
Expand Down Expand Up @@ -132,10 +140,11 @@ func TestMetadataImportsExcluded(t *testing.T) {
Modules: []*schema.Module{
schema.Builtins(),
{Name: "test", Decls: []schema.Decl{
&schema.Data{Name: "Req"},
&schema.Data{Name: "Resp"},
&schema.Data{Name: "Req", Export: true},
&schema.Data{Name: "Resp", Export: true},
&schema.Verb{
Name: "call",
Export: true,
Request: &schema.Ref{Name: "Req"},
Response: &schema.Ref{Name: "Resp"},
Metadata: []schema.Metadata{
Expand Down
4 changes: 2 additions & 2 deletions buildengine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestEngine(t *testing.T) {
t.SkipNow()
}
ctx := log.ContextWithNewDefaultLogger(context.Background())
engine, err := buildengine.New(ctx, nil, []string{"testdata/projects/alpha", "testdata/projects/another"}, nil)
engine, err := buildengine.New(ctx, nil, []string{"testdata/projects/alpha", "testdata/projects/other", "testdata/projects/another"}, nil)
assert.NoError(t, err)

defer engine.Close()
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestEngine(t *testing.T) {
expected := map[string][]string{
"alpha": {"another", "other", "builtin"},
"another": {"builtin"},
"other": {},
"other": {"builtin"},
"builtin": {},
}
graph, err := engine.Graph()
Expand Down
2 changes: 1 addition & 1 deletion buildengine/testdata/projects/another/another.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EchoResponse struct {
Message string `json:"message"`
}

//ftl:verb
//ftl:verb export
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
6 changes: 3 additions & 3 deletions buildengine/testdata/projects/other/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ require (
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.17.0 // indirect
Expand Down
40 changes: 20 additions & 20 deletions buildengine/testdata/projects/other/go.sum

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

2 changes: 1 addition & 1 deletion buildengine/testdata/projects/other/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type EchoResponse struct {
Message string `json:"message"`
}

//ftl:verb
//ftl:verb export
func Echo(ctx context.Context, req EchoRequest) (EchoResponse, error) {
return EchoResponse{Message: fmt.Sprintf("Hello, %s!", req.Name.Default("anonymous"))}, nil
}
7 changes: 6 additions & 1 deletion examples/go/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ type TimeResponse struct {

// Time returns the current time.
//
//ftl:verb
//ftl:verb export
func Time(ctx context.Context, req TimeRequest) (TimeResponse, error) {
return TimeResponse{Time: time.Now()}, nil
}

//ftl:verb
func Internal(ctx context.Context, req TimeRequest) (TimeResponse, error) {
return TimeResponse{Time: time.Now()}, nil
}

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

14 changes: 7 additions & 7 deletions go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestExtractModuleSchemaTwo(t *testing.T) {
assert.NoError(t, err)
actual = schema.Normalise(actual)
expected := `module two {
enum TwoEnum: String {
export enum TwoEnum: String {
Red = "Red"
Blue = "Blue"
Green = "Green"
Expand All @@ -149,24 +149,24 @@ func TestExtractModuleSchemaTwo(t *testing.T) {
data Exported {
}
data Payload<T> {
export data Payload<T> {
body T
}
data User {
export data User {
name String
}
data UserResponse {
export data UserResponse {
user two.User
}
verb callsTwo(two.Payload<String>) two.Payload<String>
export verb callsTwo(two.Payload<String>) two.Payload<String>
+calls two.two
verb returnsUser(Unit) two.UserResponse
export verb returnsUser(Unit) two.UserResponse
verb two(two.Payload<String>) two.Payload<String>
export verb two(two.Payload<String>) two.Payload<String>
}
`
assert.Equal(t, normaliseString(expected), normaliseString(actual.String()))
Expand Down
8 changes: 4 additions & 4 deletions go-runtime/compile/testdata/two/two.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/TBD54566975/ftl/go-runtime/ftl"
)

//ftl:enum
//ftl:enum export
type TwoEnum string

const (
Expand All @@ -31,17 +31,17 @@ type UserResponse struct {
User User
}

//ftl:verb
//ftl:verb export
func Two(ctx context.Context, req Payload[string]) (Payload[string], error) {
return Payload[string]{}, nil
}

//ftl:verb
//ftl:verb export
func CallsTwo(ctx context.Context, req Payload[string]) (Payload[string], error) {
return ftl.Call(ctx, Two, req)
}

//ftl:verb
//ftl:verb export
func ReturnsUser(ctx context.Context) (UserResponse, error) {
return UserResponse{
User: User{
Expand Down
27 changes: 27 additions & 0 deletions integration/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ func exec(cmd string, args ...string) action {
}
}

// execWithOutput runs a command from the test working directory.
// The output is captured and is returned as part of the error.
func execWithOutput(cmd string, args ...string) action {
return func(t testing.TB, ic testContext) error {
infof("Executing: %s %s", cmd, shellquote.Join(args...))
output, err := ftlexec.Capture(ic, ic.workDir, cmd, args...)
if err != nil {
return fmt.Errorf("command execution failed: %s, output: %s", err, string(output))
}
return nil
}
}

// expectError wraps an action and expects it to return an error with the given message.
func expectError(action action, expectedErrorMsg string) action {
return func(t testing.TB, ic testContext) error {
err := action(t, ic)
if err == nil {
return fmt.Errorf("expected error %q, but got nil", expectedErrorMsg)
}
if !strings.Contains(err.Error(), expectedErrorMsg) {
return fmt.Errorf("expected error %q, but got %q", expectedErrorMsg, err.Error())
}
return nil
}
}

// Deploy a module from the working directory and wait for it to become available.
func deploy(module string) action {
return chain(
Expand Down
11 changes: 11 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ func TestInterModuleCall(t *testing.T) {
)
}

func TestNonExportedDecls(t *testing.T) {
run(t,
copyModule("time"),
deploy("time"),
copyModule("echo"),
deploy("echo"),
copyModule("notexportedverb"),
expectError(execWithOutput("ftl", "deploy", "notexportedverb"), "call first argument must be a function but is an unresolved reference to echo.Echo"),
)
}

func TestDatabase(t *testing.T) {
createDB(t, "database", "testdb")
run(t,
Expand Down
2 changes: 2 additions & 0 deletions integration/testdata/go/notexportedverb/ftl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "notexportedverb"
language = "go"
46 changes: 46 additions & 0 deletions integration/testdata/go/notexportedverb/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module ftl/notexportedverb

go 1.22.2

require github.com/TBD54566975/ftl v0.193.0

require (
connectrpc.com/connect v1.14.0 // indirect
connectrpc.com/grpcreflect v1.2.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/TBD54566975/scaffolder v0.8.0 // indirect
github.com/alecthomas/concurrency v0.0.2 // indirect
github.com/alecthomas/kong v0.9.0 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/types v0.14.0 // indirect
github.com/alessio/shellescape v1.4.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // 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
github.com/swaggest/jsonschema-go v0.3.70 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/zalando/go-keyring v0.2.4 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

replace github.com/TBD54566975/ftl => /Users/wesbillman/dev/ftl
Loading

0 comments on commit ec09a41

Please sign in to comment.