Skip to content

Commit

Permalink
feat: verb request and response types can be any FTL type
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Jul 1, 2024
1 parent 43d5a32 commit 6da5eea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
9 changes: 4 additions & 5 deletions backend/controller/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ func matchSegments(pattern, urlPath string, onMatch func(segment, value string))
}

func ValidateCallBody(body []byte, verb *schema.Verb, sch *schema.Schema) error {
var requestMap map[string]any
err := json.Unmarshal(body, &requestMap)
var root any
err := json.Unmarshal(body, &root)
if err != nil {
return fmt.Errorf("HTTP request body is not valid JSON: %w", err)
return fmt.Errorf("request body is not valid JSON: %w", err)
}

return validateValue(verb.Request, []string{verb.Request.String()}, requestMap, sch)
return validateValue(verb.Request, []string{verb.Request.String()}, root, sch)
}

func getBodyField(ref *schema.Ref, sch *schema.Schema) (*schema.Field, error) {
Expand Down
6 changes: 4 additions & 2 deletions go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func TestExtractModuleSchema(t *testing.T) {
data WithoutDirectiveStruct {
}
verb batchStringToTime([String]) [Time]
export verb http(builtin.HttpRequest<one.Req>) builtin.HttpResponse<one.Resp, Unit>
+ingress http GET /get
Expand All @@ -174,6 +176,8 @@ func TestExtractModuleSchema(t *testing.T) {
verb source(Unit) one.SourceResp
verb stringToTime(String) Time
verb verb(one.Req) one.Resp
}
`
Expand Down Expand Up @@ -537,7 +541,6 @@ func TestErrorReporting(t *testing.T) {
`45:1-2: must have at most two parameters (context.Context, struct)`,
`45:69-69: unsupported response type "ftl/failing.Response"`,
`50:22-27: first parameter must be of type context.Context but is ftl/failing.Request`,
`50:37-43: second parameter must be a struct but is string`,
`50:53-53: unsupported response type "ftl/failing.Response"`,
`55:43-47: second parameter must not be ftl.Unit`,
`55:59-59: unsupported response type "ftl/failing.Response"`,
Expand All @@ -550,7 +553,6 @@ func TestErrorReporting(t *testing.T) {
`74:35-35: unsupported request type "ftl/failing.Request"`,
`74:48-48: must return an error but is ftl/failing.Response`,
`79:41-41: unsupported request type "ftl/failing.Request"`,
`79:55-55: first result must be a struct but is string`,
`79:63-63: must return an error but is string`,
`79:63-63: second result must not be ftl.Unit`,
// `86:1-2: duplicate declaration of "WrongResponse" at 79:6`, TODO: fix this
Expand Down
11 changes: 11 additions & 0 deletions go-runtime/compile/testdata/one/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package one

import (
"context"
"errors"
"time"

"ftl/builtin"
Expand Down Expand Up @@ -183,3 +184,13 @@ type NonFTLStruct struct {
}

func (NonFTLStruct) NonFTLInterface() {}

//ftl:verb
func StringToTime(ctx context.Context, input string) (time.Time, error) {
return time.Time{}, errors.New("not implemented")
}

//ftl:verb
func BatchStringToTime(ctx context.Context, input []string) ([]time.Time, error) {
return nil, errors.New("not implemented")
}
6 changes: 0 additions & 6 deletions go-runtime/schema/verb/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func checkSignature(pass *analysis.Pass, node *ast.FuncDecl, sig *types.Signatur
}

if params.Len() == 2 {
if !common.IsType[*types.Struct](params.At(1).Type()) {
common.TokenErrorf(pass, params.At(1).Pos(), params.At(1).Name(), "second parameter must be a struct but is %s", params.At(1).Type())
}
if params.At(1).Type().String() == common.FtlUnitTypePath {
common.TokenErrorf(pass, params.At(1).Pos(), params.At(1).Name(), "second parameter must not be ftl.Unit")
}
Expand All @@ -98,9 +95,6 @@ func checkSignature(pass *analysis.Pass, node *ast.FuncDecl, sig *types.Signatur
common.TokenErrorf(pass, results.At(results.Len()-1).Pos(), results.At(results.Len()-1).Name(), "must return an error but is %s", results.At(0).Type())
}
if results.Len() == 2 {
if !common.IsType[*types.Struct](results.At(0).Type()) {
common.TokenErrorf(pass, results.At(0).Pos(), results.At(0).Name(), "first result must be a struct but is %s", results.At(0).Type())
}
if results.At(1).Type().String() == common.FtlUnitTypePath {
common.TokenErrorf(pass, results.At(1).Pos(), results.At(1).Name(), "second result must not be ftl.Unit")
}
Expand Down

0 comments on commit 6da5eea

Please sign in to comment.