Home > Enterprise >  gorm read json from interface type of model
gorm read json from interface type of model

Time:09-13

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.

CodePudding user response:

Because gorm use pgx, you can use pgtype package. there's type JSONB

So your model look like this

import (
    ...
    "github.com/jackc/pgtype"
    ...
)

type FlowTransaction struct {
    gorm.Model
    Name                          string
    PartnerTransactionReferenceId string
    ReconData                     pgtype.JSONB `gorm:"type:jsonb"`
    DestinationAccountNumber      *string
    DestinationIfsc               *string
    Amount                        uint64
    PartnerId                     uint
    FlowId                        uint
    SelfSettle                    bool
    IsSandbox                     bool
}

CodePudding user response:

Define the JSON type.


import (
    "database/sql/driver"
    "encoding/json"
    "errors"
    "fmt"
)

type JSON json.RawMessage

func (j *JSON) Scan(value interface{}) error {
    bytes, ok := value.([]byte)
    if !ok {
        return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
    }

    result := json.RawMessage{}
    err := json.Unmarshal(bytes, &result)
    *j = JSON(result)
    return err
}

func (j JSON) Value() (driver.Value, error) {
    if len(j) == 0 {
        return nil, nil
    }
    return json.RawMessage(j).MarshalJSON()
}

and in model struct :

type FlowTransaction struct {
    gorm.Model
    Name                          string
    PartnerTransactionReferenceId string
    ReconData                     JSON `type:jsonb`
    DestinationAccountNumber      *string
    DestinationIfsc               *string
    Amount                        uint64
    PartnerId                     uint
    FlowId                        uint
    SelfSettle                    bool
    IsSandbox                     bool
}
  • Related