Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract databases in Kotlin schema #762

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions backend/schema/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package schema

import (
"fmt"
"strings"

"google.golang.org/protobuf/proto"

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

type Database struct {
Pos Position `parser:"" protobuf:"1,optional"`

Comments []string `parser:"@Comment*" protobuf:"3"`
Name string `parser:"'database' @Ident" protobuf:"2"`
}

var _ Decl = (*Database)(nil)

// schemaDecl implements Decl
func (*Database) schemaDecl() {}
func (d *Database) schemaChildren() []Node { return nil }
func (d *Database) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(d.Comments))
fmt.Fprintf(w, "database %s", d.Name)
return w.String()
}

func (d *Database) ToProto() proto.Message {
return &schemapb.Database{
Pos: posToProto(d.Pos),
Name: d.Name,
Comments: d.Comments,
}
}
func DatabaseToSchema(s *schemapb.Database) *Database {
return &Database{
Pos: posFromProto(s.Pos),
Name: s.Name,
Comments: s.Comments,
}
}

func databaseListToSchema(s []*schemapb.Database) []*Database {
var out []*Database
for _, n := range s {
out = append(out, DatabaseToSchema(n))
}
return out
}
4 changes: 2 additions & 2 deletions backend/schema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ func nodeToJSSchema(node Node, dataRefs map[DataRef]bool) *jsonschema.Schema {
{TypeObject: &jsonschema.Schema{Type: &jsonschema.Type{SimpleTypes: &null}}},
}}

case Decl, *Field, Metadata, *MetadataCalls, *MetadataIngress,
case Decl, *Field, Metadata, *MetadataCalls, *MetadataDatabases, *MetadataIngress,
IngressPathComponent, *IngressPathLiteral, *IngressPathParameter, *Module,
*Schema, Type, *Verb, *VerbRef, *SourceRef, *SinkRef:
*Schema, Type, *Database, *Verb, *VerbRef, *SourceRef, *SinkRef:
panic(fmt.Sprintf("unsupported node type %T", node))

default:
Expand Down
55 changes: 55 additions & 0 deletions backend/schema/metadatadatabases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package schema

import (
"fmt"
"strings"

"google.golang.org/protobuf/proto"

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

type MetadataDatabases struct {
Pos Position `parser:"" protobuf:"1,optional"`

Calls []*Database `parser:"'database' 'calls' @@ (',' @@)*" protobuf:"2"`
}

var _ Metadata = (*MetadataDatabases)(nil)

func (m *MetadataDatabases) String() string {
out := &strings.Builder{}
fmt.Fprint(out, "database calls ")
w := 6
for i, call := range m.Calls {
if i > 0 {
fmt.Fprint(out, ", ")
w += 2
}
str := call.String()
if w+len(str) > 70 {
w = 6
fmt.Fprint(out, "\n ")
}
w += len(str)
fmt.Fprint(out, str)
}
fmt.Fprintln(out)
return out.String()
}

func (m *MetadataDatabases) schemaChildren() []Node {
out := make([]Node, 0, len(m.Calls))
for _, ref := range m.Calls {
out = append(out, ref)
}
return out
}
func (*MetadataDatabases) schemaMetadata() {}

func (m *MetadataDatabases) ToProto() proto.Message {
return &schemapb.MetadataDatabases{
Pos: posToProto(m.Pos),
Calls: nodeListToProto[*schemapb.Database](m.Calls),
}
}
7 changes: 7 additions & 0 deletions backend/schema/normalise.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func Normalise[T Node](n T) T {
c.Fields = normaliseSlice(c.Fields)
c.Metadata = normaliseSlice(c.Metadata)

case *Database:
c.Pos = zero

case *DataRef:
c.Pos = zero

Expand Down Expand Up @@ -77,6 +80,10 @@ func Normalise[T Node](n T) T {
c.Pos = zero
c.Calls = normaliseSlice(c.Calls)

case *MetadataDatabases:
c.Pos = zero
c.Calls = normaliseSlice(c.Calls)

case *MetadataIngress:
c.Pos = zero
c.Path = normaliseSlice(c.Path)
Expand Down
4 changes: 2 additions & 2 deletions backend/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

var (
declUnion = []Decl{&Data{}, &Verb{}}
declUnion = []Decl{&Data{}, &Verb{}, &Database{}}
nonOptionalTypeUnion = []Type{
&Int{}, &Float{}, &String{}, &Bytes{}, &Bool{}, &Time{}, &Array{},
&Map{}, &DataRef{}, &Unit{},
}
typeUnion = append(nonOptionalTypeUnion, &Optional{})
metadataUnion = []Metadata{&MetadataCalls{}, &MetadataIngress{}}
metadataUnion = []Metadata{&MetadataCalls{}, &MetadataIngress{}, &MetadataDatabases{}}
ingressUnion = []IngressPathComponent{&IngressPathLiteral{}, &IngressPathParameter{}}

// Used by protobuf generation.
Expand Down
8 changes: 8 additions & 0 deletions backend/schema/protobuf_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func declListToSchema(s []*schemapb.Decl) []Decl {
out = append(out, VerbToSchema(n.Verb))
case *schemapb.Decl_Data:
out = append(out, DataToSchema(n.Data))
case *schemapb.Decl_Database:
out = append(out, DatabaseToSchema(n.Database))
}
}
return out
Expand Down Expand Up @@ -74,6 +76,12 @@ func metadataToSchema(s *schemapb.Metadata) Metadata {
Calls: verbRefListToSchema(s.Calls.Calls),
}

case *schemapb.Metadata_Databases:
return &MetadataDatabases{
Pos: posFromProto(s.Databases.Pos),
Calls: databaseListToSchema(s.Databases.Calls),
}

case *schemapb.Metadata_Ingress:
return &MetadataIngress{
Pos: posFromProto(s.Ingress.Pos),
Expand Down
5 changes: 5 additions & 0 deletions backend/schema/protobuf_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func declListToProto(nodes []Decl) []*schemapb.Decl {
v = &schemapb.Decl_Verb{Verb: n.ToProto().(*schemapb.Verb)}
case *Data:
v = &schemapb.Decl_Data{Data: n.ToProto().(*schemapb.Data)}
case *Database:
v = &schemapb.Decl_Database{Database: n.ToProto().(*schemapb.Database)}
}
out[i] = &schemapb.Decl{Value: v}
}
Expand All @@ -44,6 +46,9 @@ func metadataListToProto(nodes []Metadata) []*schemapb.Metadata {
case *MetadataCalls:
v = &schemapb.Metadata_Calls{Calls: n.ToProto().(*schemapb.MetadataCalls)}

case *MetadataDatabases:
v = &schemapb.Metadata_Databases{Databases: n.ToProto().(*schemapb.MetadataDatabases)}

case *MetadataIngress:
v = &schemapb.Metadata_Ingress{Ingress: n.ToProto().(*schemapb.MetadataIngress)}

Expand Down
4 changes: 2 additions & 2 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ func ValidateModule(module *Module) error {
}
}

case *Array, *Bool, *DataRef, *Field, *Float, *Int,
case *Array, *Bool, *DataRef, *Database, *Field, *Float, *Int,
*Time, *Map, *Module, *Schema, *String, *Bytes, *VerbRef,
*MetadataCalls, *MetadataIngress, IngressPathComponent,
*MetadataCalls, *MetadataDatabases, *MetadataIngress, IngressPathComponent,
*IngressPathLiteral, *IngressPathParameter, *Optional,
*SourceRef, *SinkRef, *Unit:
case Type, Metadata, Decl: // Union sql.
Expand Down
5 changes: 5 additions & 0 deletions examples/kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>ftl-runtime</artifactId>
<version>${ftl.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.1</version>
</dependency>
</dependencies>

<build>
Expand Down
106 changes: 106 additions & 0 deletions frontend/src/protos/xyz/block/ftl/v1/schema/schema_pb.ts

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

Loading