aion-package 0.10.0

Archive validation, content hashing, and namespacing for Aion workflow packages.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Emission of the generated codecs module `src/<pkg>_codecs.gleam`.
//!
//! Types-first: the author declares boundary types in `src/<pkg>_io.gleam`;
//! this module emits everything wire-shaped for them — per type a
//! `<prefix>_to_json` encoder, a `<prefix>_decoder`, and a typed
//! `<prefix>_codec()` — so no codec is ever hand-written (ADR-014). The
//! emitted idioms mirror the previously generated io-module codecs:
//! `json.object` / `json.array` encoders and `gleam/dynamic/decode`
//! use-chained decoders, with `decode.optional_field` + `decode.optional` for
//! optional fields (omitted from the wire when `None`).
//!
//! Emission is a pure, deterministic function of the boundary-type model
//! (types in name order, fields in declared order), so a
//! delete-and-regenerate round-trip is byte-identical and
//! [`CodegenMode::Check`](super::project::CodegenMode) can byte-compare.

use std::fmt::Write as _;

use super::model::{BoundaryType, EnumDef, Field, GleamType, RecordDef, TypeDef};

/// Header line every types-derived generated module starts with (the
/// do-not-edit contract; `--check` regenerates and byte-compares against it).
pub(crate) const GENERATED_TYPES_HEADER: &str =
    "//// Generated by aion generate — do not edit; regenerate from the project's types module.";

/// Renders `src/<pkg>_codecs.gleam` for the boundary types (already sorted by
/// type name): per type an encoder, a decoder, and a typed codec wrapper.
pub(crate) fn emit_codecs_module(package_name: &str, types: &[BoundaryType]) -> String {
    let has_optionals = types.iter().any(|boundary| {
        own_def(boundary).is_some_and(|def| match def {
            TypeDef::Record(record) => record.fields.iter().any(|field| !field.required),
            TypeDef::Enum(_) => false,
        })
    });

    let mut out = String::new();
    let _ = writeln!(out, "{GENERATED_TYPES_HEADER}");
    out.push_str("////\n");
    let _ = writeln!(
        out,
        "//// JSON codecs for the `{package_name}` boundary types declared in"
    );
    let _ = writeln!(
        out,
        "//// `src/{package_name}_io.gleam`: per type, a schema-shaped encoder/decoder"
    );
    out.push_str("//// pair and a typed codec for activity and workflow I/O.\n");
    out.push('\n');
    out.push_str("import aion/codec\n");
    out.push_str("import gleam/dynamic/decode\n");
    out.push_str("import gleam/json\n");
    if has_optionals {
        out.push_str("import gleam/list\n");
        out.push_str("import gleam/option\n");
    }
    let _ = writeln!(out, "import {package_name}_io as io");

    for boundary in types {
        // Each boundary type's own definition leads its closure; siblings it
        // references are boundary types themselves, emitted on their own turn.
        match own_def(boundary) {
            Some(TypeDef::Record(record)) => emit_record_codecs(&mut out, record),
            Some(TypeDef::Enum(definition)) => emit_enum_codecs(&mut out, definition),
            None => {}
        }
    }
    out
}

/// The boundary type's own definition (always first in its closure).
fn own_def(boundary: &BoundaryType) -> Option<&TypeDef> {
    boundary.defs.first()
}

