aion-package 0.9.1

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
//! Emission of the generated wire-compat golden (remote tiers only).
//!
//! For each value type a remote activity carries, this derives a gleeunit test
//! pinning the type's encoded wire shape:
//!
//! ```gleam
//! codecs.<prefix>_to_json(<canonical sample>) |> json.to_string |> should.equal("<literal>")
//! ```
//!
//! Both the canonical Gleam sample value and the expected compact-JSON literal
//! are *derived* from the same boundary-type model the codecs come from
//! (checklist C3) — no hand-written literal — by walking the record/enum
//! definitions: required scalars take their zero value (`""`, `0`, `0.0`,
//! `false`), lists take the empty list, nested records and enums recurse, and
//! an optional field takes `option.None`, which the encoder omits from the
//! wire, so it never appears in the literal. The literal is exactly what
//! `json.to_string` produces for that value (compact, keys in field order),
//! so the test is a true fixed point: it passes the moment the codecs and the
//! literal agree and fails the instant a wire shape moves.

use std::collections::HashSet;
use std::fmt::Write as _;
use std::path::Path;

use super::activity_model::{ResolvedActivity, ResolvedType};
use super::activity_wrappers::GENERATED_ACTIVITY_HEADER;
use super::error::CodegenError;
use super::model::{EnumDef, GleamType, RecordDef, TypeDef};

/// Emits `test/<pkg>_wire_compat_test.gleam`: one gleeunit test per distinct
/// value type the remote `activities` carry (input before output, declaration
/// order, deduplicated by type name), each pinning the type's encoded wire
/// shape against a literal derived from its boundary type.
///
/// # Errors
///
/// Returns [`CodegenError::GoldenTypeUnresolved`] if a value type or a nested
/// field references a named type absent from its boundary type's definitions —
/// a generator invariant violation, never bad author input, since the
/// interface front-end collects every referenced definition into the closure.
pub(crate) fn emit(
    package_name: &str,
    activities: &[&ResolvedActivity],
) -> Result<String, CodegenError> {
    let mut tests: Vec<(String, String, String)> = Vec::new();
    let mut used_option = false;
    for value_type in distinct_value_types(activities) {
        let (value, literal) = value_type_sample(value_type, &mut used_option)?;
        tests.push((value_type.fn_prefix.clone(), value, literal));
    }

    let mut out = String::new();
    let _ = writeln!(out, "{GENERATED_ACTIVITY_HEADER}");
    out.push_str("////\n");
    let _ = writeln!(
        out,
        "//// Wire-compat goldens for the `{package_name}` remote activities: each test pins"
    );
    out.push_str(
        "//// a value type's encoded wire shape against a literal derived from its type.\n",
    );
    out.push('\n');
    let _ = writeln!(out, "import {package_name}_codecs as codecs");
    let _ = writeln!(out, "import {package_name}_io as io");
    out.push_str("import gleam/json\n");
    if used_option {
        out.push_str("import gleam/option\n");
    }
    out.push_str("import gleeunit/should\n");

    for (prefix, value, literal) in &tests {
        let _ = write!(
            out,
            "\npub fn {prefix}_wire_test() {{\n  codecs.{prefix}_to_json({value})\n  \
             |> json.to_string\n  |> should.equal(\"{}\")\n}}\n",
            gleam_escape(literal)
        );
    }
    Ok(out)
}

/// Collects the value types the remote activities carry, in deterministic order
/// — input before output, declaration order, first occurrence wins — so a
/// type shared across activities is pinned exactly once.
fn distinct_value_types<'a>(activities: &[&'a ResolvedActivity<'a>]) -> Vec<&'a ResolvedType<'a>> {
    let mut seen: HashSet<&str> = HashSet::new();
    let mut types: Vec<&ResolvedType> = Vec::new();
    for activity in activities {
        for value_type in [&activity.input, &activity.output] {
            if seen.insert(value_type.gleam_type.as_str()) {
                types.push(value_type);
            }
        }
    }
    types
}

/// Derives the canonical sample value and its wire literal for one value type.
fn value_type_sample(
    value_type: &ResolvedType,
    used_option: &mut bool,
) -> Result<(String, String), CodegenError> {
    named_sample(
        &value_type.gleam_type,
        &value_type.boundary.defs,
        &value_type.gleam_type,
        value_type.boundary.file.as_path(),
        used_option,
    )
}

/// Resolves a named type to its definition and derives its sample.
fn named_sample(
    type_name: &str,
    defs: &[TypeDef],
    root_type: &str,
    file: &Path,
    used_option: &mut bool,
) -> Result<(String, String), CodegenError> {
    let def = defs
        .iter()
        .find(|def| def.type_name() == type_name)
        .ok_or_else(|| CodegenError::GoldenTypeUnresolved {
            root_type: root_type.to_owned(),
            missing: type_name.to_owned(),
            file: file.to_path_buf(),
        })?;
    match def {
        TypeDef::Record(record) => record_sample(record, defs, root_type, file, used_option),
        TypeDef::Enum(definition) => enum_sample(definition, root_type, file),
    }
}

/// Derives a record's sample: every required field, in order; optional fields
/// take `option.None` and are omitted from the literal, matching the encoder.
fn record_sample(
    record: &RecordDef,
    defs: &[TypeDef],
    root_type: &str,
    file: &Path,
    used_option: &mut bool,
) -> Result<(String, String), CodegenError> {
    if record.fields.is_empty() {
        return Ok((format!("io.{}", record.type_name), "{}".to_owned()));
    }
    let mut value_fields: Vec<String> = Vec::with_capacity(record.fields.len());
    let mut literal_fields: Vec<String> = Vec::new();
    for field in &record.fields {
        if field.required {
            let (value, literal) = type_sample(&field.ty, defs, root_type, file, used_option)?;
            value_fields.push(format!("{}: {value}", field.wire));
            literal_fields.push(format!("\"{}\":{literal}", field.wire));
        } else {
            *used_option = true;
            value_fields.push(format!("{}: option.None", field.wire));
        }
    }
    Ok((
        format!("io.{}({})", record.type_name, value_fields.join(", ")),
        format!("{{{}}}", literal_fields.join(",")),
    ))
}

/// Derives an enum's sample: the first variant's constructor and wire string.
fn enum_sample(
    definition: &EnumDef,
    root_type: &str,
    file: &Path,
) -> Result<(String, String), CodegenError> {
    let first = definition
        .variants
        .first()
        .ok_or_else(|| CodegenError::GoldenTypeUnresolved {
            root_type: root_type.to_owned(),
            missing: format!("{} (enum has no variants)", definition.type_name),
            file: file.to_path_buf(),
        })?;
    Ok((
        format!("io.{}", first.constructor),
        format!("\"{}\"", first.wire),
    ))
}

/// Derives the sample value and literal for a field type.
fn type_sample(
    ty: &GleamType,
    defs: &[TypeDef],
    root_type: &str,
    file: &Path,
    used_option: &mut bool,
) -> Result<(String, String), CodegenError> {
    Ok(match ty {
        GleamType::String => ("\"\"".to_owned(), "\"\"".to_owned()),
        GleamType::Int => ("0".to_owned(), "0".to_owned()),
        GleamType::Float => ("0.0".to_owned(), "0.0".to_owned()),
        GleamType::Bool => ("False".to_owned(), "false".to_owned()),
        GleamType::List(_) => ("[]".to_owned(), "[]".to_owned()),
        GleamType::Named { type_name, .. } => {
            return named_sample(type_name, defs, root_type, file, used_option);
        }
    })
}

/// Escapes a JSON literal for embedding inside a Gleam double-quoted string.
fn gleam_escape(literal: &str) -> String {
    literal.replace('\\', "\\\\").replace('"', "\\\"")
}

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

    use super::emit;
    use crate::codegen::activity_model::{ResolvedActivity, ResolvedType};
    use crate::codegen::declaration::{ActivityDeclaration, Tier};
    use crate::codegen::error::CodegenError;
    use crate::codegen::model::{
        BoundaryType, EnumDef, EnumVariant, Field, GleamType, RecordDef, TypeDef,
    };

    /// `OrderInputLine` -> `order_input_line`, matching the interface
    /// front-end's derived function prefixes. Delegates to the canonical
    /// helper so the test and production share one PascalCase→snake
    /// conversion.
    fn snake(pascal: &str) -> String {
        crate::codegen::names::pascal_to_snake(pascal)
    }

    fn named(type_name: &str) -> GleamType {
        GleamType::Named {
            type_name: type_name.to_owned(),
            fn_prefix: snake(type_name),
        }
    }

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

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

    fn boundary(type_name: &str, defs: Vec<TypeDef>) -> BoundaryType {
        BoundaryType {
            file: PathBuf::from(format!("schemas/{}.json", snake(type_name))),
            stem: snake(type_name),
            root: named(type_name),
            defs,
        }
    }

    fn declaration(name: &str, input: &str, output: &str) -> ActivityDeclaration {
        ActivityDeclaration {
            name: name.to_owned(),
            tier: Tier::RemotePython,
            input_type: input.to_owned(),
            output_type: output.to_owned(),
        }
    }

    fn resolved<'a>(
        declaration: &'a ActivityDeclaration,
        input: &'a BoundaryType,
        output: &'a BoundaryType,
    ) -> ResolvedActivity<'a> {
        ResolvedActivity {
            declaration,
            input: ResolvedType {
                gleam_type: declaration.input_type.clone(),
                fn_prefix: snake(&declaration.input_type),
                boundary: input,
            },
            output: ResolvedType {
                gleam_type: declaration.output_type.clone(),
                fn_prefix: snake(&declaration.output_type),
                boundary: output,
            },
        }
    }

    #[test]
    fn all_required_scalars_pin_to_a_zero_value_literal() -> Result<(), Box<dyn std::error::Error>>
    {
        let order = boundary(
            "OrderInput",
            vec![record_def(
                "OrderInput",
                vec![
                    field("order_id", GleamType::String, true),
                    field("quantity", GleamType::Int, true),
                    field("amount", GleamType::Float, true),
                    field("rush", GleamType::Bool, true),
                ],
            )],
        );
        let receipt = boundary(
            "Receipt",
            vec![record_def(
                "Receipt",
                vec![field("id", GleamType::String, true)],
            )],
        );
        let charge = declaration("charge", "OrderInput", "Receipt");
        let activities = [resolved(&charge, &order, &receipt)];
        let refs: Vec<&ResolvedActivity> = activities.iter().collect();

        let module = emit("order_saga", &refs)?;

        assert!(module.starts_with(super::GENERATED_ACTIVITY_HEADER));
        assert!(module.contains("import order_saga_codecs as codecs\n"));
        assert!(module.contains("import order_saga_io as io\n"));
        assert!(module.contains("import gleam/json\n"));
        assert!(module.contains("import gleeunit/should\n"));
        // No optional field anywhere → no gleam/option import.
        assert!(!module.contains("import gleam/option"));
        assert!(module.contains(
            "pub fn order_input_wire_test() {\n  \
             codecs.order_input_to_json(io.OrderInput(order_id: \"\", quantity: 0, amount: 0.0, rush: False))\n  \
             |> json.to_string\n  \
             |> should.equal(\"{\\\"order_id\\\":\\\"\\\",\\\"quantity\\\":0,\\\"amount\\\":0.0,\\\"rush\\\":false}\")\n}\n"
        ));
        // Output type pinned too.
        assert!(module.contains("pub fn receipt_wire_test() {"));
        Ok(())
    }

    #[test]
    fn optionals_lists_nested_records_and_enums_are_derived()
    -> Result<(), Box<dyn std::error::Error>> {
        // OrderInput { order_id: String (req), tags: List(String) (req),
        //   line: OrderInputLine (req, nested record),
        //   kind: OrderInputKind (req, enum), note: String (optional) }
        let order = boundary(
            "OrderInput",
            vec![
                record_def(
                    "OrderInput",
                    vec![
                        field("order_id", GleamType::String, true),
                        field("tags", GleamType::List(Box::new(GleamType::String)), true),
                        field("line", named("OrderInputLine"), true),
                        field("kind", named("OrderInputKind"), true),
                        field("note", GleamType::String, false),
                    ],
                ),
                record_def(
                    "OrderInputLine",
                    vec![field("sku", GleamType::String, true)],
                ),
                TypeDef::Enum(EnumDef {
                    type_name: "OrderInputKind".to_owned(),
                    fn_prefix: "order_input_kind".to_owned(),
                    variants: vec![
                        EnumVariant {
                            constructor: "OrderInputKindStandard".to_owned(),
                            wire: "standard".to_owned(),
                        },
                        EnumVariant {
                            constructor: "OrderInputKindRush".to_owned(),
                            wire: "rush".to_owned(),
                        },
                    ],
                }),
            ],
        );
        let ok = boundary(
            "Ok",
            vec![record_def("Ok", vec![field("done", GleamType::Bool, true)])],
        );
        let place = declaration("place", "OrderInput", "Ok");
        let activities = [resolved(&place, &order, &ok)];
        let refs: Vec<&ResolvedActivity> = activities.iter().collect();

        let module = emit("demo", &refs)?;

        // Optional field present somewhere → gleam/option imported.
        assert!(module.contains("import gleam/option\n"));
        // Nested record recurses to io.OrderInputLine(...); enum takes the
        // first variant; list is empty; optional `note` is None and omitted
        // from the literal.
        assert!(module.contains(
            "codecs.order_input_to_json(io.OrderInput(order_id: \"\", tags: [], \
             line: io.OrderInputLine(sku: \"\"), kind: io.OrderInputKindStandard, \
             note: option.None))"
        ));
        assert!(module.contains(
            "|> should.equal(\"{\\\"order_id\\\":\\\"\\\",\\\"tags\\\":[],\\\"line\\\":{\\\"sku\\\":\\\"\\\"},\\\"kind\\\":\\\"standard\\\"}\")"
        ));
        Ok(())
    }

    #[test]
    fn unresolved_named_type_is_an_internal_error() -> Result<(), Box<dyn std::error::Error>> {
        // A field references a named type with no matching def — a generator
        // invariant violation that must surface, not panic.
        let broken = boundary(
            "OrderInput",
            vec![record_def(
                "OrderInput",
                vec![field("line", named("MissingLine"), true)],
            )],
        );
        let out = boundary("Ok", vec![record_def("Ok", vec![])]);
        let place = declaration("place", "OrderInput", "Ok");
        let activities = [resolved(&place, &broken, &out)];
        let refs: Vec<&ResolvedActivity> = activities.iter().collect();

        let result = emit("demo", &refs);
        let Err(CodegenError::GoldenTypeUnresolved { missing, .. }) = result else {
            return Err(format!("expected GoldenTypeUnresolved, got {result:?}").into());
        };
        assert_eq!(missing, "MissingLine");
        Ok(())
    }
}