goadesign / goa

🌟 Goa: Elevate Go API development! 🚀 Streamlined design, automatic code generation, and seamless HTTP/gRPC support. ✨
https://goa.design
MIT License
5.66k stars 558 forks source link

SkipResponseWriter #3537

Closed duckbrain closed 3 months ago

duckbrain commented 3 months ago

Implementation for #3522

Since the tests, don't include the comment, here's the result:

// Service is the admin service interface.
type Service interface {
    // Assets implements assets.

    // If body implements [io.WriterTo], that implementation will be used instead.
    // Consider [goa.design/goa/v3/pkg.SkipResponseWriter] to adapt existing
    // implementations.
    Assets(context.Context, *AuthenticatedRequest) (res *CustomJSONResponse, body io.ReadCloser, err error)
}

Note: Tried this out in one of my projects and added WriterToFunc to the exported API as well, since I found myself wanting it for most of the handlers I was rewriting. Allows you to use a closure and avoids needing to manually keep track of bytes written, while still preserving the io.WriterTo interface. Useful for times when you're passing the writer to something else that doesn't report how many bytes are written like json.Encoder or xml.Encoder.

raphael commented 3 months ago

Thank you for the great contribution! I was wondering if you'd be up to updating the upload_download example to take advantage of this new feature after we release it?