2024-12-29 12:51:49 -05:00
|
|
|
package jsontypes
|
|
|
|
|
|
|
|
import (
|
2024-12-29 15:08:06 -05:00
|
|
|
"encoding/json"
|
|
|
|
|
2024-12-29 12:51:49 -05:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UUID uuid.UUID
|
|
|
|
type UUIDs []UUID
|
|
|
|
|
|
|
|
func (u *UUIDs) UUIDs() []uuid.UUID {
|
|
|
|
r := make([]uuid.UUID, 0, len(*u))
|
|
|
|
|
|
|
|
for _, v := range *u {
|
|
|
|
r = append(r, v.UUID())
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-12-29 15:08:06 -05:00
|
|
|
func (u *UUIDs) UnmarshalJSON(b []byte) error {
|
|
|
|
var ss []string
|
|
|
|
err := json.Unmarshal(b, &ss)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
usl := make([]UUID, 0, len(ss))
|
|
|
|
for _, s := range ss {
|
|
|
|
uu, err := uuid.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
usl = append(usl, UUID(uu))
|
|
|
|
}
|
|
|
|
|
|
|
|
*u = usl
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-29 12:51:49 -05:00
|
|
|
func (u UUID) UUID() uuid.UUID {
|
|
|
|
return uuid.UUID(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UUID) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + u.UUID().String() + `"`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UUID) UnmarshalJSON(b []byte) error {
|
2024-12-29 15:08:06 -05:00
|
|
|
id, err := uuid.Parse(string(b[:]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*u = UUID(id)
|
|
|
|
return nil
|
2024-12-29 12:51:49 -05:00
|
|
|
}
|
2024-12-29 13:07:25 -05:00
|
|
|
|
|
|
|
func (u *UUID) UnmarshalText(t []byte) error {
|
|
|
|
var gu uuid.UUID
|
|
|
|
err := gu.UnmarshalText(t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*u = UUID(gu)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|