You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
IsZero() allows uuid to support the Nullable interface and interface better with existing code. It can be as simple as:
func (uUUID) IsZero() bool {
returnu==Nil
}
This is particularly important when interfacing UUID with the existing ORM libraries. Heck, IsZero() is even used by the reflect package too!
ToNullUUID
Another helper function that is used by this null package (and is quite convenient) are to-null values from the original primitives. So in the case of uuid.UUID and uuuid.NullUUID, adding the following helper function.
funcToNullUUID[T uuid.UUID| uuid.NullUUID](idT) uuid.NullUUID {
ifv, ok:=any(id).(uuid.UUID); ok {
ifv==uuid.Nil {
return uuid.NullUUID{}
}
return uuid.NullUUID{v, true}
} elseifv, ok:=any(id).(uuid.NullUUID); ok {
returnv
} else {
panic("this should not happen")
}
}
That way we don't have to write the following code:
id:=uuid.New()
// If you know that id is not nilnullVal:= uuid.NullUUID{id, true}
// if you don't know whether or not id is nil.nullVal:= uuid.NullUUID{id, id==uuid.Nil}
// And if you want to use named struct fieldsnullVal:= uuid.NullUUID{UUID: id, Valid: id==uuid.Nil}
// What it looks like with the helper functionnullVal:=uuid.ToNullUUID(id)
This is particularly useful when combined with existing libraries such as ORMs. Casting to uuid.NullUUID is common.
The text was updated successfully, but these errors were encountered:
Summary
Suggest adding a couple helper functions.
IsZero
IsZero()
allows uuid to support theNullable
interface and interface better with existing code. It can be as simple as:This is particularly important when interfacing UUID with the existing ORM libraries. Heck,
IsZero()
is even used by the reflect package too!ToNullUUID
Another helper function that is used by this null package (and is quite convenient) are to-null values from the original primitives. So in the case of
uuid.UUID
anduuuid.NullUUID
, adding the following helper function.That way we don't have to write the following code:
This is particularly useful when combined with existing libraries such as ORMs. Casting to
uuid.NullUUID
is common.The text was updated successfully, but these errors were encountered: