style: auto-format example files with lux fmt
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,67 +1,41 @@
|
||||
// Schema Evolution Demo
|
||||
// Demonstrates version tracking and automatic migrations
|
||||
|
||||
// ============================================================
|
||||
// PART 1: Type-Declared Migrations
|
||||
// ============================================================
|
||||
|
||||
// Define a versioned type with a migration from v1 to v2
|
||||
type User @v2 {
|
||||
type User = {
|
||||
name: String,
|
||||
email: String,
|
||||
|
||||
// Migration from v1: add default email
|
||||
from @v1 = { name: old.name, email: "unknown@example.com" }
|
||||
}
|
||||
|
||||
// Create a v1 user
|
||||
let v1_user = Schema.versioned("User", 1, { name: "Alice" })
|
||||
let v1_version = Schema.getVersion(v1_user) // 1
|
||||
|
||||
// Migrate to v2 - uses the declared migration automatically
|
||||
let v1_version = Schema.getVersion(v1_user)
|
||||
|
||||
let v2_user = Schema.migrate(v1_user, 2)
|
||||
let v2_version = Schema.getVersion(v2_user) // 2
|
||||
|
||||
// ============================================================
|
||||
// PART 2: Runtime Schema Operations (separate type)
|
||||
// ============================================================
|
||||
let v2_version = Schema.getVersion(v2_user)
|
||||
|
||||
// Create versioned values for a different type (no migration)
|
||||
let config1 = Schema.versioned("Config", 1, "debug")
|
||||
|
||||
let config2 = Schema.versioned("Config", 2, "release")
|
||||
|
||||
// Check versions
|
||||
let c1 = Schema.getVersion(config1) // 1
|
||||
let c2 = Schema.getVersion(config2) // 2
|
||||
let c1 = Schema.getVersion(config1)
|
||||
|
||||
let c2 = Schema.getVersion(config2)
|
||||
|
||||
// Migrate config (auto-migration since no explicit migration defined)
|
||||
let upgradedConfig = Schema.migrate(config1, 2)
|
||||
let upgradedConfigVersion = Schema.getVersion(upgradedConfig) // 2
|
||||
|
||||
// ============================================================
|
||||
// PART 2: Practical Example - API Versioning
|
||||
// ============================================================
|
||||
let upgradedConfigVersion = Schema.getVersion(upgradedConfig)
|
||||
|
||||
// Simulate different API response versions
|
||||
fn createResponseV1(data: String): { version: Int, payload: String } =
|
||||
{ version: 1, payload: data }
|
||||
fn createResponseV1(data: String): { version: Int, payload: String } = { version: 1, payload: data }
|
||||
|
||||
fn createResponseV2(data: String, timestamp: Int): { version: Int, payload: String, meta: { ts: Int } } =
|
||||
{ version: 2, payload: data, meta: { ts: timestamp } }
|
||||
fn createResponseV2(data: String, timestamp: Int): { version: Int, payload: String, meta: { ts: Int } } = { version: 2, payload: data, meta: { ts: timestamp } }
|
||||
|
||||
// Version-aware processing
|
||||
fn getPayload(response: { version: Int, payload: String }): String =
|
||||
response.payload
|
||||
fn getPayload(response: { version: Int, payload: String }): String = response.payload
|
||||
|
||||
let resp1 = createResponseV1("Hello")
|
||||
|
||||
let resp2 = createResponseV2("World", 1234567890)
|
||||
|
||||
let payload1 = getPayload(resp1)
|
||||
let payload2 = resp2.payload
|
||||
|
||||
// ============================================================
|
||||
// RESULTS
|
||||
// ============================================================
|
||||
let payload2 = resp2.payload
|
||||
|
||||
fn main(): Unit with {Console} = {
|
||||
Console.print("=== Schema Evolution Demo ===")
|
||||
|
||||
Reference in New Issue
Block a user