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

feat: add support for int, float, and bool on ingress #905

Merged
merged 1 commit into from
Feb 8, 2024

Conversation

wesbillman
Copy link
Collaborator

Some go examples:

//ftl:verb
//ftl:ingress http GET /int
func Int(ctx context.Context, req builtin.HttpRequest[int]) (builtin.HttpResponse[int], error) {
	return builtin.HttpResponse[int]{Body: req.Body}, nil
}

//ftl:verb
//ftl:ingress http GET /float
func Float(ctx context.Context, req builtin.HttpRequest[float64]) (builtin.HttpResponse[float64], error) {
	return builtin.HttpResponse[float64]{Body: req.Body}, nil
}

//ftl:verb
//ftl:ingress http GET /bool
func Bool(ctx context.Context, req builtin.HttpRequest[bool]) (builtin.HttpResponse[bool], error) {
	return builtin.HttpResponse[bool]{Body: req.Body}, nil
}

Copy link

codesandbox bot commented Feb 8, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@wesbillman wesbillman force-pushed the add-int-float-bool-http-ingress-types branch from 2d30d48 to 56d77bf Compare February 8, 2024 22:34
@@ -127,4 +127,34 @@ class Echo {
body = req.body
)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Love the tests!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Me too!

if err := json.Unmarshal(body, &responseInt); err != nil {
return nil, fmt.Errorf("HTTP response body is not valid int: %w", err)
}
return []byte(fmt.Sprintf("%d", responseInt)), nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd use the strconv package rather than Sprintf for these values here and below.

strconv.Itoa(), strconv.FormatBool(), though strconv.FormatFloat() might be too annoying.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

strconv.FormatFloat(responseFloat, 'f', -1, 64 yeah this format is crazy! :)

return nil, err
}

floatVal, err := strconv.ParseFloat(string(bodyData), 64)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't use ParseFloat for integers, it loses precision.

@wesbillman wesbillman force-pushed the add-int-float-bool-http-ingress-types branch from 56d77bf to 9948438 Compare February 8, 2024 23:21
case *schema.Int:
var responseInt int
if err := json.Unmarshal(body, &responseInt); err != nil {
return nil, fmt.Errorf("HTTP response body is not valid int: %w", err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

"not a valid"

@wesbillman wesbillman force-pushed the add-int-float-bool-http-ingress-types branch from 9948438 to 8e3d771 Compare February 8, 2024 23:34
@wesbillman wesbillman merged commit 0013a43 into main Feb 8, 2024
11 checks passed
@wesbillman wesbillman deleted the add-int-float-bool-http-ingress-types branch February 8, 2024 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants