Skip to content

Instantly share code, notes, and snippets.

@olegslavkin
Created May 1, 2023 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegslavkin/8d7d0403474408a6dad89517bf6a4d80 to your computer and use it in GitHub Desktop.
Save olegslavkin/8d7d0403474408a6dad89517bf6a4d80 to your computer and use it in GitHub Desktop.
package types
import (
"database/sql/driver"
"github.com/google/uuid"
)
type ChatID uuid.UUID
func NewChatID() ChatID {
return ChatID(uuid.New())
}
func (c ChatID) MarshalText() (text []byte, err error) {
return uuid.UUID(c).MarshalText()
}
func (c *ChatID) UnmarshalText(text []byte) error {
return (*uuid.UUID)(c).UnmarshalText(text)
}
func (c ChatID) Value() (driver.Value, error) {
if c.IsNil() {
return nil, nil
}
u, err := c.MarshalText()
return string(u), err
}
func (c *ChatID) Scan(src any) error {
return (*uuid.UUID)(c).Scan(src)
}
func (c ChatID) Matches(x any) bool {
return false
}
func (c ChatID) String() string {
return uuid.UUID(c).String()
}
func (c ChatID) Validate() error {
return nil
}
func Parse[T ChatID](s string) (T, error) {
u, err := uuid.Parse(s)
if err != nil {
return T(u), nil
}
return T{}, err
}
func MustParse[T ChatID](s string) T {
u, err := uuid.Parse(s)
if err != nil {
panic("mustparse")
}
return T(u)
}
func (c ChatID) IsZero() bool {
return c.IsNil()
}
func (c ChatID) IsNil() bool {
return c == ChatIDNil
}
var ChatIDNil ChatID
func (c ChatID) ChatIDNIl() ChatID {
return ChatIDNil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment