mirror of
https://github.com/dstotijn/hetty.git
synced 2025-07-01 18:47:29 -04:00
32 lines
463 B
Go
32 lines
463 B
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
|
|
"github.com/oklog/ulid"
|
|
)
|
|
|
|
type ULID ulid.ULID
|
|
|
|
func (u *ULID) UnmarshalGQL(v interface{}) (err error) {
|
|
str, ok := v.(string)
|
|
if !ok {
|
|
return fmt.Errorf("ulid must be a string")
|
|
}
|
|
|
|
id, err := ulid.Parse(str)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse ULID: %w", err)
|
|
}
|
|
|
|
*u = ULID(id)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (u ULID) MarshalGQL(w io.Writer) {
|
|
fmt.Fprint(w, strconv.Quote(ulid.ULID(u).String()))
|
|
}
|