Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active May 22, 2021 09:55
Show Gist options
  • Save dipeshhkc/46781db11a11df8dd5a1d1bd062a17d2 to your computer and use it in GitHub Desktop.
Save dipeshhkc/46781db11a11df8dd5a1d1bd062a17d2 to your computer and use it in GitHub Desktop.
//MYTYPE -> new datatype
type MYTYPE uuid.UUID
// StringToMYTYPE -> parse string to MYTYPE
func StringToMYTYPE(s string) (MYTYPE, error) {
id, err := uuid.Parse(s)
return MYTYPE(id), err
}
//String -> String Representation of Binary16
func (my MYTYPE) String() string {
return uuid.UUID(my).String()
}
//GormDataType -> sets type to binary(16)
func (my MYTYPE) GormDataType() string {
return "binary(16)"
}
func (my MYTYPE) MarshalJSON() ([]byte, error) {
s := uuid.UUID(my)
str := "\"" + s.String() + "\""
return []byte(str), nil
}
func (my *MYTYPE) UnmarshalJSON(by []byte) error {
s, err := uuid.ParseBytes(by)
*my = MYTYPE(s)
return err
}
// Scan --> tells GORM how to receive from the database
func (my *MYTYPE) Scan(value interface{}) error {
bytes, _ := value.([]byte)
parseByte, err := uuid.FromBytes(bytes)
*my = MYTYPE(parseByte)
return err
}
// Value -> tells GORM how to save into the database
func (my MYTYPE) Value() (driver.Value, error) {
return uuid.UUID(my).MarshalBinary()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment