Skip to content

Commit

Permalink
group declarations when printing module
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Mar 25, 2024
1 parent 0764aa2 commit c2d2cd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
33 changes: 29 additions & 4 deletions backend/schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"database/sql/driver"
"fmt"
"os"
"reflect"
"sort"
"strings"

"golang.org/x/exp/maps"
"google.golang.org/protobuf/proto"

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

type Module struct {
Expand Down Expand Up @@ -99,11 +101,34 @@ func (m *Module) String() string {
fmt.Fprint(w, "builtin ")
}
fmt.Fprintf(w, "module %s {\n", m.Name)
for i, s := range m.Decls {
if i > 0 {
fmt.Fprintln(w)

// print decls in groups
groupedTypes := []struct {
types []interface{}
blankLineSeparation bool
}{
{[]interface{}{&Config{}, &Secret{}}, false},
{[]interface{}{&Database{}}, false},
{[]interface{}{&Enum{}}, true},
{[]interface{}{&Data{}}, true},
{[]interface{}{&Verb{}}, true},
}
hasPrintedDecl := false
for _, group := range groupedTypes {
hasPrintedDeclInGroup := false
for _, t := range group.types {
reflected := reflect.TypeOf(t)
decls := slices.Filter(m.Decls, func(decl Decl) bool { return reflect.TypeOf(decl) == reflected })
for _, decl := range decls {
if hasPrintedDecl && (!hasPrintedDeclInGroup || group.blankLineSeparation) {
fmt.Fprintln(w)
}
fmt.Fprintln(w, indent(decl.String()))

hasPrintedDecl = true
hasPrintedDeclInGroup = true
}
}
fmt.Fprintln(w, indent(s.String()))
}
fmt.Fprintln(w, "}")
return w.String()
Expand Down
1 change: 0 additions & 1 deletion backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func TestSchemaString(t *testing.T) {
// A comment
module todo {
config configValue String
secret secretValue String
database testdb
Expand Down

0 comments on commit c2d2cd7

Please sign in to comment.