gorilla/schema

[bug] Registered encoder doesn't work for struct pointer types

tkhametov opened this issue · 0 comments

Describe the bug
Custom registered encoders don't work for struct pointer types. Let's say I have the following structure:

type CustomTime struct {
	time time.Time
}

type S1 struct {
	DateStart *CustomTime
	DateEnd   *CustomTime
}

And having a registered encoder, I expect to get only 2 fields filled:

encoder.RegisterEncoder(&CustomTime{}, func(value reflect.Value) string {
	if value.IsNil() {
	      return ""
	}

	custom := value.Interface().(*CustomTime)
	return custom.time.String()
})

But instead, it goes through nested fields and resolves their values:
image
image

In other words, it completely ignores my predefined encoders since it's struct pointer. My custom registered encoder will not be executed at all.

Versions
Go version: go version go1.16 windows/amd64
Package version: 9cd959358f4d00eb3c800dc0d41a5ec1a12ced0f

Steps to Reproduce

  1. Declare structures:
type CustomTime struct {
	time time.Time
}

type S1 struct {
	DateStart *CustomTime
	DateEnd   *CustomTime
}
  1. Define custom encoder:
encoder.RegisterEncoder(&CustomTime{}, func(value reflect.Value) string {
	if value.IsNil() {
		return ""
	}

	custom := value.Interface().(*CustomTime)
	return custom.time.String()
})

Expected behavior

A registered encoder is invoked and there are only 2 values returned
image

Code Snippets
See tests in #174