diff --git a/backend/schema/schema.go b/backend/schema/schema.go index e511744c77..60707db670 100644 --- a/backend/schema/schema.go +++ b/backend/schema/schema.go @@ -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" ) @@ -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"` diff --git a/backend/schema/validate.go b/backend/schema/validate.go index 8ef3d854df..07660808fe 100644 --- a/backend/schema/validate.go +++ b/backend/schema/validate.go @@ -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)) @@ -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. }