From d02208d917d2eb54b7f8469ccdc0fa5a0577bc6f Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 8 Feb 2024 06:12:34 +1100 Subject: [PATCH] chore: implement DataRef.schemaChildren() It was returning nil previously, resulting in the Visit() function never visiting the type parameters and all sorts of things failing! --- backend/schema/dataref.go | 10 ++++-- backend/schema/module.go | 8 ----- backend/schema/schema_test.go | 68 ++++++++--------------------------- backend/schema/validate.go | 1 + 4 files changed, 23 insertions(+), 64 deletions(-) diff --git a/backend/schema/dataref.go b/backend/schema/dataref.go index fdef34431d..394eb92200 100644 --- a/backend/schema/dataref.go +++ b/backend/schema/dataref.go @@ -52,8 +52,14 @@ func (d *DataRef) ToProto() protoreflect.ProtoMessage { } } -func (*DataRef) schemaChildren() []Node { return nil } -func (*DataRef) schemaType() {} +func (d *DataRef) schemaChildren() []Node { + out := make([]Node, 0, len(d.TypeParameters)) + for _, t := range d.TypeParameters { + out = append(out, t) + } + return out +} +func (*DataRef) schemaType() {} func ParseDataRef(ref string) (*DataRef, error) { return dataRefParser.ParseString("", ref) diff --git a/backend/schema/module.go b/backend/schema/module.go index 571102afc8..85bc6dfc81 100644 --- a/backend/schema/module.go +++ b/backend/schema/module.go @@ -149,14 +149,6 @@ func (m *Module) Imports() []string { if n.Module != "" && n.Module != m.Name { imports[n.Module] = true } - // add imports for type parameters DataRefs as well - for _, p := range n.TypeParameters { - if p, ok := p.(*DataRef); ok { - if p.Module != "" && p.Module != m.Name { - imports[p.Module] = true - } - } - } case *VerbRef: if n.Module != "" && n.Module != m.Name { diff --git a/backend/schema/schema_test.go b/backend/schema/schema_test.go index 35d7b5f60f..934209d0d2 100644 --- a/backend/schema/schema_test.go +++ b/backend/schema/schema_test.go @@ -55,19 +55,15 @@ func normaliseString(s string) string { func TestImports(t *testing.T) { input := ` module test { - data Generic { - value T - } data Data { ref other.Data ref another.Data - ref Generic } } ` schema, err := ParseModuleString("", input) assert.NoError(t, err) - assert.Equal(t, []string{"another", "new", "other"}, schema.Imports()) + assert.Equal(t, []string{"another", "other"}, schema.Imports()) } func TestVisit(t *testing.T) { @@ -98,7 +94,9 @@ Module VerbRef Verb DataRef + DataRef DataRef + DataRef MetadataIngress IngressPathLiteral IngressPathLiteral @@ -115,7 +113,7 @@ Module return next() }) assert.NoError(t, err) - assert.Equal(t, normaliseString(expected), normaliseString(actual.String()), "%s", actual.String()) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(actual.String()), "%s", actual.String()) } func TestParserRoundTrip(t *testing.T) { @@ -188,35 +186,15 @@ func TestParsing(t *testing.T) { input: `module int { data String { name String } verb verb(String) String }`, errors: []string{"1:14: data structure name \"String\" is a reserved word"}}, {name: "BuiltinRef", - input: `module test { verb myIngress(HttpRequest) HttpResponse }`, + input: `module test { verb myIngress(HttpRequest) HttpResponse }`, expected: &Schema{ Modules: []*Module{{ Name: "test", Decls: []Decl{ &Verb{ - Name: "myIngress", - Request: &DataRef{Module: "builtin", Name: "HttpRequest", TypeParameters: []Type{ - &DataRef{ - Pos: Position{ - Offset: 41, - Line: 1, - Column: 42, - }, - Name: "string", - TypeParameters: []Type{}, - }, - }}, - Response: &DataRef{Module: "builtin", Name: "HttpResponse", TypeParameters: []Type{ - &DataRef{ - Pos: Position{ - Offset: 63, - Line: 1, - Column: 64, - }, - Name: "string", - TypeParameters: []Type{}, - }, - }}, + Name: "myIngress", + Request: &DataRef{Module: "builtin", Name: "HttpRequest", TypeParameters: []Type{&String{}}}, + Response: &DataRef{Module: "builtin", Name: "HttpResponse", TypeParameters: []Type{&String{}}}, }, }, }}, @@ -310,32 +288,14 @@ func TestParsing(t *testing.T) { Comments: []string{}, Name: "test", Request: &DataRef{ - Module: "test", - Name: "Data", - TypeParameters: []Type{ - &String{ - Pos: Position{ - Offset: 81, - Line: 7, - Column: 21, - }, - Str: true, - }, - }, + Module: "test", + Name: "Data", + TypeParameters: []Type{&String{}}, }, Response: &DataRef{ - Module: "test", - Name: "Data", - TypeParameters: []Type{ - &String{ - Pos: Position{ - Offset: 95, - Line: 7, - Column: 35, - }, - Str: true, - }, - }, + Module: "test", + Name: "Data", + TypeParameters: []Type{&String{}}, }, }, }, diff --git a/backend/schema/validate.go b/backend/schema/validate.go index 5c8dc76813..b3871cd0a7 100644 --- a/backend/schema/validate.go +++ b/backend/schema/validate.go @@ -104,6 +104,7 @@ func Validate(schema *Schema) (*Schema, error) { } case *DataRef: + fmt.Println(n, n.Untyped()) if mdecl := scopes.Resolve(n.Untyped()); mdecl != nil { switch decl := mdecl.Decl.(type) { case *Data: