Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only build verb schema for non-nil refs #1591

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions backend/controller/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ func visitNode(sch *schema.Schema, n schema.Node, verbString *string) error {
return schema.Visit(n, func(n schema.Node, next func() error) error {
switch n := n.(type) {
case *schema.Ref:
decl, ok := sch.Resolve(n).Get()
if !ok {
return fmt.Errorf("failed to resolve %s", n)
}
*verbString += decl.String() + "\n\n"
err := visitNode(sch, decl, verbString)
if err != nil {
return err
if decl, ok := sch.Resolve(n).Get(); ok {
*verbString += decl.String() + "\n\n"
err := visitNode(sch, decl, verbString)
if err != nil {
return err
}
Comment on lines +47 to +52
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alecthomas this change sort of undoes the change that was made here: 3c91886#diff-cf10aad402fc38d803d69ee88de91fa8b7f27d33ea52e2dc67ebb6749dfe61afR47-R50 so I wanted to call it out.

Maybe there's a better approach we can use here, but wanted to fix the broken console until then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks identical to me, just reordered, or am I missing something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it's very similar. The difference is that this one doesn't error if the decl cannot be resolved, it just skips it. This was happening for Body in the HttpRequest<Body> data types. Since Body isn't a ref that it can find.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So instead of:

return fmt.Errorf("failed to resolve %s", n)

it just moves on to the next Ref

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah I see, makes sense

}

default:
}
return next()
Expand Down
76 changes: 76 additions & 0 deletions backend/controller/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ func TestVerbSchemaString(t *testing.T) {
Request: &schema.Ref{Module: "foo", Name: "EchoRequest"},
Response: &schema.Ref{Module: "foo", Name: "EchoResponse"},
}
ingressVerb := &schema.Verb{
Name: "Ingress",
Request: &schema.Ref{Module: "builtin", Name: "HttpRequest", TypeParameters: []schema.Type{&schema.String{}}},
Response: &schema.Ref{Module: "builtin", Name: "HttpResponse", TypeParameters: []schema.Type{&schema.String{}, &schema.String{}}},
Metadata: []schema.Metadata{
&schema.MetadataIngress{Type: "http", Method: "GET", Path: []schema.IngressPathComponent{&schema.IngressPathLiteral{Text: "test"}}},
},
}
sch := &schema.Schema{
Modules: []*schema.Module{
schema.Builtins(),
{Name: "foo", Decls: []schema.Decl{
verb,
ingressVerb,
&schema.Data{
Name: "EchoRequest",
Fields: []*schema.Field{
Expand Down Expand Up @@ -51,6 +61,7 @@ func TestVerbSchemaString(t *testing.T) {
}},
{Name: "bar", Decls: []schema.Decl{
verb,
ingressVerb,
&schema.Data{
Name: "BarData",
Export: true,
Expand Down Expand Up @@ -92,3 +103,68 @@ verb Echo(foo.EchoRequest) foo.EchoResponse`
assert.NoError(t, err)
assert.Equal(t, expected, schemaString)
}

func TestVerbSchemaStringIngress(t *testing.T) {
verb := &schema.Verb{
Name: "Ingress",
Request: &schema.Ref{Module: "builtin", Name: "HttpRequest", TypeParameters: []schema.Type{&schema.Ref{Module: "foo", Name: "FooRequest"}}},
Response: &schema.Ref{Module: "builtin", Name: "HttpResponse", TypeParameters: []schema.Type{&schema.Ref{Module: "foo", Name: "FooResponse"}, &schema.String{}}},
Metadata: []schema.Metadata{
&schema.MetadataIngress{Type: "http", Method: "GET", Path: []schema.IngressPathComponent{&schema.IngressPathLiteral{Text: "foo"}}},
},
}
sch := &schema.Schema{
Modules: []*schema.Module{
schema.Builtins(),
{Name: "foo", Decls: []schema.Decl{
verb,
&schema.Data{
Name: "FooRequest",
Fields: []*schema.Field{
{Name: "Name", Type: &schema.String{}},
},
},
&schema.Data{
Name: "FooResponse",
Fields: []*schema.Field{
{Name: "Message", Type: &schema.String{}},
},
},
}},
},
}

expected := `// HTTP request structure used for HTTP ingress verbs.
export data HttpRequest<Body> {
method String
path String
pathParameters {String: String}
query {String: [String]}
headers {String: [String]}
body Body
}

data FooRequest {
Name String
}

// HTTP response structure used for HTTP ingress verbs.
export data HttpResponse<Body, Error> {
status Int
headers {String: [String]}
// Either "body" or "error" must be present, not both.
body Body?
error Error?
}

data FooResponse {
Message String
}

verb Ingress(builtin.HttpRequest<foo.FooRequest>) builtin.HttpResponse<foo.FooResponse, String>
+ingress http GET /foo`

schemaString, err := verbSchemaString(sch, verb)
assert.NoError(t, err)
assert.Equal(t, expected, schemaString)
}
Loading