Skip to content

Commit

Permalink
Update Value docs re: zero-value behavior (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon authored Sep 22, 2020
1 parent a75b9f7 commit c6bb9e1
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,19 +469,38 @@ func (id *ULID) Scan(src interface{}) error {
return ErrScanValue
}

// Value implements the sql/driver.Valuer interface. This returns the value
// represented as a byte slice. If instead a string is desirable, a wrapper
// type can be created that calls String().
// Value implements the sql/driver.Valuer interface, returning the ULID as a
// slice of bytes, by invoking MarshalBinary. If your use case requires a string
// representation instead, you can create a wrapper type that calls String()
// instead.
//
// // stringValuer wraps a ULID as a string-based driver.Valuer.
// type stringValuer ULID
// type stringValuer ulid.ULID
//
// func (id stringValuer) Value() (driver.Value, error) {
// return ULID(id).String(), nil
// }
// func (v stringValuer) Value() (driver.Value, error) {
// return ulid.ULID(v).String(), nil
// }
//
// // Example usage.
// db.Exec("...", stringValuer(id))
//
// All valid ULIDs, including zero-value ULIDs, return a valid Value with a nil
// error. If your use case requires zero-value ULIDs to return a non-nil error,
// you can create a wrapper type that special-cases this behavior.
//
// var zeroValueULID ulid.ULID
//
// type invalidZeroValuer ulid.ULID
//
// func (v invalidZeroValuer) Value() (driver.Value, error) {
// if ulid.ULID(v).Compare(zeroValueULID) == 0 {
// return nil, fmt.Errorf("zero value")
// }
// return ulid.ULID(v).Value()
// }
//
// // Example usage.
// db.Exec("...", invalidZeroValuer(id))
//
// // Example usage.
// db.Exec("...", stringValuer(id))
func (id ULID) Value() (driver.Value, error) {
return id.MarshalBinary()
}
Expand Down

0 comments on commit c6bb9e1

Please sign in to comment.