ggicci/httpin

decoder registered with ReplaceTypeDecoder not invoked

rlimberger opened this issue · 2 comments

Hi, first of all, httpin is great! Thanks for your hard work!

Im trying to use ReplaceTypeDecoder to override the time.Time decoding like such (v0.10.1):

// register custom decoder for time.Time
func init() {
	httpin.ReplaceTypeDecoder(reflect.TypeOf(time.Now()), httpin.ValueTypeDecoderFunc(decodeDartTimestamp))
}

// custom time.Time decoder
func decodeDartTimestamp(value string) (interface{}, error) {
	return time.Parse(time.RFC3339, value)
}

// example of query input 
type UpcomingReturnsRequest struct {
	After      time.Time `in:"query=after"`
}

// example of body input
type PrioritizedInput struct {
	httpin.JSONBody
	PrioritizedAt    time.Time `json:"prioritized_at"`
}

Unfortunately, my custom decode never gets executed (verified with log as well as breakpoint. What am I doing wrong?

Hi @rlimberger, thanks for the report. I have tried your code on the Go playgroud.

For the query input example, the custom decoder did get executed, you can check it here: https://go.dev/play/p/eDUe--DgFpu

For the body input example, since httpin uses the Go builtin json package encoding/json by default to decode the whole request body directly, the custom time decoder won't be used.

Closed since this works as intended.