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

How do I use httpin.RegisterTypeDecoder in the latest version? #69

Closed vamshiaruru32 closed 1 year ago

vamshiaruru32 commented 1 year ago

Hello everyone, I am currently on httpin 0.11.0 and I use httpin.RegisterTypeDecoder to register types for pointer fields (I do not want to use the new patch fields because of personal preferences). But looks like the method has been removed in 0.12.0 as part of refactor. What is the new way of registering a new type decoder?

Previous code:

func decodeStringPointer(value string) (any, error) {
    return &value, nil
}

func init() {
    strVal := "some string"
    httpin.RegisterTypeDecoder(reflect.TypeOf(&strVal), httpin.ValueTypeDecoderFunc(decodeStringPointer))
}

What's the new code? Thanks in advance!

ggicci commented 1 year ago

Hi @vamshiaruru32, sorry the new code brought in some breacking changes of the APIs.

Quick answer here:

func decodeStringPointer(value string) (any, error) {
    return &value, nil
}

func init() {
    httpin.RegisterValueTypeDecoder[*string](httpin.DecoderFunc[string](decodeStringPointer))
}

But for the latest version v0.14.0, you don't need to create pointer type decoders, it's already been registered automatically by httpin.

vamshiaruru32 commented 1 year ago

Thank you! I have upgraded to 0.14.0 and removed the register functions, and everything is working. I'll be closing this issue now :)

CleverCoder commented 10 months ago

This should mandate a MAJOR version number change per semver. 😢 You should also update the documentation here: https://ggicci.github.io/httpin/directives/decoder