为了账号安全,请及时绑定邮箱和手机立即绑定

gorm 从模型的接口类型读取 json

gorm 从模型的接口类型读取 json

Go
慕标琳琳 2023-02-14 15:03:15
我有一个模型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}从我的 postgres 数据库中读取时,在我的数据库中,ReconData我收到的消息为unreadable could not resolve interface type. 我尝试实施扫描和价值方法。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)}但它给出了一个错误invalid receiver type customJson (pointer or interface type)。你们知道我该如何解决这个问题吗?任何帮助表示赞赏。
查看完整描述

2 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

定义 JSON 类型。



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()

}


在模型结构中:


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

}


查看完整回答
反对 回复 2023-02-14
?
智慧大石

TA贡献1946条经验 获得超3个赞

因为 gorm 使用pgx,你可以使用pgtype包。有类型JSONB

所以你的模型看起来像这样

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

}


查看完整回答
反对 回复 2023-02-14
  • 2 回答
  • 0 关注
  • 465 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号