As explained above, `type AliasedDate Date` clears any methods you have
defined on `Date`.
But `Date` embeds `time.Time` which has its own `UnmrshalJSON`,
which now gets promoted.

So `Date.UnmsrshalJSON` calls your `UnmsrshalJSON`
`AliastedDate.UnmsrshalJSON` calls `AliastedDate.Time.UnmsrshalJSON`

On Fri, May 20, 2022 at 3:14 PM Marcin Romaszewicz <marc...@gmail.com>
wrote:

> Sorry about mixing up terms, however, my question still stands.
>
> encoding/json looks for the Marshaler/Unmarshaler interface
> convertibility, and in this case, the compiler thinks that the redeclared
> type does implement json.Unmarshaler. I extended my example to also do this
> test via reflection.
>
> https://go.dev/play/p/TrxM2zxG2pX
>
> So, yes, it's a new type, but it seems to behave just like the base type.
>
> On Fri, May 20, 2022 at 12:35 AM Axel Wagner <
> axel.wagner...@googlemail.com> wrote:
>
>> It's not an "aliased type". Type aliases take the form
>> type A = B
>> and they make the name A be interchangeable with the name B. What you
>> have is a type declaration
>> type A B
>> which creates a fully new type, with a new method set, but the same
>> underlying type as B.
>>
>> On Fri, May 20, 2022 at 9:28 AM Marcin Romaszewicz <marc...@gmail.com>
>> wrote:
>>
>>> Hi All,
>>>
>>> I've created a simple struct that wraps time.Time because I'd like to
>>> unmarshal it differently from JSON than the default. It works great.
>>>
>>> However, when I pass a type alias of that struct to json.Marshal or
>>> json.Unmarshal, the MarshalJSON and UnmarshalJSON functions aren't invoked,
>>> despite both types conforming to the interfaces.
>>>
>>> Here's the shortest example I could make.
>>> https://go.dev/play/p/pETWQB3CWxV
>>>
>>> This is where this came up:
>>> https://github.com/deepmap/oapi-codegen/issues/579
>>>
>>> I did find a workaround, in redeclaring the MarshalJSON/UnmarshalJSON on
>>> the aliased type, but I don't understand why this is necessary, and I'm
>>> hoping someone who understands the innards of this better than I do could
>>> enlighten me.
>>>
>>> Thanks,
>>> -- Marcin
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to golang-nuts+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LuOa9sHFWxPxqYq_HtiQZ6_pUhCtMUOAL8Yq1X_UvyjJw%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LuOa9sHFWxPxqYq_HtiQZ6_pUhCtMUOAL8Yq1X_UvyjJw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LvC7NY-wGXX9JUQAcGPohLB_puPz0F2Nehsq7Xx2zAZeg%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LvC7NY-wGXX9JUQAcGPohLB_puPz0F2Nehsq7Xx2zAZeg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPpyzxzpqNy%3DqFo3pdRp2HT8Gks-xSB9e7KLmEVG%3DTb%2Bwg%40mail.gmail.com.

Reply via email to