generated from mrz1836/go-template
/
model_ids.go
54 lines (44 loc) · 1.06 KB
/
model_ids.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package engine
import (
"database/sql/driver"
"encoding/json"
"github.com/bitcoin-sv/spv-wallet/engine/datastore"
"github.com/bitcoin-sv/spv-wallet/engine/utils"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// IDs are string ids saved as an array
type IDs []string
// GormDataType type in gorm
func (i IDs) GormDataType() string {
return gormTypeText
}
// Scan scan value into JSON, implements sql.Scanner interface
func (i *IDs) Scan(value interface{}) error {
if value == nil {
return nil
}
byteValue, err := utils.ToByteArray(value)
if err != nil {
return nil
}
return json.Unmarshal(byteValue, &i)
}
// Value return json value, implement driver.Valuer interface
func (i IDs) Value() (driver.Value, error) {
if i == nil {
return nil, nil
}
marshal, err := json.Marshal(i)
if err != nil {
return nil, err
}
return string(marshal), nil
}
// GormDBDataType the gorm data type for metadata
func (IDs) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
if db.Dialector.Name() == datastore.Postgres {
return datastore.JSONB
}
return datastore.JSON
}