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

chore: group declarations when printing module #1133

Merged
merged 4 commits into from
Mar 27, 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
39 changes: 35 additions & 4 deletions backend/schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"database/sql/driver"
"fmt"
"os"
"reflect"
"slices"
"sort"
"strings"

"github.com/alecthomas/types/optional"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/proto"

Expand Down Expand Up @@ -92,18 +95,46 @@ func (m *Module) schemaChildren() []Node {
}
return children
}

type spacingRule struct {
gapWithinType bool
skipGapAfterTypes []reflect.Type
}

func (m *Module) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(m.Comments))
if m.Builtin {
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 with spacing rules
typeSpacingRules := map[reflect.Type]spacingRule{
reflect.TypeOf(&Config{}): {gapWithinType: false},
reflect.TypeOf(&Secret{}): {gapWithinType: false, skipGapAfterTypes: []reflect.Type{reflect.TypeOf(&Config{})}},
reflect.TypeOf(&Database{}): {gapWithinType: false},
reflect.TypeOf(&Enum{}): {gapWithinType: true},
reflect.TypeOf(&Data{}): {gapWithinType: true},
reflect.TypeOf(&Verb{}): {gapWithinType: true},
}

lastTypePrinted := optional.None[reflect.Type]()
for _, decl := range m.Decls {
t := reflect.TypeOf(decl)
rules, ok := typeSpacingRules[t]
if !ok {
rules = spacingRule{gapWithinType: true}
}
if lastType, ok := lastTypePrinted.Get(); ok {
if lastType == t && rules.gapWithinType {
fmt.Fprintln(w)
} else if !slices.Contains(rules.skipGapAfterTypes, lastType) {
fmt.Fprintln(w)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declarations should already be sorted in the correct order, so you could simplify this considerably by just iterating over them and printing the correct spacing as you transition between groups/lines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to trust the exisiting order, and use reflect.Type rather than empty structs which makes it easier to access rules from the mapping

}
fmt.Fprintln(w, indent(s.String()))
fmt.Fprintln(w, indent(decl.String()))
lastTypePrinted = optional.Some[reflect.Type](t)
}
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
1 change: 0 additions & 1 deletion go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestExtractModuleSchema(t *testing.T) {
actual = schema.Normalise(actual)
expected := `module one {
config configValue one.Config

secret secretValue String

enum Color(String) {
Expand Down
Loading