Skip to content

Commit

Permalink
fix: {Get,Pull}Schema() weren't returning builtin module (#749)
Browse files Browse the repository at this point in the history
Also renamed builtin module to "builtin" from "ftl" so that imports are
`ftl.builtin` rather than `ftl.ftl`.

---------

Co-authored-by: Wes <[email protected]>
  • Loading branch information
alecthomas and wesbillman authored Jan 8, 2024
1 parent c878c7e commit e2db954
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
24 changes: 19 additions & 5 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,13 @@ func (s *Service) GetSchema(ctx context.Context, c *connect.Request[ftlv1.GetSch
if err != nil {
return nil, err
}
sch := &schemapb.Schema{
Modules: slices.Map(schemas, func(d *schema.Module) *schemapb.Module {
return d.ToProto().(*schemapb.Module) //nolint:forcetypeassert
}),
modules := []*schemapb.Module{ //nolint:forcetypeassert
schema.Builtins().ToProto().(*schemapb.Module),
}
return connect.NewResponse(&ftlv1.GetSchemaResponse{Schema: sch}), nil
modules = append(modules, slices.Map(schemas, func(d *schema.Module) *schemapb.Module {
return d.ToProto().(*schemapb.Module) //nolint:forcetypeassert
})...)
return connect.NewResponse(&ftlv1.GetSchemaResponse{Schema: &schemapb.Schema{Modules: modules}}), nil
}

func (s *Service) PullSchema(ctx context.Context, req *connect.Request[ftlv1.PullSchemaRequest], stream *connect.ServerStream[ftlv1.PullSchemaResponse]) error {
Expand Down Expand Up @@ -949,6 +950,19 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
}
logger.Infof("Seeded %d deployments", initialCount)

builtins := schema.Builtins().ToProto().(*schemapb.Module) //nolint:forcetypeassert
buildinsResponse := &ftlv1.PullSchemaResponse{
ModuleName: builtins.Name,
Schema: builtins,
ChangeType: ftlv1.DeploymentChangeType_DEPLOYMENT_ADDED,
More: initialCount > 1,
}

err = sendChange(buildinsResponse)
if err != nil {
return err
}

// Subscribe to deployment changes.
s.dal.DeploymentChanges.Subscribe(deploymentChanges)
defer s.dal.DeploymentChanges.Unsubscribe(deploymentChanges)
Expand Down
4 changes: 2 additions & 2 deletions backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ func TestParsing(t *testing.T) {
Decls: []Decl{
&Verb{
Name: "myIngress",
Request: &DataRef{Module: "ftl", Name: "HttpRequest"},
Response: &DataRef{Module: "ftl", Name: "HttpResponse"},
Request: &DataRef{Module: "builtin", Name: "HttpRequest"},
Response: &DataRef{Module: "builtin", Name: "HttpResponse"},
},
},
}},
Expand Down
6 changes: 3 additions & 3 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
// BuiltinsSource is the schema source code for built-in types.
BuiltinsSource = `
// Built-in types for FTL.
builtin module ftl {
builtin module builtin {
// HTTP request structure used for HTTP ingress verbs.
data HttpRequest {
method String
Expand Down Expand Up @@ -152,7 +152,7 @@ func resolveRef(localModule *Module, ref *Ref, exist map[string]bool) bool {
if ref.Module != "" {
return exist[ref.String()]
}
for _, module := range []string{localModule.Name, "ftl"} {
for _, module := range []string{localModule.Name, "builtin"} {
clone := reflect.DeepCopy(ref)
clone.Module = module
if exist[clone.String()] {
Expand All @@ -173,7 +173,7 @@ func ValidateModule(module *Module) error {
if !validNameRe.MatchString(module.Name) {
merr = append(merr, fmt.Errorf("%s: module name %q is invalid", module.Pos, module.Name))
}
if module.Builtin && module.Name != "ftl" {
if module.Builtin && module.Name != "builtin" {
merr = append(merr, fmt.Errorf("%s: only the \"ftl\" module can be marked as builtin", module.Pos))
}
err := Visit(module, func(n Node, next func() error) error {
Expand Down

0 comments on commit e2db954

Please sign in to comment.