/// Emits the encoder, decoder, and codec for one record type.
fn emit_record_codecs(out: &mut String, record: &RecordDef) {
    let name = &record.type_name;
    let prefix = &record.fn_prefix;

    // Encoder.
    let _ = write!(
        out,
        "\n/// Encodes `io.{name}` as schema-shaped JSON; optional fields are\n\
         /// omitted when `None`.\n\
         pub fn {prefix}_to_json(value: io.{name}) -> json.Json {{\n"
    );
    if record.fields.iter().all(|field| field.required) {
        out.push_str("  json.object([\n");
        for field in &record.fields {
            let _ = writeln!(out, "    {},", field_pair(field));
        }
        out.push_str("  ])\n");
    } else {
        out.push_str("  json.object(\n    list.flatten([\n");
        for field in &record.fields {
            if field.required {
                let _ = writeln!(out, "      [{}],", field_pair(field));
            } else {
                let _ = writeln!(out, "      case value.{} {{", field.wire);
                let _ = writeln!(
                    out,
                    "        option.Some(present) -> [#(\"{}\", {})]",
                    field.wire,
                    encode_call(&field.ty, "present", 0)
                );
                out.push_str("        option.None -> []\n      },\n");
            }
        }
        out.push_str("    ]),\n  )\n");
    }
    out.push_str("}\n");

    // Decoder. Bindings are hygienic (`field_<wire>`): a raw `use <wire>`
    // binding for a field named `decode` or `option` would shadow the
    // generated imports for the rest of the decoder and fail `gleam build`.
    let _ = write!(
        out,
        "\n/// Decoder for `io.{name}` from schema-shaped JSON.\n\
         pub fn {prefix}_decoder() -> decode.Decoder(io.{name}) {{\n"
    );
    for field in &record.fields {
        if field.required {
            let _ = writeln!(
                out,
                "  use field_{wire} <- decode.field(\"{wire}\", {})",
                decoder_expr(&field.ty),
                wire = field.wire,
            );
        } else {
            let _ = writeln!(
                out,
                "  use field_{wire} <- decode.optional_field(\n    \"{wire}\",\n    \
                 option.None,\n    decode.optional({}),\n  )",
                decoder_expr(&field.ty),
                wire = field.wire,
            );
        }
    }
    if record.fields.is_empty() {
        let _ = writeln!(out, "  decode.success(io.{name})");
    } else {
        let _ = writeln!(out, "  decode.success(io.{name}(");
        for field in &record.fields {
            let _ = writeln!(out, "    {wire}: field_{wire},", wire = field.wire);
        }
        out.push_str("  ))\n");
    }
    out.push_str("}\n");

    emit_codec_wrapper(out, name, prefix);
}

/// Emits the encoder, decoder, and codec for one enum type.
fn emit_enum_codecs(out: &mut String, definition: &EnumDef) {
    // The interface front-end only builds enums with two or more variants, so
    // a first variant always exists (needed as the `decode.failure`
    // placeholder). Were the invariant ever broken, the missing functions
    // would fail `gleam build` loudly rather than panic here.
    let Some(first_variant) = definition.variants.first() else {
        return;
    };
    let name = &definition.type_name;
    let prefix = &definition.fn_prefix;

    let _ = write!(
        out,
        "\n/// Encodes `io.{name}` as its wire string.\n\
         pub fn {prefix}_to_json(value: io.{name}) -> json.Json {{\n  case value {{\n"
    );
    for variant in &definition.variants {
        let _ = writeln!(
            out,
            "    io.{} -> json.string(\"{}\")",
            variant.constructor, variant.wire
        );
    }
    out.push_str("  }\n}\n");

    let _ = write!(
        out,
        "\n/// Decoder for `io.{name}` from its wire string.\n\
         pub fn {prefix}_decoder() -> decode.Decoder(io.{name}) {{\n  \
         decode.then(decode.string, fn(raw) {{\n    case raw {{\n"
    );
    for variant in &definition.variants {
        let _ = writeln!(
            out,
            "      \"{}\" -> decode.success(io.{})",
            variant.wire, variant.constructor
        );
    }
    let _ = writeln!(
        out,
        "      _ -> decode.failure(io.{}, \"{name}\")",
        first_variant.constructor
    );
    out.push_str("    }\n  })\n}\n");

    emit_codec_wrapper(out, name, prefix);
}

/// Emits the typed `<prefix>_codec()` wrapper for one type.
fn emit_codec_wrapper(out: &mut String, name: &str, prefix: &str) {
    let _ = write!(
        out,
        "\n/// Typed codec for `io.{name}` (activity and workflow I/O).\n\
         pub fn {prefix}_codec() -> codec.Codec(io.{name}) {{\n  \
         codec.json_codec({prefix}_to_json, {prefix}_decoder())\n}}\n"
    );
}

/// `#("wire", <encoded value.field>)` for a required field.
fn field_pair(field: &Field) -> String {
    format!(
        "#(\"{wire}\", {})",
        encode_call(&field.ty, &format!("value.{}", field.wire), 0),
        wire = field.wire,
    )
}

/// An applied encoder expression for `expr` of type `ty`.
fn encode_call(ty: &GleamType, expr: &str, depth: usize) -> String {
    match ty {
        GleamType::String => format!("json.string({expr})"),
        GleamType::Int => format!("json.int({expr})"),
        GleamType::Float => format!("json.float({expr})"),
        GleamType::Bool => format!("json.bool({expr})"),
        GleamType::List(inner) => format!("json.array({expr}, {})", encode_fn(inner, depth)),
        GleamType::Named { fn_prefix, .. } => format!("{fn_prefix}_to_json({expr})"),
    }
}

