Documentation
¶
Index ¶
- type ID
- func (id ID) Equal(other ID) bool
- func (id ID) IsZero() bool
- func (id ID) MarshalJSON() ([]byte, error)
- func (id *ID) Scan(value interface{}) error
- func (id ID) ShortString() string
- func (id ID) String() string
- func (id *ID) UnmarshalJSON(data []byte) error
- func (id ID) Value() (driver.Value, error)
- type NullableID
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ID ¶
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 ¶
FromString parses a standard UUID string into an ID
func (ID) MarshalJSON ¶
MarshalJSON implements json.Marshaler to encode as shortuuid
func (ID) ShortString ¶
ShortString returns the shortuuid string representation
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler to decode from shortuuid
type NullableID ¶
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