gorm 从接口类型的模型中读取 JSON

huangapple go评论101阅读模式
英文:

gorm read json from interface type of model

问题

我有一个模型如下:

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
}

在从我的PostgreSQL数据库中读取时,我在ReconData中得到的消息是unreadable could not resolve interface type。我尝试实现了ScanValue方法。

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)。你们有任何想法如何解决这个问题吗?任何帮助都将不胜感激。

英文:

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.

答案1

得分: 2

因为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
}
英文:

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
}

答案2

得分: 1

定义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("无法解组JSONB值:", 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
}
英文:

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
}

huangapple
  • 本文由 发表于 2022年9月12日 23:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/73691386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定