Skip to content

Commit

Permalink
chore: group declarations when printing module (#1133)
Browse files Browse the repository at this point in the history
#1089

Ordering and spacing is the following (example has 2 of each declaration
type)
```
{
   config
   config
   secret
   secret

   database
   database

   enum

   enum

   data

   data

   verb

   verb
}
```

Full example:
```
module echo {
  config default String
  config default2 String
  secret default3 String
  secret default3 String

  // An echo request.
  data EchoRequest {
    name String? +alias json "name"
    inner echo.EchoRequest2 +alias json "inner"
  }

  data EchoRequest2 {
    name String? +alias json "name"
  }

  data EchoResponse {
    message String +alias json "message"
    inner echo.EchoResponse2 +alias json "inner"
  }

  data EchoResponse2 {
    message String +alias json "message"
  }

  // Echo returns a greeting with the current time.
  verb echo(echo.EchoRequest) echo.EchoResponse  
      +calls time.time

  verb echo2(echo.EchoRequest) echo.EchoResponse  
      +calls time.time
}
```
  • Loading branch information
matt2e authored Mar 27, 2024
1 parent 2daa7c0 commit 11b2561
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
38 changes: 34 additions & 4 deletions backend/schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"database/sql/driver"
"fmt"
"os"
"reflect"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -66,18 +68,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)
}
}
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 @@ -47,7 +47,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

0 comments on commit 11b2561

Please sign in to comment.