I have a model as
type FlowTransaction struct {
gorm.Model
Name string
PartnerTransactionReferenceId string
ReconData interface{} `gorm:"type:jsonb"`
DestinationAccountNumber *string
DestinationIfsc *string
Amount uint64
PartnerId uint
FlowId uint
SelfSettle bool
IsSandbox bool
}
while reading from my postgres database, in my ReconData
I am getting message as unreadable could not resolve interface type
. I tried implementing scan and value methods.
type customJson interface{}
func (j *customJson) Scan(value interface{}) error {
return Scan(value, j)
}
func (j customJson) Value() (driver.Value, error) {
return Value(j)
}
func Scan[dtoType any](value interface{}, model *dtoType) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}
err := json.Unmarshal(bytes, model)
return err
}
func Value[dtoType any](j dtoType) ([]byte, error) {
return json.Marshal(j)
}
but it is giving an error as invalid receiver type customJson (pointer or interface type)
. Do you guys have any idea how can I go about solving this. Any help is appreciated.
2
Answers
Because gorm use pgx, you can use pgtype package. there’s type JSONB
So your model look like this
Define the JSON type.
and in model struct :