'Extend golang struct/type from another package

I have done some reading and found similar threads but none seem to work for my use case.

I am trying to extend mongo's primitive library specifically primitive.Decimal128 with my own custom methods here is what I have

type Decimal128 primitive.Decimal128

type Decimal128 struct {
    primitive.Decimal128
}

type PayrollDeduction struct {
    PayTypeYtd          Decimal128           `json:"payTypeYtd" bson:"payTypeYtd"`
}

I know in the snippet you can't have both struct and custom type the same name this is simply for reference as to what I have tried.

Golang itself does not complain and will allow either the struct or custom type, it's mongo's cursor decode that is having the problem

error decoding key payrolls.0.employees.0.payTypes.0.payTypeYtd: cannot decode 128-bit decimal into a models.Decimal128

If I replace the PayTypeYtd type for the official primitive.Decimal128 my cursor.Decode works fine.

I would rather if possible use the struct type as I loose access to the official Decimal128 methods using the custom type.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source