// M7 Practical Showcase: Typed Structs, Secret Governance, Checked Conversions & Unboxed Loops
// Try:
// veko check --strict-types examples/12_typed_structs_and_secrets.vk
// veko run examples/12_typed_structs_and_secrets.vk
// veko compile --vko examples/12_typed_structs_and_secrets.vk -o dist/12.vko ; veko run dist/12.vko
// veko run --wasm examples/12_typed_structs_and_secrets.vk
// veko compile --native examples/12_typed_structs_and_secrets.vk -o dist/12.exe ; ./dist/12.exe
Upstream = struct {
name str
host str
port i64 = 443
tls bool = true
api_key secret str = ''
}
RouteSummary = struct {
name str
host str
port i64
load i64
}
// Runtime entry type guards enforce Upstream schema and u8 bounds [0..255]
compute-weight = fn [ep Upstream, factor u8, samples i64] i64 [
mut acc = 0
mut i = 0
while i < samples [
acc = acc + (i & 7) + factor
i = i + 1
]
acc + ep.port
]
format-target = fn [ep Upstream] str [
str/join [ep.name ep.host (mold ep.port)] ':'
]
// 14-byte inline SSO holds short hostnames with zero heap allocations
secret-tok = os/getenv/secret/or 'VEKO_GATEWAY_KEY' ['dev-secret-fallback']
secret-buf = bytes/with-secret 8
println (str/join ['secret-buf-mold=' (mold secret-buf)] '')
// Checked conversions fail cleanly with /or instead of truncating or panicking
parsed-port = str/to-i64/or '8443' [443]
bad-port = str/to-i64/or 'not-a-number' [-1]
clamped-u8 = as-u8/or 300 [as-u8 255]
ok-u8 = as-u8/or 12 [as-u8 0]
primary = Upstream {
name = 'edge-us'
host = 'api.veko.local'
api_key = secret-tok
}
standby = primary \ struct/with { name = 'edge-eu', host = 'eu.veko.local', port = parsed-port }
// Passing `standby` or `secret-buf` directly to `json/encode` or `os/write-file` is blocked
// both statically by `veko check` and recursively at runtime; print/mold safely redact to `<secret>`.
println (str/join ['target=' (format-target standby)] '')
println (str/join ['bad-port=' (mold bad-port)] '')
println (str/join ['clamped-u8=' (mold clamped-u8)] '')
// Unboxed typed register loop runs without heap tag boxing per iteration
score = compute-weight standby ok-u8 5000
summary = RouteSummary {
name = standby.name
host = standby.host
port = standby.port
load = score
}
println (str/join ['summary=' (json/encode summary)] '')