Skip to content

Commit

Permalink
refactor: rename pschema -> schemapb (#469)
Browse files Browse the repository at this point in the history
This is more consistent with how protos usually work.
  • Loading branch information
alecthomas authored Oct 9, 2023
1 parent 239355e commit a7ae71f
Show file tree
Hide file tree
Showing 25 changed files with 150 additions and 146 deletions.
12 changes: 6 additions & 6 deletions backend/controller/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1"
pbconsole "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/console"
"github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/console/pbconsoleconnect"
pschema "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)

type ConsoleService struct {
Expand Down Expand Up @@ -59,7 +59,7 @@ func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pb
switch decl := decl.(type) {
case *schema.Verb:
//nolint:forcetypeassert
v := decl.ToProto().(*pschema.Verb)
v := decl.ToProto().(*schemapb.Verb)
verbSchema := schema.VerbToSchema(v)
dataRef := schema.DataRef{
Module: deployment.Module,
Expand All @@ -80,7 +80,7 @@ func (c *ConsoleService) GetModules(ctx context.Context, req *connect.Request[pb
})
case *schema.Data:
//nolint:forcetypeassert
d := decl.ToProto().(*pschema.Data)
d := decl.ToProto().(*schemapb.Data)
data = append(data, &pbconsole.Data{
Data: d,
Schema: schema.DataToSchema(d).String(),
Expand Down Expand Up @@ -287,9 +287,9 @@ func eventDALToProto(event dal.Event) *pbconsole.Event {
rstr := r.String()
requestName = &rstr
}
var sourceVerbRef *pschema.VerbRef
var sourceVerbRef *schemapb.VerbRef
if sourceVerb, ok := event.SourceVerb.Get(); ok {
sourceVerbRef = sourceVerb.ToProto().(*pschema.VerbRef) //nolint:forcetypeassert
sourceVerbRef = sourceVerb.ToProto().(*schemapb.VerbRef) //nolint:forcetypeassert
}
return &pbconsole.Event{
TimeStamp: timestamppb.New(event.Time),
Expand All @@ -300,7 +300,7 @@ func eventDALToProto(event dal.Event) *pbconsole.Event {
DeploymentName: event.DeploymentName.String(),
TimeStamp: timestamppb.New(event.Time),
SourceVerbRef: sourceVerbRef,
DestinationVerbRef: &pschema.VerbRef{
DestinationVerbRef: &schemapb.VerbRef{
Module: event.DestVerb.Module,
Name: event.DestVerb.Name,
},
Expand Down
24 changes: 12 additions & 12 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/console/pbconsoleconnect"
"github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/ftlv1connect"
pschema "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)

type Config struct {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
creq := connect.NewRequest(&ftlv1.CallRequest{
Verb: &pschema.VerbRef{Module: route.Module, Name: route.Verb},
Verb: &schemapb.VerbRef{Module: route.Module, Name: route.Verb},
Body: body,
})
headers.SetRequestName(creq.Header(), requestName)
Expand Down Expand Up @@ -299,7 +299,7 @@ func (s *Service) Status(ctx context.Context, req *connect.Request[ftlv1.StatusR
Language: d.Language,
Name: d.Module,
MinReplicas: int32(d.MinReplicas),
Schema: d.Schema.ToProto().(*pschema.Module), //nolint:forcetypeassert
Schema: d.Schema.ToProto().(*schemapb.Module), //nolint:forcetypeassert
Labels: labels,
}, nil
})
Expand All @@ -319,7 +319,7 @@ func (s *Service) Status(ctx context.Context, req *connect.Request[ftlv1.StatusR
IngressRoutes: slices.Map(status.IngressRoutes, func(r dal.IngressRouteEntry) *ftlv1.StatusResponse_IngressRoute {
return &ftlv1.StatusResponse_IngressRoute{
DeploymentName: r.Deployment.String(),
Verb: &pschema.VerbRef{Module: r.Module, Name: r.Verb},
Verb: &schemapb.VerbRef{Module: r.Module, Name: r.Verb},
Method: r.Method,
Path: r.Path,
}
Expand Down Expand Up @@ -369,9 +369,9 @@ func (s *Service) GetSchema(ctx context.Context, c *connect.Request[ftlv1.GetSch
if err != nil {
return nil, errors.WithStack(err)
}
sch := &pschema.Schema{
Modules: slices.Map(deployments, func(d dal.Deployment) *pschema.Module {
return d.Schema.ToProto().(*pschema.Module) //nolint:forcetypeassert
sch := &schemapb.Schema{
Modules: slices.Map(deployments, func(d dal.Deployment) *schemapb.Module {
return d.Schema.ToProto().(*schemapb.Module) //nolint:forcetypeassert
}),
}
return connect.NewResponse(&ftlv1.GetSchemaResponse{Schema: sch}), nil
Expand Down Expand Up @@ -511,7 +511,7 @@ func (s *Service) GetDeployment(ctx context.Context, req *connect.Request[ftlv1.
logger.Infof("Get deployment for: %s", deployment.Name)

return connect.NewResponse(&ftlv1.GetDeploymentResponse{
Schema: deployment.Schema.ToProto().(*pschema.Module), //nolint:forcetypeassert
Schema: deployment.Schema.ToProto().(*schemapb.Module), //nolint:forcetypeassert
Artefacts: slices.Map(deployment.Artefacts, ftlv1.ArtefactToProto),
}), nil
}
Expand Down Expand Up @@ -912,8 +912,8 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
delete(moduleState, name)
delete(moduleByDeploymentName, deletion)
} else if message, ok := notification.Message.Get(); ok {
moduleSchema := message.Schema.ToProto().(*pschema.Module) //nolint:forcetypeassert
moduleSchema.Runtime = &pschema.ModuleRuntime{
moduleSchema := message.Schema.ToProto().(*schemapb.Module) //nolint:forcetypeassert
moduleSchema.Runtime = &schemapb.ModuleRuntime{
Language: message.Language,
CreateTime: timestamppb.New(message.CreatedAt),
MinReplicas: int32(message.MinReplicas),
Expand Down Expand Up @@ -1076,9 +1076,9 @@ func runWithRetries(ctx context.Context, success, failure time.Duration, fn func
func extractIngressRoutingEntries(req *ftlv1.CreateDeploymentRequest) []dal.IngressRoutingEntry {
var ingressRoutes []dal.IngressRoutingEntry
for _, decl := range req.Schema.Decls {
if verb, ok := decl.Value.(*pschema.Decl_Verb); ok {
if verb, ok := decl.Value.(*schemapb.Decl_Verb); ok {
for _, metadata := range verb.Verb.Metadata {
if ingress, ok := metadata.Value.(*pschema.Metadata_Ingress); ok {
if ingress, ok := metadata.Value.(*schemapb.Metadata_Ingress); ok {
ingressRoutes = append(ingressRoutes, dal.IngressRoutingEntry{
Verb: verb.Verb.Name,
Method: ingress.Ingress.Method,
Expand Down
8 changes: 4 additions & 4 deletions backend/controller/internal/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/TBD54566975/ftl/backend/controller/internal/sqltypes"
"github.com/TBD54566975/ftl/backend/schema"
ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1"
pschema "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)

var (
Expand Down Expand Up @@ -268,7 +268,7 @@ func (d *DAL) GetStatus(
return Status{}, errors.Wrap(translatePGError(err), "could not get routing table")
}
statusDeployments, err := slices.MapErr(deployments, func(in sql.GetDeploymentsRow) (Deployment, error) {
protoSchema := &pschema.Module{}
protoSchema := &schemapb.Module{}
if err := proto.Unmarshal(in.Deployment.Schema, protoSchema); err != nil {
return Deployment{}, errors.Wrapf(err, "%q: could not unmarshal schema", in.ModuleName)
}
Expand Down Expand Up @@ -724,7 +724,7 @@ func (d *DAL) GetActiveDeployments(ctx context.Context) ([]Deployment, error) {
return nil, errors.WithStack(translatePGError(err))
}
deployments, err := slices.MapErr(rows, func(in sql.GetDeploymentsRow) (Deployment, error) {
protoSchema := &pschema.Module{}
protoSchema := &schemapb.Module{}
if err := proto.Unmarshal(in.Deployment.Schema, protoSchema); err != nil {
return Deployment{}, errors.Wrapf(err, "%q: could not unmarshal schema", in.ModuleName)
}
Expand Down Expand Up @@ -898,7 +898,7 @@ func (d *DAL) InsertLogEvent(ctx context.Context, log *LogEvent) error {
}

func (d *DAL) loadDeployment(ctx context.Context, deployment sql.GetDeploymentRow) (*model.Deployment, error) {
pm := &pschema.Module{}
pm := &schemapb.Module{}
err := proto.Unmarshal(deployment.Deployment.Schema, pm)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
2 changes: 1 addition & 1 deletion backend/schema/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ProtobufSchema() string {
// This file is generated by github.com/TBD54566975/ftl/backend/schema/protobuf.go, DO NOT MODIFY
package xyz.block.ftl.v1.schema;
option go_package = "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema;pschema";
option go_package = "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema;schemapb";
option java_multiple_files = true;
import "xyz/block/ftl/v1/schema/runtime.proto";
Expand Down
64 changes: 32 additions & 32 deletions backend/schema/protobuf_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/alecthomas/errors"
"google.golang.org/protobuf/proto"

pschema "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)

// FromProto converts a protobuf Schema to a Schema and validates it.
func FromProto(s *pschema.Schema) (*Schema, error) {
func FromProto(s *schemapb.Schema) (*Schema, error) {
modules, err := moduleListToSchema(s.Modules)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -21,7 +21,7 @@ func FromProto(s *pschema.Schema) (*Schema, error) {
return schema, Validate(schema)
}

func moduleListToSchema(s []*pschema.Module) ([]*Module, error) {
func moduleListToSchema(s []*schemapb.Module) ([]*Module, error) {
var out []*Module
for _, n := range s {
module, err := ModuleFromProto(n)
Expand All @@ -34,7 +34,7 @@ func moduleListToSchema(s []*pschema.Module) ([]*Module, error) {
}

// ModuleFromProto converts a protobuf Module to a Module and validates it.
func ModuleFromProto(s *pschema.Module) (*Module, error) {
func ModuleFromProto(s *schemapb.Module) (*Module, error) {
module := &Module{
Name: s.Name,
Comments: s.Comments,
Expand All @@ -44,35 +44,35 @@ func ModuleFromProto(s *pschema.Module) (*Module, error) {
}

func ModuleFromBytes(b []byte) (*Module, error) {
s := &pschema.Module{}
s := &schemapb.Module{}
if err := proto.Unmarshal(b, s); err != nil {
return nil, errors.WithStack(err)
}
return ModuleFromProto(s)
}

// VerbRefFromProto converts a protobuf VerbRef to a VerbRef.
func VerbRefFromProto(s *pschema.VerbRef) *VerbRef {
func VerbRefFromProto(s *schemapb.VerbRef) *VerbRef {
return &VerbRef{
Module: s.Module,
Name: s.Name,
}
}

func declListToSchema(s []*pschema.Decl) []Decl {
func declListToSchema(s []*schemapb.Decl) []Decl {
var out []Decl
for _, n := range s {
switch n := n.Value.(type) {
case *pschema.Decl_Verb:
case *schemapb.Decl_Verb:
out = append(out, VerbToSchema(n.Verb))
case *pschema.Decl_Data:
case *schemapb.Decl_Data:
out = append(out, DataToSchema(n.Data))
}
}
return out
}

func VerbToSchema(s *pschema.Verb) *Verb {
func VerbToSchema(s *schemapb.Verb) *Verb {
return &Verb{
Name: s.Name,
Comments: s.Comments,
Expand All @@ -82,97 +82,97 @@ func VerbToSchema(s *pschema.Verb) *Verb {
}
}

func DataToSchema(s *pschema.Data) *Data {
func DataToSchema(s *schemapb.Data) *Data {
return &Data{
Name: s.Name,
Fields: fieldListToSchema(s.Fields),
Comments: s.Comments,
}
}

func fieldListToSchema(s []*pschema.Field) []*Field {
func fieldListToSchema(s []*schemapb.Field) []*Field {
var out []*Field
for _, n := range s {
out = append(out, fieldToSchema(n))
}
return out
}

func fieldToSchema(s *pschema.Field) *Field {
func fieldToSchema(s *schemapb.Field) *Field {
return &Field{
Name: s.Name,
Comments: s.Comments,
Type: typeToSchema(s.Type),
}
}

func typeToSchema(s *pschema.Type) Type {
func typeToSchema(s *schemapb.Type) Type {
switch s := s.Value.(type) {
case *pschema.Type_VerbRef:
case *schemapb.Type_VerbRef:
return verbRefToSchema(s.VerbRef)
case *pschema.Type_DataRef:
case *schemapb.Type_DataRef:
return dataRefToSchema(s.DataRef)
case *pschema.Type_Int:
case *schemapb.Type_Int:
return &Int{}
case *pschema.Type_Float:
case *schemapb.Type_Float:
return &Float{}
case *pschema.Type_String_:
case *schemapb.Type_String_:
return &String{}
case *pschema.Type_Time:
case *schemapb.Type_Time:
return &Time{}
case *pschema.Type_Bool:
case *schemapb.Type_Bool:
return &Bool{}
case *pschema.Type_Array:
case *schemapb.Type_Array:
return arrayToSchema(s.Array)
case *pschema.Type_Map:
case *schemapb.Type_Map:
return mapToSchema(s.Map)
}
panic("unreachable")
}

func verbRefToSchema(s *pschema.VerbRef) *VerbRef {
func verbRefToSchema(s *schemapb.VerbRef) *VerbRef {
return &VerbRef{
Name: s.Name,
Module: s.Module,
}
}

func dataRefToSchema(s *pschema.DataRef) *DataRef {
func dataRefToSchema(s *schemapb.DataRef) *DataRef {
return &DataRef{
Name: s.Name,
Module: s.Module,
}
}

func arrayToSchema(s *pschema.Array) *Array {
func arrayToSchema(s *schemapb.Array) *Array {
return &Array{
Element: typeToSchema(s.Element),
}
}

func mapToSchema(s *pschema.Map) *Map {
func mapToSchema(s *schemapb.Map) *Map {
return &Map{
Key: typeToSchema(s.Key),
Value: typeToSchema(s.Value),
}
}

func metadataListToSchema(s []*pschema.Metadata) []Metadata {
func metadataListToSchema(s []*schemapb.Metadata) []Metadata {
var out []Metadata
for _, n := range s {
out = append(out, metadataToSchema(n))
}
return out
}

func metadataToSchema(s *pschema.Metadata) Metadata {
func metadataToSchema(s *schemapb.Metadata) Metadata {
switch s := s.Value.(type) {
case *pschema.Metadata_Calls:
case *schemapb.Metadata_Calls:
return &MetadataCalls{
Calls: verbRefListToSchema(s.Calls.Calls),
}

case *pschema.Metadata_Ingress:
case *schemapb.Metadata_Ingress:
return &MetadataIngress{
Method: s.Ingress.Method,
Path: s.Ingress.Path,
Expand All @@ -183,7 +183,7 @@ func metadataToSchema(s *pschema.Metadata) Metadata {
}
}

func verbRefListToSchema(s []*pschema.VerbRef) []*VerbRef {
func verbRefListToSchema(s []*schemapb.VerbRef) []*VerbRef {
var out []*VerbRef
for _, n := range s {
out = append(out, verbRefToSchema(n))
Expand Down
Loading

0 comments on commit a7ae71f

Please sign in to comment.