Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a GORM custom data type, how to get context in scan?

Tags:

go

go-gorm

I'm trying to write a Gorm custom data type: https://gorm.io/docs/data_types.html

type MyDataType struct {}

func (f *MyDataType) Scan(value interface{}) error {
    //
}

func (f MyDataType) Value() (driver.Value, error) {
    //
}

For some reasons, I need to access context in Scan or to be able to retrieve field tags.

Is there a way to do this ? Thanks

like image 471
boolangery Avatar asked Nov 06 '22 04:11

boolangery


1 Answers

Value are based on user space which you may need to change the code to be looks like :

type MyDataType struct {}

func (f *MyDataType) Scan(context context.Context, value interface{}) error {
    // proceed the context right there
}

func (f MyDataType) Value() (driver.Value, error) {
    //
}
like image 150
dodo nosk Avatar answered Nov 11 '22 03:11

dodo nosk