goa icon indicating copy to clipboard operation
goa copied to clipboard

feature: make all marshal/unmarshal functions in `client/encode_decode.go` public

Open gabyx opened this issue 5 months ago • 2 comments

It would be very nice to have all marshal/unmarshal private function in client/encode_decode.go to be public as it enables better integration testing, withhout having to do it manuall again. So having that feature I can do

Scenario:


endpoint := cl.GetContract()
req := cm.GetContractPayload{Token: token, ID: id}
resp, err = endpoint(ctx, &req)  // type "Contract"

// you wanna reuse that `resp` type directly with another endpoint.
// if `encode_decode.go` unmarshal/marshal stuff would be public
// I could do

endpoint := cl.GetBanana()
req2 := cm.GetBananaPayload{Token: token, ID: id}
resp2, err = endpoint(ctx, unmarshalContractToContractRequestBody(resp) ) // marshal/unmarshal to "ContractRequestBody"

gabyx avatar Sep 11 '25 20:09 gabyx

I would be hesitant to make this change wholesale as it seems useful to keep the generated package API clear (only things that are required are exposed - it keeps the cognitive load low). However it should be pretty easy to write a plugin that did that. Plugins have access to the generated files, both their templates and their data.

Also just making sure you are aware of the struct:pkg:path Meta key. Assuming both endpoints are defined in the same design, they could be set to generate the types in the same package making them interchangeable.

raphael avatar Sep 13 '25 04:09 raphael

Hmm I am using the meta tag actually but the response type for the client endpoint cannot be fed directly to the endpoint taking the same type in the design, as the GetBanana endpoint wraps the type again in XXXRequestBody. Maybe I dont understand something quite right.

gabyx avatar Sep 13 '25 20:09 gabyx