亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 438 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號