Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: support mysql 5.6 #520

Merged
merged 24 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions cdnsystem/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func New(cfg *config.Config) (*Server, error) {
if err := s.register(context.Background()); err != nil {
return nil, err
}
logger.Info("cdn register to manager")
}

return s, nil
Expand Down Expand Up @@ -180,17 +179,14 @@ func (s *Server) register(ctx context.Context) error {
logger.Infof("update cdn %s to manager successfully", cdn.HostName)

cdnClusterID := s.config.Manager.CDNClusterID
if cdnClusterID != 0 {
if _, err := s.managerClient.AddCDNToCDNCluster(ctx, &manager.AddCDNToCDNClusterRequest{
CdnId: cdn.Id,
CdnClusterId: cdnClusterID,
}); err != nil {
logger.Warnf("add cdn %s to cdn cluster %s failed %v", cdn.HostName, cdnClusterID, err)
return err
}
logger.Infof("add cdn %s to cdn cluster %s successfully", cdn.HostName, cdnClusterID)
if _, err := s.managerClient.AddCDNToCDNCluster(ctx, &manager.AddCDNToCDNClusterRequest{
CdnId: cdn.Id,
CdnClusterId: cdnClusterID,
}); err != nil {
logger.Warnf("add cdn %s to cdn cluster %s failed %v", cdn.HostName, cdnClusterID, err)
return err
}

logger.Infof("add cdn %s to cdn cluster %s successfully", cdn.HostName, cdnClusterID)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
github.com/jarcoal/httpmock v1.0.8
github.com/klauspost/compress v1.13.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mcuadros/go-gin-prometheus v0.1.0
github.com/mitchellh/mapstructure v1.4.1
Expand Down Expand Up @@ -73,9 +72,9 @@ require (
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
gorm.io/datatypes v1.0.1
gorm.io/driver/mysql v1.0.5
gorm.io/gorm v1.21.9
gorm.io/plugin/soft_delete v1.0.2
k8s.io/apimachinery v0.20.6 // indirect
k8s.io/client-go v11.0.0+incompatible
)
25 changes: 10 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc h1:VRRKCwnzqk8QCaRC4os14xoKDdbHqqlJtJA0oc1ZAjg=
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D0yGjAk=
github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
Expand Down Expand Up @@ -525,9 +524,8 @@ github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA=
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
Expand Down Expand Up @@ -800,7 +798,6 @@ golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -1222,30 +1219,28 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/datatypes v1.0.1 h1:6npnXbBtjpSb7FFVA2dG/llyTN8tvZfbUqs+WyLrYgQ=
gorm.io/datatypes v1.0.1/go.mod h1:HEHoUU3/PO5ZXfAJcVWl11+zWlE16+O0X2DgJEb4Ixs=
gorm.io/driver/mysql v1.0.3/go.mod h1:twGxftLBlFgNVNakL7F+P/x9oYqoymG3YYT8cAfI9oI=
gorm.io/driver/mysql v1.0.5 h1:WAAmvLK2rG0tCOqrf5XcLi2QUwugd4rcVJ/W3aoon9o=
gorm.io/driver/mysql v1.0.5/go.mod h1:N1OIhHAIhx5SunkMGqWbGFVeh4yTNWKmMo1GOAsohLI=
gorm.io/driver/postgres v1.0.8 h1:PAgM+PaHOSAeroTjHkCHCBIHHoBIf9RgPWGo8dF2DA8=
gorm.io/driver/postgres v1.0.8/go.mod h1:4eOzrI1MUfm6ObJU/UcmbXyiHSs8jSwH95G5P5dxcAg=
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/driver/sqlite v1.1.3 h1:BYfdVuZB5He/u9dt4qDpZqiqDJ6KhPqs5QUqsr/Eeuc=
gorm.io/driver/sqlite v1.1.3/go.mod h1:AKDgRWk8lcSQSw+9kxCJnX/yySj8G3rdwYlU57cB45c=
gorm.io/driver/sqlserver v1.0.4 h1:V15fszi0XAo7fbx3/cF50ngshDSN4QT0MXpWTylyPTY=
gorm.io/driver/sqlserver v1.0.4/go.mod h1:ciEo5btfITTBCj9BkoUVDvgQbUdLWQNqdFY5OGuGnRg=
gorm.io/driver/sqlserver v1.0.7 h1:uwUtb0kdFwW5PkRbd2KJ2h4wlsqvLSjox1XVg/RnzRE=
gorm.io/driver/sqlserver v1.0.7/go.mod h1:ng66aHI47ZIKz/vvnxzDoonzmTS8HXP+JYlgg67wOog=
gorm.io/gorm v1.20.0/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.11/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.3/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.4/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.6/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.7/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.21.9 h1:INieZtn4P2Pw6xPJ8MzT0G4WUOsHq3RhfuDF1M6GW0E=
gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/plugin/dbresolver v1.1.0 h1:cegr4DeprR6SkLIQlKhJLYxH8muFbJ4SmnojXvoeb00=
gorm.io/plugin/dbresolver v1.1.0/go.mod h1:tpImigFAEejCALOttyhWqsy4vfa2Uh/vAUVnL5IRF7Y=
gorm.io/plugin/soft_delete v1.0.2 h1:BrchTwbT4qjAJrTjXeAOhdqRnWgRhaIxnQMCTgNYVoQ=
gorm.io/plugin/soft_delete v1.0.2/go.mod h1:gBRnGiHKEXQIST8E/EWXkzQQFzJ09Rk+ZNqmHrrLPEA=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
7 changes: 7 additions & 0 deletions manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type MysqlConfig struct {
Host string `yaml:"host" mapstructure:"host"`
Port int `yaml:"port" mapstructure:"port"`
DBName string `yaml:"dbname" mapstructure:"dbname"`
Migrate bool `yaml:"migrate" mapstructure:"migrate"`
}

type RedisConfig struct {
Expand Down Expand Up @@ -93,6 +94,9 @@ func New() *Config {
BrokerDB: 1,
BackendDB: 2,
},
Mysql: &MysqlConfig{
Migrate: true,
},
},
Cache: &CacheConfig{
Redis: &RedisCacheConfig{
Expand Down Expand Up @@ -139,6 +143,9 @@ func (cfg *Config) Validate() error {
}

if cfg.Database.Mysql == nil {
if cfg.Database.Mysql.Host == "" {
return errors.New("empty cache mysql host is not specified")
}
return errors.New("empty cache mysql config is not specified")
}
}
Expand Down
49 changes: 26 additions & 23 deletions manager/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ func newMyqsl(cfg *config.MysqlConfig) (*gorm.DB, error) {
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
return nil, err
}

// Run migration
if err := migrate(db); err != nil {
return nil, err
if cfg.Migrate {
if err := migrate(db); err != nil {
return nil, err
}
}

// Run seed
Expand All @@ -63,7 +66,7 @@ func newMyqsl(cfg *config.MysqlConfig) (*gorm.DB, error) {
}

func migrate(db *gorm.DB) error {
return db.AutoMigrate(
return db.Set("gorm:table_options", "DEFAULT CHARSET=utf8mb4 ROW_FORMAT=Dynamic").AutoMigrate(
&model.CDNCluster{},
&model.CDN{},
&model.SchedulerCluster{},
Expand All @@ -87,26 +90,6 @@ func seed(db *gorm.DB) error {
}
}

var adminUserCount int64
var adminUserName = "admin"
if err := db.Model(model.User{}).Where("name = ?", adminUserName).Count(&adminUserCount).Error; err != nil {
return err
}
if adminUserCount <= 0 {
encryptedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte("Dragonfly2"), bcrypt.MinCost)
if err != nil {
return err
}
if err := db.Create(&model.User{
EncryptedPassword: string(encryptedPasswordBytes),
Name: adminUserName,
Email: fmt.Sprintf("%s@Dragonfly2.com", adminUserName),
State: model.UserStateEnabled,
}).Error; err != nil {
return err
}
}

var schedulerClusterCount int64
if err := db.Model(model.SchedulerCluster{}).Count(&schedulerClusterCount).Error; err != nil {
return err
Expand Down Expand Up @@ -138,5 +121,25 @@ func seed(db *gorm.DB) error {
}
}

var adminUserCount int64
var adminUserName = "admin"
if err := db.Model(model.User{}).Where("name = ?", adminUserName).Count(&adminUserCount).Error; err != nil {
return err
}
if adminUserCount <= 0 {
encryptedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte("Dragonfly2"), bcrypt.MinCost)
if err != nil {
return err
}
if err := db.Create(&model.User{
EncryptedPassword: string(encryptedPasswordBytes),
Name: adminUserName,
Email: fmt.Sprintf("%s@Dragonfly2.com", adminUserName),
State: model.UserStateEnabled,
}).Error; err != nil {
return err
}
}

return nil
}
12 changes: 12 additions & 0 deletions manager/model/casbin_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package model

type CasbinRule struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:id"`
Ptype string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:policy type"`
V0 string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:v0"`
V1 string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:v1"`
V2 string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:v2"`
V3 string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:v3"`
V4 string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:v4"`
V5 string `gorm:"type:varchar(100);default:NULL;uniqueIndex:uk_casbin_rule;comment:v5"`
}
16 changes: 8 additions & 8 deletions manager/model/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const (

type CDN struct {
Model
HostName string `gorm:"column:host_name;size:256;uniqueIndex;not null" json:"host_name"`
IDC string `gorm:"column:idc;size:1024" json:"idc"`
Location string `gorm:"column:location;size:1024" json:"location"`
IP string `gorm:"column:ip;size:256;not null" json:"ip"`
Port int32 `gorm:"column:port;not null" json:"port"`
DownloadPort int32 `gorm:"column:download_port;not null" json:"download_port"`
Status string `gorm:"type:enum('active', 'inactive');default:'inactive'" json:"status"`
CDNClusterID *uint
HostName string `gorm:"column:host_name;type:varchar(256);index:uk_scheduler_cluster_name,unique;not null;comment:hostname" json:"host_name"`
IDC string `gorm:"column:idc;type:varchar(1024);comment:internet data center" json:"idc"`
Location string `gorm:"column:location;type:varchar(1024);comment:location" json:"location"`
IP string `gorm:"column:ip;type:varchar(256);not null;comment:ip address" json:"ip"`
Port int32 `gorm:"column:port;not null;comment:grpc service listening port" json:"port"`
DownloadPort int32 `gorm:"column:download_port;not null;comment:download service listening port" json:"download_port"`
Status string `gorm:"column:status;type:varchar(256);default:'inactive';comment:service status" json:"status"`
CDNClusterID *uint `gorm:"index:idx_cdn_cluster_cd_ns;comment:cdn cluster id"`
CDNCluster CDNCluster `json:"-"`
}
14 changes: 5 additions & 9 deletions manager/model/cdn_cluster.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package model

import (
"gorm.io/datatypes"
)

type CDNCluster struct {
Model
Name string `gorm:"column:name;size:256;uniqueIndex;not null" json:"name"`
BIO string `gorm:"column:bio;size:1024" json:"bio"`
Config datatypes.JSONMap `gorm:"column:config;not null" json:"config"`
Name string `gorm:"column:name;type:varchar(256);index:uk_cdn_cluster_name,unique;not null;comment:name" json:"name"`
BIO string `gorm:"column:bio;type:varchar(1024);comment:biography" json:"bio"`
Config JSONMap `gorm:"column:config;not null;comment:configuration" json:"config"`
SchedulerClusters []SchedulerCluster `gorm:"many2many:cdn_cluster_scheduler_cluster;" json:"-"`
CDNs []CDN `json:"-"`
SecurityGroupID *uint
SecurityGroup SecurityGroup `json:"-"`
SecurityGroupID *uint `gorm:"comment:security group id"`
SecurityGroup SecurityGroup `json:"-"`
}
63 changes: 59 additions & 4 deletions manager/model/models.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package model

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"

"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/plugin/soft_delete"
)

type Model struct {
ID uint `gorm:"primarykey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
ID uint `gorm:"primarykey;comment:id" json:"id"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;comment:created timestamp" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;comment:updated timestamp" json:"updated_at"`
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag;comment:soft delete flag"`
}

func Paginate(page, perPage int) func(db *gorm.DB) *gorm.DB {
Expand All @@ -19,3 +25,52 @@ func Paginate(page, perPage int) func(db *gorm.DB) *gorm.DB {
return db.Offset(offset).Limit(perPage)
}
}

type JSONMap map[string]interface{}

func (m JSONMap) Value() (driver.Value, error) {
if m == nil {
return nil, nil
}
ba, err := m.MarshalJSON()
return string(ba), err
}

func (m *JSONMap) Scan(val interface{}) error {
var ba []byte
switch v := val.(type) {
case []byte:
ba = v
case string:
ba = []byte(v)
default:
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
}
t := map[string]interface{}{}
err := json.Unmarshal(ba, &t)
*m = JSONMap(t)
return err
}

func (m JSONMap) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
t := (map[string]interface{})(m)
return json.Marshal(t)
}

func (m *JSONMap) UnmarshalJSON(b []byte) error {
t := map[string]interface{}{}
err := json.Unmarshal(b, &t)
*m = JSONMap(t)
return err
}

func (m JSONMap) GormDataType() string {
return "jsonmap"
}

func (JSONMap) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return "text"
}
Loading