Skip to content

[Strongly Typed Params] Typed params in context #406

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

Merged
merged 6 commits into from
Apr 23, 2025

Conversation

EwenQuim
Copy link
Member

First step towards #356

@dylanhitt
Copy link
Collaborator

Is this ready for review?

@EwenQuim
Copy link
Member Author

Yes, it is only a first step as it does not implement the solution but it paves the way to support typed params in a backward-compatible way (except for testing with fuego.NewMockContext)

@dylanhitt
Copy link
Collaborator

Got it. Will review soon.

@EwenQuim EwenQuim changed the title Typed params in context [Strongly Typed Params] Typed params in context Feb 21, 2025
Copy link
Collaborator

@dylanhitt dylanhitt left a comment

Choose a reason for hiding this comment

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

I believe go work and all the go mods should probably be bumped on the commit titled Update go version to 1.24

ctx.go Outdated
_ Context[any, any] = &netHttpContext[any, any]{} // Check that ContextWithBody implements Ctx.
_ ContextWithBody[any] = &netHttpContext[any, any]{} // Check that ContextWithBody implements Ctx.
_ ContextWithBody[string] = &netHttpContext[string, any]{} // Check that ContextWithBody implements Ctx.
_ ValidableCtx = &netHttpContext[any, any]{} // Check that ContextWithBody implements ValidableCtx.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there is a lot of copy pasta right here maybe with the comments? Probably should go above as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

@crunk1
Copy link

crunk1 commented Apr 3, 2025

Hey, just discovered this project yesterday and I'm really impressed :). Forgive me if any of my comments/questions are uninformed.

Will Param type support be an effective replacement for the route options OptionQuery, OptionHeader, OptionPath, etc.?
For back compat, I'm assuming those Options will continue to live on. Will route options and Param type support share logic?
Will validation struct tags be supported?
Will OpenAPI spec generation be updated as well? Will other OpenAPI fields (e.g. description) be definable via struct tags?

All-in-all, I'm excited for this update; I think Param types fit the fuego philosophy much better than declarations via route options - leave it to the function signature to define params instead of the route registering logic (again, forgive me if I'm speaking out of turn).

Also, I'm happy to help once I get more familiar with the codebase - if you'll have me.

@dylanhitt
Copy link
Collaborator

Hey @crunk1

Umm. I guess @EwenQuim and I have never discussed this. I think they should. I'd imagine they Options would live through our v1 at the very least.

@EwenQuim
Copy link
Member Author

EwenQuim commented Apr 4, 2025

Hi @dylanhitt and @crunk1 !

Typed params will not replace the Options for a long time, you can safely use them.

I know it creates a "two ways of doing the same thing" but one is very strict and the other is (too much?) loosely coupled, and users may want to choose between the 2.

@crunk1
Copy link

crunk1 commented Apr 4, 2025

@EwenQuim I'm currently building (in my own project) a wrapper around route registration that translates Param struct types into calls to Options{Query,Header,Path,Cookie} and Param{Required,Default,Example}. I'd be happy to share it with you; perhaps wrapping Options will minimize the "two ways of doing the same thing".

Thanks for the warm welcome from you and @dylanhitt.

@EwenQuim EwenQuim force-pushed the typed-params-in-context branch 3 times, most recently from 0afc291 to 69459c0 Compare April 15, 2025 20:31
@EwenQuim EwenQuim requested a review from dylanhitt April 15, 2025 20:32
@EwenQuim
Copy link
Member Author

Ready for review @ccoVeille @dylanhitt

Comment on lines +645 to +668
// TODO: implementation must pass this test
// require.NotEmpty(t, params)
// require.Equal(t, 456, params.ID)
// require.Equal(t, "hello", params.Other)
// require.Equal(t, "application/json", params.ContentType)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Something left behind?

There is gocritic rule about reporting commented out code

Copy link
Member Author

Choose a reason for hiding this comment

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

No, something that must be implemented in the future

@dylanhitt
Copy link
Collaborator

I do plan on reviewing this sometime this week by the way. Day job is getting in the way this week.

@crunk1
Copy link

crunk1 commented Apr 16, 2025

I don't suppose this will help with your current direction, but I have taken a crack at writing a wrapper library that supports Param types, piggy-backing on fuego's route options: https://github.com/crunk1/xfuego. It populates routes' params types on incoming requests.

example: https://github.com/crunk1/xfuego/blob/main/example/main.go

@EwenQuim
Copy link
Member Author

Omg @crunk1 that's incredible work!!! I'm definitely having a look at your code

@EwenQuim
Copy link
Member Author

@dylanhitt Happy to take your review when you have some time :)

Copy link
Collaborator

@dylanhitt dylanhitt left a comment

Choose a reason for hiding this comment

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

Looks good!

Took me a minute to remember this PR is split. Wa wondering where the hell the spec registration was. 😆

@EwenQuim EwenQuim force-pushed the typed-params-in-context branch from bf11ff2 to d993e71 Compare April 23, 2025 20:43
@EwenQuim EwenQuim merged commit d7ffcf7 into main Apr 23, 2025
7 checks passed
@EwenQuim EwenQuim deleted the typed-params-in-context branch April 23, 2025 20:47
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.

4 participants