Skip to content

Commit

Permalink
feat: add .Imports() method to schema.Module (#573)
Browse files Browse the repository at this point in the history
This will initially be used by templates to generate import statements.
  • Loading branch information
alecthomas authored Nov 8, 2023
1 parent 714a615 commit e353e38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
23 changes: 23 additions & 0 deletions backend/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alecthomas/errors"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -230,6 +231,28 @@ func (m *Module) Data() []*Data {
return data
}

// Imports returns the modules imported by this module.
func (m *Module) Imports() []string {
imports := map[string]bool{}
_ = Visit(m, func(n Node, next func() error) error {
switch n := n.(type) {
case *DataRef:
if n.Module != "" && n.Module != m.Name {
imports[n.Module] = true
}

case *VerbRef:
if n.Module != "" && n.Module != m.Name {
imports[n.Module] = true
}

default:
}
return nil
})
return maps.Keys(imports)
}

type Schema struct {
Pos Position `json:"pos,omitempty" parser:"" protobuf:"1,optional"`

Expand Down
12 changes: 10 additions & 2 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func ValidateModule(module *Module) error {
}
err := Visit(module, func(n Node, next func() error) error {
switch n := n.(type) {
case *VerbRef:
if n.Name == module.Name {
merr = append(merr, errors.Errorf("%s: references to Verbs in the same module cannot include a module name", n.Pos))
}
case *DataRef:
if n.Name == module.Name {
merr = append(merr, errors.Errorf("%s: references to Data in the same module cannot include a module name", n.Pos))
}
case *Verb:
if !validNameRe.MatchString(n.Name) {
merr = append(merr, errors.Errorf("%s: Verb name %q is invalid", n.Pos, n.Name))
Expand Down Expand Up @@ -117,8 +125,8 @@ func ValidateModule(module *Module) error {
}
}

case *Array, *Bool, *DataRef, *Field, *Float, *Int,
*Time, *Map, *Module, *Schema, *String, *VerbRef,
case *Array, *Bool, *Field, *Float, *Int,
*Time, *Map, *Module, *Schema, *String,
*MetadataCalls, *MetadataIngress:
case Type, Metadata, Decl: // Union sql.
}
Expand Down

0 comments on commit e353e38

Please sign in to comment.