Add advanced Decodal example

This commit is contained in:
Keisuke Hirata 2026-06-16 10:04:44 +09:00
parent bd3da1aeee
commit 6316939438
No known key found for this signature in database
4 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,22 @@
# Advanced Decodal example
This example exercises multiple current Decodal features together:
- multi-file `import`
- top-level recursive module scope
- schema/value composition with `&`
- deep patch with `//`
- `default` fallback during materialization
- constrained function parameters
- lazy function arguments
- `match` expressions
- nested dot-path fields
- arrays
Run it with:
```sh
cargo run -q -p decodal -- examples/advanced/main.dcdl
```
The entrypoint is `main.dcdl`.

View File

@ -0,0 +1,47 @@
let
schema = import "./schema.dcdl";
profiles = import "./profiles.dcdl";
mkService = (cfg: schema.Service) =>
cfg // {
summary = match cfg.env {
"prod": "production service";
"dev": "development service";
_: "custom service";
};
};
devService = mkService(
profiles.base & {
name = "api-dev";
port = 8443;
}
);
prodService = mkService(
profiles.prod & {
name = "api";
port = 9443;
}
);
disabledService = mkService(
profiles.disabled & {
name = "api-disabled";
port = 10443;
}
);
in
{
services = [
devService,
prodService,
disabledService,
];
selected = match "prod" {
"prod": prodService;
"dev": devService;
_: disabledService;
};
}

View File

@ -0,0 +1,18 @@
# Environment-specific patches.
base = {
env = "dev";
tags = ["decodal", "dev"];
};
prod = base // {
env = "prod";
host = "api.internal";
feature.limit = 100;
tags = ["decodal", "prod"];
};
disabled = base // {
feature.enable = false;
tags = ["decodal", "disabled"];
};

View File

@ -0,0 +1,17 @@
# Shared service schema.
# Top-level fields are recursive, so SecurePort can refer to Port.
Port = Int & >= 1 & <= 65535;
SecurePort = Port & > 443;
Service = {
name = String;
env = String default "dev";
host = String default "localhost";
port = SecurePort default 8443;
feature = {
enable = Bool default true;
limit = Int & >= 0 default 10;
};
};