uuid

package module
v0.0.0-...-0c810ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 5 Imported by: 0

README

uuid

Opinionated ID for APIs. Uses a UUID for backend (sql) and shortuuid for frontend (json).

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID struct {
	guuid.UUID
}

ID represents a UUID that can be marshaled as a shortuuid for JSON and as a regular UUID for database operations

Example
package main

import (
	"fmt"

	"github.com/freddierice/uuid"
	guuid "github.com/google/uuid"
)

func main() {
	// Create a new ID
	id := uuid.New()
	fmt.Printf("UUID string: %s\n", id.String())
	fmt.Printf("Short string: %s\n", id.ShortString())

	// Create from existing UUID
	existingUUID := guuid.New()
	idFromUUID := uuid.FromUUID(existingUUID)
	fmt.Printf("From UUID: %s\n", idFromUUID.ShortString())

	// Parse from shortuuid string
	parsed, _ := uuid.Parse(id.ShortString())
	fmt.Printf("Parsed equals original: %t\n", parsed.Equal(id))

	// Output will vary due to random UUIDs, but structure will be:
	// UUID string: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
	// Short string: xxxxxxxxxxxxxxxxxxxxxx
	// From UUID: xxxxxxxxxxxxxxxxxxxxxx
	// Parsed equals original: true
}
Example (Json)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/freddierice/uuid"
)

// User demonstrates how to use the ID type in a struct
type User struct {
	ID        uuid.ID         `json:"id" db:"id"`
	Name      string          `json:"name" db:"name"`
	Email     string          `json:"email" db:"email"`
	ManagerID uuid.NullableID `json:"manager_id" db:"manager_id"`
}

func main() {
	// Create a manager
	managerID := uuid.New()

	// Create a user with an ID and optional manager
	user := User{
		ID:        uuid.New(),
		Name:      "John Doe",
		Email:     "john@example.com",
		ManagerID: uuid.NewNullable(managerID), // Has a manager
	}

	// Create a user without a manager
	userNoManager := User{
		ID:    uuid.New(),
		Name:  "Jane Smith",
		Email: "jane@example.com",
		// ManagerID will be invalid/null by default
	}

	// Marshal to JSON - IDs will be encoded as shortuuids, null manager_id as null
	jsonData, _ := json.Marshal(user)
	fmt.Printf("JSON with manager: %s\n", jsonData)

	jsonData2, _ := json.Marshal(userNoManager)
	fmt.Printf("JSON without manager: %s\n", jsonData2)

	// Unmarshal back - shortuuids will be decoded to UUIDs internally
	var parsed User
	json.Unmarshal(jsonData, &parsed)
	fmt.Printf("Same ID after JSON round-trip: %t\n", user.ID.Equal(parsed.ID))
	fmt.Printf("Manager ID preserved: %t\n", user.ManagerID.ID.Equal(parsed.ManagerID.ID))

	// Output will vary due to random IDs, but structure will be:
	// JSON with manager: {"id":"xxxxxxxxxxxxxxxxxxxxxx","name":"John Doe","email":"john@example.com","manager_id":"yyyyyyyyyyyyyyyyyyyyyy"}
	// JSON without manager: {"id":"xxxxxxxxxxxxxxxxxxxxxx","name":"Jane Smith","email":"jane@example.com","manager_id":null}
	// Same ID after JSON round-trip: true
	// Manager ID preserved: true
}

func FromString

func FromString(s string) (ID, error)

FromString parses a standard UUID string into an ID

func FromUUID

func FromUUID(u guuid.UUID) ID

FromUUID creates an ID from an existing UUID

func New

func New() ID

New creates a new random ID

func Parse

func Parse(s string) (ID, error)

Parse parses a shortuuid string into an ID

func (ID) Equal

func (id ID) Equal(other ID) bool

Equal returns true if two IDs are equal

func (ID) IsZero

func (id ID) IsZero() bool

IsZero returns true if the ID is the zero UUID

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler to encode as shortuuid

func (*ID) Scan

func (id *ID) Scan(value interface{}) error

Scan implements sql.Scanner for database retrieval from UUID

func (ID) ShortString

func (id ID) ShortString() string

ShortString returns the shortuuid string representation

func (ID) String

func (id ID) String() string

String returns the standard UUID string representation

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler to decode from shortuuid

func (ID) Value

func (id ID) Value() (driver.Value, error)

Value implements driver.Valuer for database storage as UUID

type NullableID

type NullableID struct {
	ID    ID
	Valid bool
}

NullableID represents an ID that can be null in the database

func NewNullable

func NewNullable(id ID) NullableID

NewNullable creates a NullableID with a valid ID

func NullableFromPtr

func NullableFromPtr(id *ID) NullableID

NullableFromPtr creates a NullableID from a pointer

func (NullableID) MarshalJSON

func (nid NullableID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for NullableID

func (NullableID) Ptr

func (nid NullableID) Ptr() *ID

Ptr returns a pointer to the ID if valid, nil otherwise

func (*NullableID) Scan

func (nid *NullableID) Scan(value interface{}) error

Scan implements sql.Scanner for database retrieval

func (*NullableID) UnmarshalJSON

func (nid *NullableID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for NullableID

func (NullableID) Value

func (nid NullableID) Value() (driver.Value, error)

Value implements driver.Valuer for database storage

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL