Skip to content

Commit

Permalink
chore: implement DataRef.schemaChildren()
Browse files Browse the repository at this point in the history
It was returning nil previously, resulting in the Visit() function never
visiting the type parameters and all sorts of things failing!
  • Loading branch information
alecthomas committed Feb 7, 2024
1 parent 0ecde12 commit d02208d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 64 deletions.
10 changes: 8 additions & 2 deletions backend/schema/dataref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions backend/schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
68 changes: 14 additions & 54 deletions backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,15 @@ func normaliseString(s string) string {
func TestImports(t *testing.T) {
input := `
module test {
data Generic<T> {
value T
}
data Data {
ref other.Data
ref another.Data
ref Generic<new.Data>
}
}
`
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) {
Expand Down Expand Up @@ -98,7 +94,9 @@ Module
VerbRef
Verb
DataRef
DataRef
DataRef
DataRef
MetadataIngress
IngressPathLiteral
IngressPathLiteral
Expand All @@ -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) {
Expand Down Expand Up @@ -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<string>) HttpResponse<string> }`,
input: `module test { verb myIngress(HttpRequest<String>) HttpResponse<String> }`,
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{}}},
},
},
}},
Expand Down Expand Up @@ -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{}},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d02208d

Please sign in to comment.