Skip to content

Instantly share code, notes, and snippets.

@manakuro
Created December 10, 2021 04:48
Show Gist options
  • Save manakuro/81d853ed70a0c64d0116f63eb6f14ea9 to your computer and use it in GitHub Desktop.
Save manakuro/81d853ed70a0c64d0116f63eb6f14ea9 to your computer and use it in GitHub Desktop.
package ulid
import (
"crypto/rand"
"database/sql/driver"
"fmt"
"io"
"strconv"
"time"
"github.com/oklog/ulid/v2"
)
// ID implements a ULID
type ID string
var defaultEntropySource *ulid.MonotonicEntropy
func init() {
// Seed the default entropy source.
defaultEntropySource = ulid.Monotonic(rand.Reader, 0)
}
// newULID returns a new ULID for time.Now() using the default entropy source.
func newULID() ulid.ULID {
return ulid.MustNew(ulid.Timestamp(time.Now()), defaultEntropySource)
}
// MustNew returns a new ULID for time.Now() given a prefix. This uses the default entropy source.
func MustNew(prefix string) ID {
return ID(prefix + fmt.Sprint(newULID()))
}
// UnmarshalGQL implements the graphql.Unmarshaller interface.
func (i *ID) UnmarshalGQL(v interface{}) error {
return i.Scan(v)
}
// MarshalGQL implements the graphql.Marshaler interface.
func (i ID) MarshalGQL(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(string(i)))
}
// Scan implements the Scanner interface.
func (i *ID) Scan(src interface{}) error {
if src == nil {
return fmt.Errorf("ulid: expected a value")
}
switch s := src.(type) {
case string:
*i = ID(s)
case []byte:
str := string(s)
*i = ID(str)
default:
return fmt.Errorf("ulid: expected a string %v", s)
}
return nil
}
// Value implements the driver Valuer interface.
func (i ID) Value() (driver.Value, error) {
return string(i), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment