Skip to content

Commit

Permalink
fix: normalise refs without a module name
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Dec 19, 2024
1 parent e471620 commit a639a07
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
8 changes: 5 additions & 3 deletions common/schema/typeresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ func (s Scopes) Resolve(ref Ref) *ModuleDecl {
return &decl
}
}
return nil
}
// If a module is provided, try to resolve it, then resolve the reference through the module.
// Try to resolve the module (if provided), then resolve the reference through the module.
for i := len(s) - 1; i >= 0; i-- {
scope := s[i]
if mdecl, ok := scope[ref.Module]; ok {
for key, mdecl := range scope {
if ref.Module != "" && key != ref.Module {
continue
}
if resolver, ok := mdecl.Symbol.(Resolver); ok {
if decl := resolver.Resolve(ref); decl != nil {
// Holy nested if statement Batman.
Expand Down
8 changes: 8 additions & 0 deletions common/schema/typeresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func TestTypeResolver(t *testing.T) {
t T
}
verb test(test.Request<String>) Empty
// This module has it's own definition of HTTPRequest
data HTTPRequest {
}
}
`)
assert.NoError(t, err)
Expand All @@ -40,4 +44,8 @@ func TestTypeResolver(t *testing.T) {
scopes = scopes.PushScope(actualData.Scope())
actualTP, _ := ResolveAs[*TypeParameter](scopes, Ref{Name: "T"})
assert.Equal(t, actualTP, &TypeParameter{Name: "T"}, assert.Exclude[Position]())

// Resolve a local data decl with module == ""
httpRequest := scopes.Resolve(Ref{Name: "HTTPRequest"})
assert.Equal(t, httpRequest.Module.MustGet().Name, "test")
}
26 changes: 13 additions & 13 deletions common/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func ValidateModuleInSchema(schema *Schema, m optional.Option[*Module]) (*Schema
}
indent++
defer func() { indent-- }()

// Validate all children before validating the current node.
if err := next(); err != nil {
return err
}
switch n := n.(type) {
case *Ref:
mdecl := scopes.Resolve(*n)
Expand Down Expand Up @@ -222,7 +227,7 @@ func ValidateModuleInSchema(schema *Schema, m optional.Option[*Module]) (*Schema
*EnumVariant, *Config, *Secret, *Topic, *DatabaseRuntime, *DatabaseRuntimeConnections,
*Data, *Field:
}
return next()
return nil
})
if err != nil {
merr = append(merr, err)
Expand Down Expand Up @@ -267,6 +272,12 @@ func ValidateModule(module *Module) error {
scopes = scopes.PushScope(scoped.Scope())
defer func() { scopes = pop }()
}

// Validate all children before validating the current node.
if err := next(); err != nil {
return err
}

if n, ok := n.(Decl); ok {
tname := typeName(n)
duplKey := tname + ":" + n.GetName()
Expand Down Expand Up @@ -351,17 +362,12 @@ func ValidateModule(module *Module) error {
}
}

case *MetadataRetry:
if n.Catch != nil && n.Catch.Module == "" {
n.Catch.Module = module.Name
}

case Type, Metadata, Value, IngressPathComponent, DatabaseConnector,
*Module, *Schema, *Optional, *TypeParameter, *EnumVariant,
*Config, *Secret, *DatabaseRuntime, *DatabaseRuntimeConnections,
*Database, *Enum:
}
return next()
return nil
})

merr = cleanErrors(merr)
Expand Down Expand Up @@ -908,9 +914,6 @@ func validateQueryParamsPayloadType(n Node, r Type, v *Verb, reqOrResp string) e

func validateVerbSubscriptions(module *Module, v *Verb, md *MetadataSubscriber, scopes Scopes) (merr []error) {
merr = []error{}
if md.Topic.Module == "" {
md.Topic.Module = module.Name
}
topicDecl := scopes.Resolve(*md.Topic)
if topicDecl == nil {
// errors for invalid refs are handled elsewhere
Expand Down Expand Up @@ -959,9 +962,6 @@ func validateRetries(module *Module, retry *MetadataRetry, requestType optional.
merr = append(merr, errorf(retry, "catch can only be defined on verbs"))
return
}
if retry.Catch.Module == "" {
retry.Catch.Module = module.Name
}
catchDecl := scopes.Resolve(*retry.Catch)
if catchDecl == nil {
if retry.Catch.Module != "" && retry.Catch.Module != module.Name && !schema.Ok() {
Expand Down
10 changes: 5 additions & 5 deletions common/schema/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func TestValidate(t *testing.T) {
}
`,
errs: []string{
"3:20: ingress verb a: request type Empty must be builtin.HttpRequest",
"3:27: ingress verb a: response type Empty must be builtin.HttpResponse",
"3:20: ingress verb a: request type builtin.Empty must be builtin.HttpRequest",
"3:27: ingress verb a: response type builtin.Empty must be builtin.HttpResponse",
}},
{name: "IngressBodyTypes",
schema: `
Expand Down Expand Up @@ -136,8 +136,8 @@ func TestValidate(t *testing.T) {
}
`,
errs: []string{
"11:22: ingress verb any: GET request type HttpRequest<Any, Unit, Unit> must have a body of unit not Any",
"11:52: ingress verb any: response type HttpResponse<Any, Any> must have a body of bytes, string, data structure, unit, float, int, bool, map, or array not Any",
"11:22: ingress verb any: GET request type builtin.HttpRequest<Any, Unit, Unit> must have a body of unit not Any",
"11:52: ingress verb any: response type builtin.HttpResponse<Any, Any> must have a body of bytes, string, data structure, unit, float, int, bool, map, or array not Any",
"16:31: ingress verb pathInvalid: cannot use path parameter \"invalid\" with request type String as it has multiple path parameters, expected Data or Map type",
"16:41: ingress verb pathInvalid: cannot use path parameter \"extra\" with request type String as it has multiple path parameters, expected Data or Map type",
"18:31: ingress verb pathMissing: request pathParameter type one.Path does not contain a field corresponding to the parameter \"missing\"",
Expand All @@ -152,7 +152,7 @@ func TestValidate(t *testing.T) {
}
`,
errs: []string{
"3:24: ingress verb bytes: GET request type HttpRequest<Bytes, Unit, Unit> must have a body of unit not Bytes",
"3:24: ingress verb bytes: GET request type builtin.HttpRequest<Bytes, Unit, Unit> must have a body of unit not Bytes",
}},
{name: "Array",
schema: `
Expand Down

0 comments on commit a639a07

Please sign in to comment.