UUID, Email & DateTime
UUID, Email, DateTime & Other Scalars
Section titled “UUID, Email, DateTime & Other Scalars”PyGo provides built-in validated scalar types for common use cases:
- UUID: Universally Unique Identifier (v4 by default)
- Email: RFC 5322-compliant email address
- DateTime: ISO 8601 datetime with timezone awareness
- URL: Validated URL string
- Phone: Validated phone number string
- Decimal: Decimal type for precise arithmetic
All scalar types are validated at compile time and generate idiomatic Go and Python code.
Definition
Section titled “Definition”model User: id: UUID # Auto-generated v4 UUID name: StringGenerated Code
Section titled “Generated Code”Go:
type User struct { ID string `json:"id"` Name string `json:"name"`}Python:
import uuid
class User(BaseModel): id: uuid.UUID name: strDatabase Representation
Section titled “Database Representation”CREATE TABLE user ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL);Definition
Section titled “Definition”model Contact: email: Email name: StringValidation
Section titled “Validation”Emails are validated at compile time:
# Validcontact = Contact(email="user@example.com", name="Alice")
# Invalid — compile-time error# contact = Contact(email="not-an-email", name="Bob")Format: local-part@domain (RFC 5322 compliant)
Generated Code
Section titled “Generated Code”Go:
type Contact struct { Email string `json:"email"` Name string `json:"name"`}Python:
class Contact(BaseModel): email: str # validated on assignment name: strDateTime
Section titled “DateTime”Definition
Section titled “Definition”model Event: title: String started_at: DateTime ended_at: DateTime?Auto-generated Timestamps
Section titled “Auto-generated Timestamps”model Article: title: String created: DateTime = now() # Set on creation updated: DateTime? # Optional, updated manuallyTimezone Handling
Section titled “Timezone Handling”All DateTime fields store timezone-aware datetimes in UTC by default:
event = Event( title="Launch", started_at="2024-01-15T09:00:00+05:00" # Timezone-aware)
# Internally stored as UTC# event.started_at → 2024-01-15 04:00:00 UTCGenerated Code
Section titled “Generated Code”Go:
import "time"
type Event struct { Title string StartedAt time.Time EndedAt *time.Time}Python:
from datetime import datetime
class Event(BaseModel): title: str started_at: datetime ended_at: Optional[datetime] = NoneDatabase Representation
Section titled “Database Representation”CREATE TABLE event ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, started_at TIMESTAMP WITH TIME ZONE, ended_at TIMESTAMP WITH TIME ZONE);Definition
Section titled “Definition”model Website: url: URL name: StringValidated URL string (e.g., https://example.com/path).
Definition
Section titled “Definition”model Contact: phone: Phone name: StringValidated phone number string (e.g., +1-555-123-4567).
Decimal
Section titled “Decimal”Definition
Section titled “Definition”model Product: name: String price: DecimalPrecise decimal type for financial calculations.
Generated Code
Section titled “Generated Code”Go:
type Product struct { Name string Price string // Decimal stored as string for precision}Python:
from decimal import Decimal
class Product(BaseModel): name: str price: DecimalCombining with Other Types
Section titled “Combining with Other Types”Optional Scalars
Section titled “Optional Scalars”model Profile: user_id: UUID secondary_email: Email? = None last_login: DateTime? = NoneArray of Scalars
Section titled “Array of Scalars”model Group: name: String member_ids: Array[UUID]Type Summary
Section titled “Type Summary”| Type | Python Type | Go Type | DB Type |
|---|---|---|---|
UUID |
uuid.UUID |
string |
UUID |
Email |
str (validated) |
string |
TEXT |
DateTime |
datetime |
time.Time |
TIMESTAMP |
URL |
str |
string |
TEXT |
Phone |
str |
string |
TEXT |
Decimal |
Decimal |
string |
TEXT |
Best Practices
Section titled “Best Practices”- Always use
UUIDas primary keys — never auto-increment integers - Validate emails at the model level, not at the controller layer
- Store all datetimes in UTC — convert to local time only for display
- Use
Decimalfor monetary or precise numerical values - Use
Email?for optional email fields