go.temporal.io/server/common/primitives/uuid.go
152 LOC · 53 covered · 99 uncovered · 20 ranges · 4170 concepts · 11 introducers · 2469 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package primitives
import (
"database/sql/driver"
"encoding/hex"
guuid "github.com/google/uuid"
)
// UUID represents a 16-byte universally unique identifier
// this type is a wrapper around google/uuid with the following differences
// - type is a byte slice instead of [16]byte
// - db serialization converts uuid to bytes as opposed to string
type UUID []byte
// MustParseUUID returns a UUID parsed from the given string representation
// returns nil if the input is empty string
// panics if the given input is malformed
if s == "" {
return nil
}
return u[:]
}
// ParseUUID returns a UUID parsed from the given string representation
// returns:
// - nil if the input is empty string
// - error if input is malformed
// - UUID object if input can be parsed and is valid
if s == "" {
}
if err != nil {
return nil, err
}
}
// MustValidateUUID parses and validates UUID contents from the given string representation
// returns empty string if the input is empty string
// panics if the given input is malformed
MustParseUUID(s)
return s
}
// ValidateUUID parses and validates UUID contents from the given string representation
// returns:
// - nil if the input is empty string
// - error if input is malformed
// - input if input can be parsed and is valid
if s == "" {
return "", nil
}
if err != nil {
return "", err
}
}
// NewUUID generates a new random UUID
u, err := guuid.NewV7()
if err != nil {
// Should never happen, but this matches the behavior of google/uuid.NewRandom
return nil
}
}
// Downcast returns the UUID as a byte slice. This is necessary when passing to type sensitive interfaces such as cql.
func (u UUID) Downcast() []byte {
return u
}
// UUIDPtr simply returns a pointer for the given value type
//
//go:fix inline
func UUIDPtr(u UUID) *UUID {
return new(u)
}
// String returns the 36 byte hexstring representation of this uuid
// return empty string if this uuid is nil
if len(u) != 16 {
return ""
}
u.encodeHex(buf[:])
return string(buf[:])
}
// StringPtr returns a pointer to the 36 byte hexstring representation of this uuid
// return empty string if this uuid is nil
func (u UUID) StringPtr() *string {
if len(u) != 16 {
return new("")
}
var buf [36]byte
u.encodeHex(buf[:])
return new(string(buf[:]))
}
return UUID(b).String()
}
//go:fix inline
func stringPtr(v string) *string {
return new(v)
}
// Scan implements sql.Scanner interface to allow this type to be
// parsed transparently by database drivers
if src == nil {
return nil
}
if err := guuid.Scan(src); err != nil {
return err
}
return nil
}
// Value implements sql.Valuer so that UUIDs can be written to databases
// transparently. This method returns a byte slice representation of uuid
return []byte(u), nil
}
hex.Encode(dst, u[:4])
dst[8] = '-'
hex.Encode(dst[9:13], u[4:6])
dst[13] = '-'
hex.Encode(dst[14:18], u[6:8])
dst[18] = '-'
hex.Encode(dst[19:23], u[8:10])
dst[23] = '-'
hex.Encode(dst[24:], u[10:])
}