/// A point-free (or lambda, for nested lists) encoder function for `ty`.
fn encode_fn(ty: &GleamType, depth: usize) -> String {
    match ty {
        GleamType::String => "json.string".to_owned(),
        GleamType::Int => "json.int".to_owned(),
        GleamType::Float => "json.float".to_owned(),
        GleamType::Bool => "json.bool".to_owned(),
        GleamType::List(inner) => {
            let var = format!("items{depth}");
            format!(
                "fn({var}) {{ json.array({var}, {}) }}",
                encode_fn(inner, depth + 1)
            )
        }
        GleamType::Named { fn_prefix, .. } => format!("{fn_prefix}_to_json"),
    }
}

/// A decoder expression for `ty`.
fn decoder_expr(ty: &GleamType) -> String {
    match ty {
        GleamType::String => "decode.string".to_owned(),
        GleamType::Int => "decode.int".to_owned(),
        GleamType::Float => "decode.float".to_owned(),
        GleamType::Bool => "decode.bool".to_owned(),
        GleamType::List(inner) => format!("decode.list({})", decoder_expr(inner)),
        GleamType::Named { fn_prefix, .. } => format!("{fn_prefix}_decoder()"),
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::{GENERATED_TYPES_HEADER, emit_codecs_module};
    use crate::codegen::model::{
        BoundaryType, EnumDef, EnumVariant, Field, GleamType, RecordDef, TypeDef,
    };

    fn named(type_name: &str) -> GleamType {
        GleamType::Named {
            type_name: type_name.to_owned(),
            fn_prefix: crate::codegen::names::pascal_to_snake(type_name),
        }
    }

    fn field(wire: &str, ty: GleamType, required: bool) -> Field {
        Field {
            wire: wire.to_owned(),
            ty,
            required,
        }
    }

    fn boundary(type_name: &str, defs: Vec<TypeDef>) -> BoundaryType {
        let stem = crate::codegen::names::pascal_to_snake(type_name);
        BoundaryType {
            file: PathBuf::from(format!("schemas/{stem}.json")),
            stem,
            root: named(type_name),
            defs,
        }
    }

    fn record_def(type_name: &str, fields: Vec<Field>) -> TypeDef {
        TypeDef::Record(RecordDef {
            type_name: type_name.to_owned(),
            fn_prefix: crate::codegen::names::pascal_to_snake(type_name),
            fields,
        })
    }

    /// The exact module `full_module_golden_with_enum_list_and_optional`
    /// pins: a record with an enum field, a list, and an optional, plus the
    /// enum itself as its own boundary type.
    const FULL_MODULE_GOLDEN: &str = r#"//// Generated by aion generate — do not edit; regenerate from the project's types module.
////
//// JSON codecs for the `demo` boundary types declared in
//// `src/demo_io.gleam`: per type, a schema-shaped encoder/decoder
//// pair and a typed codec for activity and workflow I/O.

import aion/codec
import gleam/dynamic/decode
import gleam/json
import gleam/list
import gleam/option
import demo_io as io

/// Encodes `io.Event` as schema-shaped JSON; optional fields are
/// omitted when `None`.
pub fn event_to_json(value: io.Event) -> json.Json {
  json.object(
    list.flatten([
      [#("kind", event_kind_to_json(value.kind))],
      [#("tags", json.array(value.tags, json.string))],
      case value.note {
        option.Some(present) -> [#("note", json.string(present))]
        option.None -> []
      },
    ]),
  )
}

/// Decoder for `io.Event` from schema-shaped JSON.
pub fn event_decoder() -> decode.Decoder(io.Event) {
  use field_kind <- decode.field("kind", event_kind_decoder())
  use field_tags <- decode.field("tags", decode.list(decode.string))
  use field_note <- decode.optional_field(
    "note",
    option.None,
    decode.optional(decode.string),
  )
  decode.success(io.Event(
    kind: field_kind,
    tags: field_tags,
    note: field_note,
  ))
}

/// Typed codec for `io.Event` (activity and workflow I/O).
pub fn event_codec() -> codec.Codec(io.Event) {
  codec.json_codec(event_to_json, event_decoder())
}

/// Encodes `io.EventKind` as its wire string.
pub fn event_kind_to_json(value: io.EventKind) -> json.Json {
  case value {
    io.EventKindCreated -> json.string("created")
    io.EventKindClosedOut -> json.string("closed_out")
  }
}

/// Decoder for `io.EventKind` from its wire string.
pub fn event_kind_decoder() -> decode.Decoder(io.EventKind) {
  decode.then(decode.string, fn(raw) {
    case raw {
      "created" -> decode.success(io.EventKindCreated)
      "closed_out" -> decode.success(io.EventKindClosedOut)
      _ -> decode.failure(io.EventKindCreated, "EventKind")
    }
  })
}

/// Typed codec for `io.EventKind` (activity and workflow I/O).
pub fn event_kind_codec() -> codec.Codec(io.EventKind) {
  codec.json_codec(event_kind_to_json, event_kind_decoder())
}
"#;

    #[test]
    fn full_module_golden_with_enum_list_and_optional() {
        let event = boundary(
            "Event",
            vec![
                record_def(
                    "Event",
                    vec![
                        field("kind", named("EventKind"), true),
                        field("tags", GleamType::List(Box::new(GleamType::String)), true),
                        field("note", GleamType::String, false),
                    ],
                ),
                TypeDef::Enum(EnumDef {
                    type_name: "EventKind".to_owned(),
                    fn_prefix: "event_kind".to_owned(),
                    variants: vec![
                        EnumVariant {
                            constructor: "EventKindCreated".to_owned(),
                            wire: "created".to_owned(),
                        },
                        EnumVariant {
                            constructor: "EventKindClosedOut".to_owned(),
                            wire: "closed_out".to_owned(),
                        },
                    ],
                }),
            ],
        );
        let kind = boundary(
            "EventKind",
            vec![TypeDef::Enum(EnumDef {
                type_name: "EventKind".to_owned(),
                fn_prefix: "event_kind".to_owned(),
                variants: vec![
                    EnumVariant {
                        constructor: "EventKindCreated".to_owned(),
                        wire: "created".to_owned(),
                    },
                    EnumVariant {
                        constructor: "EventKindClosedOut".to_owned(),
                        wire: "closed_out".to_owned(),
                    },
                ],
            })],
        );

        assert_eq!(
            emit_codecs_module("demo", &[event, kind]),
            FULL_MODULE_GOLDEN
        );
    }

    #[test]
    fn all_required_records_use_plain_object_lists_without_option_imports() {
        let pair = boundary(
            "Pair",
            vec![record_def(
                "Pair",
                vec![
                    field("count", GleamType::Int, true),
                    field("ratio", GleamType::Float, true),
                    field("flag", GleamType::Bool, true),
                ],
            )],
        );

        let module = emit_codecs_module("demo", &[pair]);
        assert!(module.starts_with(GENERATED_TYPES_HEADER));
        assert!(!module.contains("import gleam/list"));
        assert!(!module.contains("import gleam/option"));
        assert!(module.contains(
            "pub fn pair_to_json(value: io.Pair) -> json.Json {\n  json.object([\n    \
             #(\"count\", json.int(value.count)),\n    \
             #(\"ratio\", json.float(value.ratio)),\n    \
             #(\"flag\", json.bool(value.flag)),\n  ])\n}\n"
        ));
        assert!(module.contains("use field_count <- decode.field(\"count\", decode.int)\n"));
        assert!(module.contains(
            "pub fn pair_codec() -> codec.Codec(io.Pair) {\n  \
             codec.json_codec(pair_to_json, pair_decoder())\n}\n"
        ));
        assert!(!module.contains("list.flatten"));
    }

    #[test]
    fn nested_lists_encode_with_depth_named_lambdas() {
        let grid = boundary(
            "Grid",
            vec![record_def(
                "Grid",
                vec![field(
                    "matrix",
                    GleamType::List(Box::new(GleamType::List(Box::new(GleamType::Int)))),
                    true,
                )],
            )],
        );

        let module = emit_codecs_module("demo", &[grid]);
        assert!(module.contains(
            "#(\"matrix\", json.array(value.matrix, fn(items0) { json.array(items0, json.int) }))"
        ));
        assert!(module.contains(
            "use field_matrix <- decode.field(\"matrix\", decode.list(decode.list(decode.int)))"
        ));
    }

    #[test]
    fn empty_records_emit_bare_constructors() {
        let blank = boundary("Blank", vec![record_def("Blank", Vec::new())]);

        let module = emit_codecs_module("demo", &[blank]);
        assert!(module.contains("json.object([\n  ])"));
        assert!(module.contains("decode.success(io.Blank)\n"));
    }

    #[test]
    fn emission_is_deterministic() {
        let make = || {
            boundary(
                "Event",
                vec![record_def(
                    "Event",
                    vec![
                        field("kind", GleamType::String, true),
                        field("note", GleamType::String, false),
                    ],
                )],
            )
        };
        assert_eq!(
            emit_codecs_module("demo", &[make()]),
            emit_codecs_module("demo", &[make()])
        );
    }
}