77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"database/sql/driver"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type uuid [16]byte
|
|
|
|
var urlNS = uuid{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
|
|
|
func (u uuid) UUID5(value string) uuid {
|
|
h := sha1.New()
|
|
h.Write(u[:])
|
|
h.Write([]byte(value))
|
|
return uuid(h.Sum(nil))
|
|
}
|
|
|
|
func (u *uuid) UnmarshalText(data string) error {
|
|
data = strings.Trim(data, "{}")
|
|
data = strings.ReplaceAll(data, "-", "")
|
|
|
|
_, err := hex.Decode(u[:], []byte(data))
|
|
return err
|
|
}
|
|
func (u uuid) MarshalText() string {
|
|
s := hex.EncodeToString(u[:])
|
|
return fmt.Sprintf("{%s-%s-%s-%s-%s}", s[:8], s[8:12], s[12:16], s[16:20], s[20:])
|
|
}
|
|
|
|
func (u uuid) Value() (driver.Value, error) {
|
|
return u[:], nil
|
|
}
|
|
|
|
func (u *uuid) Scan(value any) error {
|
|
if value == nil {
|
|
return nil
|
|
}
|
|
*u = uuid(value.([]byte))
|
|
return nil
|
|
}
|
|
|
|
type uuids []uuid
|
|
|
|
func (lis uuids) ToStrList() strList {
|
|
arr := make(strList, len(lis))
|
|
for i, v := range lis {
|
|
arr[i] = v.MarshalText()
|
|
}
|
|
return arr
|
|
}
|
|
|
|
type strList []string
|
|
|
|
func (l *strList) Scan(value any) error {
|
|
s := value.(string)
|
|
s = strings.Trim(s, "[]")
|
|
for _, v := range strings.Split(s, ",") {
|
|
v = strings.TrimSpace(v)
|
|
v = strings.Trim(v, "\",")
|
|
*l = append(*l, v)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l strList) Value() (driver.Value, error) {
|
|
arr := make([]string, len(l))
|
|
for i, v := range l {
|
|
arr[i] = "\""+v+"\""
|
|
}
|
|
|
|
return "["+strings.Join(arr, ",") +"]", nil
|
|
} |