ggicci / httpin

🍡 HTTP Input for Go - HTTP Request from/to Go Struct (Bi-directional Data Binding between Go Struct and http.Request)
https://ggicci.github.io/httpin/
MIT License
315 stars 23 forks source link

Any way to pre-initialize the struct? #101

Closed matdurand closed 5 months ago

matdurand commented 6 months ago

Hi,

I would like to be able to create and initialize an empty struct for each call to Decode instead of httpin creating a new instance using

rootValue := reflect.New(r.Type)

Any chance there is an option to do that?

ggicci commented 6 months ago

Hi, could you explain what's the purpose of creating the instance by yourself, is it performance concern?

matdurand commented 6 months ago

Not performance no. Basically we already have a way to initialize structs with default values in our business logic, so I don't want to introduce a new syntax with httpin default annotation to set default values at the http controller layer, since it will ultimately lead to someone making a mistaken in the default values definition, thinking they were in the business layer but in reality they are in the http layer.

So if I could create the struct myself and have httpin fill it from the request, I could apply our default values using the same way we do it in our business logic and then pass it to httpin.

ggicci commented 6 months ago

UPDATE: released owl@0.8.0 to support this. Main commit.

TODO: design and update httpin API.

ggicci commented 5 months ago

Closing, see release v0.18.0.