Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Value docs re: zero-value behavior #60

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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