package common import ( "database/sql/driver" "github.com/oklog/ulid/v2" ) // GUID our new ULID based datatype type GUID ulid.ULID // StringToGUID -> parse string to ULID GUID func StringToGUID(s string) (GUID, error) { id, err := ulid.Parse(s) // maybe ParseStrict return GUID(id), err } // String Representation of Binary16 func (guid GUID) String() string { return ulid.ULID(guid).String() } // GormDataType sets type to binary(16) func (guid GUID) GormDataType() string { return "binary(16)" } func (guid GUID) MarshalJSON() ([]byte, error) { str := "\"" + ulid.ULID(guid).String() + "\"" return []byte(str), nil } func (guid *GUID) UnmarshalJSON(data []byte) error { id, err := ulid.Parse(string(data)) // maybe ParseStrict *guid = GUID(id) return err } // Scan tells GORM how to receive from the database func (guid *GUID) Scan(value interface{}) error { converted := ulid.ULID(*guid) c := &converted err := c.Scan(value) *guid = GUID(*c) return err } // Value tells GORM how to save into the database func (guid GUID) Value() (driver.Value, error) { return ulid.ULID(guid).Value() }