gorm custom type

gorm 에 custom type을 넣고싶다면 해당 type에 scanner 와 valuer 가 구현되어 있으면 된다.


import (
	"database/sql/driver"
	"encoding/json"

	"github.com/jinzhu/gorm"
	"github.com/pkg/errors"
)


// labels gorm bindig
type Labels map[string]string

func (labels Labels) Scan(src interface{}) error {
	str, ok := src.([]byte)
	if !ok {
		return errors.New("must []byte")
	}
	return json.Unmarshal(str, labels)
}
func (labels Labels) Value() (driver.Value, error) {
	return json.Marshal(labels)
}