openapi-nexus 0.1.3

OpenAPI 3.x multi-language code generator
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! API emission for IR operations (Go APIs).
//!
//! Groups operations by tag, emits one `apis/<tag>.go` in package `apis`. Each
//! file declares a `{Tag}API` struct carrying a `*runtime.Client` and exposes
//! one method per operation. Per-operation `{OperationID}Response` carries
//! `StatusCode`, `Raw *http.Response`, and typed payload fields for successful
//! responses.
//!
//! Non-2xx responses are surfaced as `*runtime.APIError` so callers can switch
//! on error via `errors.As`.
//!
//! File-level structure (package declaration, imports, struct/func declarations)
//! is assembled via sigil-stitch's `FileSpec`, `TypeSpec`, `FunSpec`, and
//! `CodeBlock` builders, giving us automatic import tracking through
//! `TypeName::importable`. Method bodies are emitted as `CodeBlock`s because
//! the imperative Go code (path templating, query building, JSON
//! marshal/unmarshal, status switch) does not fit the structural builders.

use std::collections::{BTreeMap, BTreeSet, HashSet};

use crate::codegen::traits::file_writer::FileInfo;
use crate::ir::types::{
    IrOperation, IrParameter, IrPrimitive, IrRequestBody, IrResponse, IrSpec, IrTypeExpr,
    ParameterLocation,
};
use heck::{ToLowerCamelCase, ToPascalCase, ToSnakeCase};
use sigil_stitch::code_block::CodeBlock;
use sigil_stitch::prelude::sigil_quote;
use sigil_stitch::spec::field_spec::FieldSpec;
use sigil_stitch::spec::file_spec::FileSpec;
use sigil_stitch::spec::fun_spec::FunSpec;
use sigil_stitch::spec::import_spec::ImportSpec;
use sigil_stitch::spec::modifiers::TypeKind;
use sigil_stitch::spec::parameter_spec::ParameterSpec;
use sigil_stitch::spec::type_spec::TypeSpec;
use sigil_stitch::type_name::TypeName;

const APIS_PACKAGE: &str = "apis";
const RENDER_WIDTH: usize = 100;

/// Generate every API file from the IR.
pub fn generate_api_files(
    ir: &IrSpec,
    module_path: &str,
    header: &str,
) -> Result<Vec<FileInfo>, String> {
    let by_tag = group_by_tag(&ir.operations);
    let mut files = Vec::with_capacity(by_tag.len());
    for (tag, ops) in &by_tag {
        let stem = tag.to_snake_case();
        // Avoid the `_test.go` suffix (Go treats those as test files).
        let filename = if stem.ends_with("_test") {
            format!("{stem}_api.go")
        } else {
            format!("{stem}.go")
        };
        let body = emit_api_file(tag, ops, module_path);
        let content = format!("{header}{body}");
        files.push(FileInfo::api(filename, content));
    }
    Ok(files)
}

fn group_by_tag(operations: &[IrOperation]) -> BTreeMap<String, Vec<&IrOperation>> {
    let mut out: BTreeMap<String, Vec<&IrOperation>> = BTreeMap::new();
    for op in operations {
        let tags: Vec<String> = if op.tags.is_empty() {
            vec!["default".to_string()]
        } else {
            op.tags.clone()
        };
        for tag in tags {
            out.entry(tag).or_default().push(op);
        }
    }
    out
}

// ---------------------------------------------------------------------------
// File assembly
// ---------------------------------------------------------------------------

fn emit_api_file(tag: &str, ops: &[&IrOperation], module_path: &str) -> String {
    let struct_name = format!("{}API", tag.to_pascal_case());

    // Pre-plan each operation so we can build specs from the plans.
    let plans: Vec<OpPlan> = ops.iter().map(|op| plan_operation(op)).collect();

    let filename = format!("{}.go", tag.to_snake_case());
    let mut fb = FileSpec::builder(&filename)
        // Package header
        .header(package_header())
        // API struct type
        .add_type(build_api_struct(&struct_name, module_path))
        // Constructor function
        .add_function(build_constructor(&struct_name, module_path));

    // Body-level imports: packages used inside CodeBlock method bodies that
    // sigil can't infer from structural TypeName references.
    for import in collect_body_imports(&plans, module_path) {
        fb = fb.add_import(import);
    }

    // For each operation: response struct + method
    for plan in &plans {
        fb = fb
            .add_type(build_response_struct(plan, module_path))
            .add_function(build_operation_fun(&struct_name, plan));
    }

    let file = fb.build().expect("FileSpec builds for API file");
    file.render(RENDER_WIDTH)
        .expect("FileSpec renders for API file")
}

// ---------------------------------------------------------------------------
// Body-level import collection
// ---------------------------------------------------------------------------

fn collect_body_imports(plans: &[OpPlan<'_>], module_path: &str) -> Vec<ImportSpec> {
    let mut pkgs: BTreeSet<String> = BTreeSet::new();

    for plan in plans {
        let has_path_params = !plan.path_params.is_empty();
        let has_query_params = !plan.query_params.is_empty();
        let has_body = plan.body.is_some();
        let has_typed_responses = !plan.typed_responses.is_empty();

        if has_path_params {
            pkgs.insert("strings".to_string());
        }
        if has_query_params {
            pkgs.insert("net/url".to_string());
        }
        if has_body {
            pkgs.insert("io".to_string());
            pkgs.insert("encoding/json".to_string());
            pkgs.insert("fmt".to_string());
            pkgs.insert("bytes".to_string());
        }
        // io.ReadAll used in error handling (4xx branch)
        pkgs.insert("io".to_string());
        if has_typed_responses {
            pkgs.insert("encoding/json".to_string());
            pkgs.insert("fmt".to_string());
        }

        // Check if any param stringification needs strconv or fmt
        for p in plan
            .path_params
            .iter()
            .chain(&plan.query_params)
            .chain(&plan.header_params)
        {
            collect_stringify_imports(&p.param.type_expr, &mut pkgs);
        }

        // Check if body or any typed response references models
        if let Some(b) = &plan.body
            && b.go_type.contains("models.")
        {
            pkgs.insert(format!("{module_path}/models"));
        }
        for tr in &plan.typed_responses {
            if tr.go_type.contains("models.") {
                pkgs.insert(format!("{module_path}/models"));
            }
        }
    }

    pkgs.into_iter()
        .map(|pkg| {
            let name = pkg.rsplit('/').next().unwrap_or(&pkg);
            ImportSpec::named(&pkg, name)
        })
        .collect()
}

fn collect_stringify_imports(t: &IrTypeExpr, pkgs: &mut BTreeSet<String>) {
    match t {
        IrTypeExpr::Primitive(
            IrPrimitive::String
            | IrPrimitive::Date
            | IrPrimitive::DateTime
            | IrPrimitive::Uuid
            | IrPrimitive::StringWithFormat(_)
            | IrPrimitive::Binary,
        )
        | IrTypeExpr::StringLiteral(_)
        | IrTypeExpr::StringEnum(_)
        | IrTypeExpr::Named(_) => {}
        IrTypeExpr::Primitive(IrPrimitive::Boolean)
        | IrTypeExpr::Primitive(IrPrimitive::Integer)
        | IrTypeExpr::Primitive(IrPrimitive::IntegerWithFormat(_))
        | IrTypeExpr::Primitive(IrPrimitive::Number)
        | IrTypeExpr::Primitive(IrPrimitive::NumberWithFormat(_)) => {
            pkgs.insert("strconv".to_string());
        }
        IrTypeExpr::Nullable(inner) => collect_stringify_imports(inner, pkgs),
        _ => {
            pkgs.insert("fmt".to_string());
        }
    }
}

// ---------------------------------------------------------------------------
// Structural builders
// ---------------------------------------------------------------------------

/// Build a `package apis` header block.
fn package_header() -> CodeBlock {
    sigil_quote!(GoLang {
        package $L(APIS_PACKAGE)
    })
    .expect("package header builds")
}

/// Build the API struct: `type {Name}API struct { client *runtime.Client }`
fn build_api_struct(struct_name: &str, module_path: &str) -> TypeSpec {
    let runtime_client_ty = TypeName::importable(&format!("{module_path}/runtime"), "Client");

    TypeSpec::builder(struct_name, TypeKind::Struct)
        .doc(&format!(
            "{struct_name} groups operations under the corresponding tag."
        ))
        .add_field(
            FieldSpec::builder("client", TypeName::pointer(runtime_client_ty))
                .build()
                .expect("client field builds"),
        )
        .build()
        .expect("API struct TypeSpec builds")
}

/// Build the constructor: `func New{Name}(client *runtime.Client) *{Name} { ... }`
fn build_constructor(struct_name: &str, module_path: &str) -> FunSpec {
    let runtime_client_ty = TypeName::importable(&format!("{module_path}/runtime"), "Client");
    let func_name = format!("New{struct_name}");

    let body = sigil_quote!(GoLang {
        $L(format!("return &{struct_name}{{client: client}}"))
    })
    .expect("constructor body builds");

    FunSpec::builder(&func_name)
        .doc(&format!(
            "New{struct_name} constructs a {struct_name} bound to client."
        ))
        .add_param(
            ParameterSpec::new("client", TypeName::pointer(runtime_client_ty))
                .expect("param builds"),
        )
        .returns(TypeName::pointer(TypeName::primitive(struct_name)))
        .body(body)
        .build()
        .expect("constructor FunSpec builds")
}

/// Build the response struct for an operation.
fn build_response_struct(plan: &OpPlan<'_>, module_path: &str) -> TypeSpec {
    let _ = module_path;

    // Raw *http.Response
    let http_response_ty = TypeName::importable("net/http", "Response");

    let mut tb = TypeSpec::builder(&plan.response_type, TypeKind::Struct)
        .doc(&format!(
            "{} carries the response from the corresponding operation.",
            plan.response_type
        ))
        // StatusCode int
        .add_field(
            FieldSpec::builder("StatusCode", TypeName::primitive("int"))
                .build()
                .expect("StatusCode field builds"),
        )
        // Raw *http.Response
        .add_field(
            FieldSpec::builder("Raw", TypeName::pointer(http_response_ty))
                .build()
                .expect("Raw field builds"),
        );

    // Typed response payload fields
    let mut seen: BTreeSet<String> = BTreeSet::new();
    for tr in &plan.typed_responses {
        if !seen.insert(tr.field_name.clone()) {
            continue;
        }
        let type_name = pointerize_type_name(&tr.go_type);
        tb = tb.add_field(
            FieldSpec::builder(&tr.field_name, type_name)
                .build()
                .expect("response payload field builds"),
        );
    }

    tb.build().expect("response struct TypeSpec builds")
}

/// Build a FunSpec for an operation method.
fn build_operation_fun(struct_name: &str, plan: &OpPlan<'_>) -> FunSpec {
    let OpPlan {
        op,
        method_name,
        response_type,
        ..
    } = plan;

    let mut fb = FunSpec::builder(method_name);

    // Doc comment
    if let Some(summary) = &op.summary {
        fb = fb.doc(&format!("{method_name} \u{2014} {summary}"));
    } else {
        fb = fb.doc(&format!(
            "{method_name} calls {} {}.",
            op.method.to_uppercase(),
            op.path,
        ));
    }
    if let Some(desc) = &op.description {
        fb = fb.doc("");
        for line in desc.lines() {
            fb = fb.doc(line);
        }
    }

    // Receiver: (a *{StructName})
    fb = fb.receiver(
        ParameterSpec::new("a", TypeName::pointer(TypeName::primitive(struct_name)))
            .expect("receiver builds"),
    );

    // Parameters: ctx context.Context, then path/query/header params, then body
    let context_ty = TypeName::importable("context", "Context");
    fb = fb.add_param(ParameterSpec::new("ctx", context_ty).expect("ctx param builds"));

    for p in plan
        .path_params
        .iter()
        .chain(&plan.query_params)
        .chain(&plan.header_params)
    {
        let type_name = TypeName::raw(&p.go_type);
        fb = fb.add_param(ParameterSpec::new(&p.var_name, type_name).expect("param builds"));
    }
    if let Some(body) = &plan.body {
        let type_name = TypeName::raw(&body.go_type);
        fb =
            fb.add_param(ParameterSpec::new(&body.var_name, type_name).expect("body param builds"));
    }

    // Return type: (*{Response}, error)
    fb = fb.returns(TypeName::raw(&format!("(*{response_type}, error)")));

    // Body
    fb = fb.body(emit_method_body(plan));

    fb.build().expect("operation FunSpec builds")
}

// ---------------------------------------------------------------------------
// Planning: resolve parameter names / types so emission is deterministic
// ---------------------------------------------------------------------------

struct OpPlan<'a> {
    op: &'a IrOperation,
    method_name: String,
    response_type: String,
    path_params: Vec<ParamBinding<'a>>,
    query_params: Vec<ParamBinding<'a>>,
    header_params: Vec<ParamBinding<'a>>,
    body: Option<BodyBinding>,
    typed_responses: Vec<TypedResponse>,
}

struct ParamBinding<'a> {
    param: &'a IrParameter,
    var_name: String,
    go_type: String,
    is_pointer: bool,
}

struct BodyBinding {
    var_name: String,
    go_type: String,
}

struct TypedResponse {
    status: String,
    field_name: String,
    go_type: String,
}

fn plan_operation<'a>(op: &'a IrOperation) -> OpPlan<'a> {
    let op_id = sanitize_operation_id(&op.operation_id, &op.method, &op.path);
    let method_name = op_id.to_pascal_case();
    let response_type = format!("{method_name}Response");

    let mut used_names: HashSet<String> = HashSet::new();
    used_names.insert("ctx".to_string());
    used_names.insert("a".to_string());

    let mut path_params = Vec::new();
    let mut query_params = Vec::new();
    let mut header_params = Vec::new();
    for p in &op.parameters {
        let var_name = unique_name(&go_ident(&p.name), &mut used_names);
        let (go_type, is_pointer) = param_go_type(p);
        let binding = ParamBinding {
            param: p,
            var_name,
            go_type,
            is_pointer,
        };
        match p.location {
            ParameterLocation::Path => path_params.push(binding),
            ParameterLocation::Query => query_params.push(binding),
            ParameterLocation::Header => header_params.push(binding),
            ParameterLocation::Cookie => header_params.push(binding),
        }
    }

    let body = op
        .request_body
        .as_ref()
        .and_then(|b| plan_body(b, &mut used_names));

    let typed_responses = op.responses.iter().filter_map(plan_response).collect();

    OpPlan {
        op,
        method_name,
        response_type,
        path_params,
        query_params,
        header_params,
        body,
        typed_responses,
    }
}

fn plan_body(b: &IrRequestBody, used_names: &mut HashSet<String>) -> Option<BodyBinding> {
    let t = pick_body_type(b)?;
    let base_ty = go_type_str(&t);
    let go_type =
        if base_ty.starts_with('[') || base_ty.starts_with("map[") || base_ty.starts_with('*') {
            base_ty
        } else {
            format!("*{base_ty}")
        };
    let var_name = unique_name("body", used_names);
    Some(BodyBinding { var_name, go_type })
}

fn plan_response(r: &IrResponse) -> Option<TypedResponse> {
    let t = pick_response_type(r)?;
    let go_type = go_type_str(&t);
    Some(TypedResponse {
        status: r.status.clone(),
        field_name: response_field_name(&r.status),
        go_type,
    })
}

fn param_go_type(p: &IrParameter) -> (String, bool) {
    let base = go_type_str(&p.type_expr);
    if p.required || base.starts_with('*') || base.starts_with('[') || base.starts_with("map[") {
        let pointer = base.starts_with('*');
        (base, pointer)
    } else {
        (format!("*{base}"), true)
    }
}

fn unique_name(desired: &str, used: &mut HashSet<String>) -> String {
    if used.insert(desired.to_string()) {
        return desired.to_string();
    }
    for i in 2..=u32::MAX {
        let candidate = format!("{desired}{i}");
        if used.insert(candidate.clone()) {
            return candidate;
        }
    }
    unreachable!("name collision space exhausted")
}

// ---------------------------------------------------------------------------
// Method body emission (CodeBlock)
// ---------------------------------------------------------------------------

fn emit_method_body(plan: &OpPlan<'_>) -> CodeBlock {
    let OpPlan {
        op,
        response_type,
        path_params,
        query_params,
        header_params,
        body,
        ..
    } = plan;

    let mut cb = CodeBlock::builder();

    // Path.
    cb.add(&format!("path := \"{}\"", op.path), ());
    cb.add_line();
    for p in path_params {
        let placeholder = format!("{{{}}}", p.param.name);
        let value_expr = deref_if_pointer(&p.var_name, p.is_pointer);
        let stringified = render_value_as_string(&value_expr, &p.param.type_expr);
        cb.add(
            &format!("path = strings.Replace(path, \"{placeholder}\", {stringified}, 1)"),
            (),
        );
        cb.add_line();
    }

    // Query.
    let has_query = !query_params.is_empty();
    if has_query {
        cb.add("query := url.Values{}", ());
        cb.add_line();
        for p in query_params {
            let value_expr = deref_if_pointer(&p.var_name, p.is_pointer);
            let stringified = render_value_as_string(&value_expr, &p.param.type_expr);
            let set_line = format!("query.Set(\"{}\", {stringified})", p.param.name);
            if p.param.required || !p.is_pointer {
                cb.add(&set_line, ());
                cb.add_line();
            } else {
                cb.begin_control_flow(&format!("if {} != nil", p.var_name), ());
                cb.add(&set_line, ());
                cb.add_line();
                cb.end_control_flow();
            }
        }
    }

    // Body.
    if let Some(body) = body {
        cb.add("var bodyReader io.Reader", ());
        cb.add_line();
        cb.begin_control_flow(&format!("if {} != nil", body.var_name), ());
        cb.add(&format!("buf, err := json.Marshal({})", body.var_name), ());
        cb.add_line();
        cb.begin_control_flow("if err != nil", ());
        cb.add("return nil, fmt.Errorf(\"marshal body: %%w\", err)", ());
        cb.add_line();
        cb.end_control_flow();
        cb.add("bodyReader = bytes.NewReader(buf)", ());
        cb.add_line();
        cb.end_control_flow();
    }

    // Build request.
    let query_arg = if has_query { "query" } else { "nil" };
    let body_arg = if body.is_some() { "bodyReader" } else { "nil" };
    cb.add(
        &format!(
            "req, err := a.client.NewRequest(ctx, \"{}\", path, {query_arg}, {body_arg})",
            op.method.to_uppercase(),
        ),
        (),
    );
    cb.add_line();
    cb.begin_control_flow("if err != nil", ());
    cb.add("return nil, err", ());
    cb.add_line();
    cb.end_control_flow();

    // Headers.
    for p in header_params {
        let value_expr = deref_if_pointer(&p.var_name, p.is_pointer);
        let stringified = render_value_as_string(&value_expr, &p.param.type_expr);
        if p.param.required || !p.is_pointer {
            cb.add(
                &format!("req.Header.Set(\"{}\", {stringified})", p.param.name),
                (),
            );
            cb.add_line();
        } else {
            cb.begin_control_flow(&format!("if {} != nil", p.var_name), ());
            cb.add(
                &format!("req.Header.Set(\"{}\", {stringified})", p.param.name),
                (),
            );
            cb.add_line();
            cb.end_control_flow();
        }
    }

    if body.is_some() {
        cb.add("req.Header.Set(\"Content-Type\", \"application/json\")", ());
        cb.add_line();
    }
    cb.add("req.Header.Set(\"Accept\", \"application/json\")", ());
    cb.add_line();

    // Dispatch.
    cb.add("httpResp, err := a.client.Do(req)", ());
    cb.add_line();
    cb.begin_control_flow("if err != nil", ());
    cb.add("return nil, err", ());
    cb.add_line();
    cb.end_control_flow();
    cb.add("defer httpResp.Body.Close()", ());
    cb.add_line();
    cb.add_line();

    cb.add(
        &format!("resp := &{response_type}{{StatusCode: httpResp.StatusCode, Raw: httpResp}}"),
        (),
    );
    cb.add_line();

    if !plan.typed_responses.is_empty() {
        cb.begin_control_flow("switch httpResp.StatusCode", ());
        for tr in &plan.typed_responses {
            let Some(code) = tr.status.parse::<u16>().ok() else {
                continue;
            };
            cb.add(&format!("case {code}:"), ());
            cb.add_line();
            cb.add("%L", emit_decode_into(&tr.field_name, &tr.go_type));
        }
        cb.add("default:", ());
        cb.add_line();
        cb.add("%>", ());
        cb.begin_control_flow("if httpResp.StatusCode >= 400", ());
        cb.add("body, _ := io.ReadAll(httpResp.Body)", ());
        cb.add_line();
        cb.add(
            "return nil, &runtime.APIError{StatusCode: httpResp.StatusCode, Status: httpResp.Status, Body: body}",
            (),
        );
        cb.add_line();
        cb.end_control_flow();
        cb.add("%<", ());
        cb.end_control_flow();
    } else {
        cb.begin_control_flow("if httpResp.StatusCode >= 400", ());
        cb.add("body, _ := io.ReadAll(httpResp.Body)", ());
        cb.add_line();
        cb.add(
            "return nil, &runtime.APIError{StatusCode: httpResp.StatusCode, Status: httpResp.Status, Body: body}",
            (),
        );
        cb.add_line();
        cb.end_control_flow();
    }

    cb.add("return resp, nil", ());
    cb.build().expect("method body builds")
}

fn emit_decode_into(field: &str, go_ty: &str) -> CodeBlock {
    let (elem_ty, assignment) = if go_ty.starts_with('[') || go_ty.starts_with("map[") {
        (go_ty.to_string(), format!("resp.{field} = payload"))
    } else {
        (
            go_ty.trim_start_matches('*').to_string(),
            format!("resp.{field} = &payload"),
        )
    };
    sigil_quote!(GoLang {
        $>
        $L(format!("var payload {elem_ty}"))
        $L("if err := json.NewDecoder(httpResp.Body).Decode(&payload); err != nil") {
            $L("return nil, fmt.Errorf(\"decode response: %w\", err)")
        }
        $L(assignment)
        $<
    })
    .expect("decode body builds")
}

fn deref_if_pointer(var: &str, is_pointer: bool) -> String {
    if is_pointer {
        format!("*{var}")
    } else {
        var.to_string()
    }
}

// ---------------------------------------------------------------------------
// TypeName builder for response struct fields
// ---------------------------------------------------------------------------

fn pointerize_type_name(go_ty: &str) -> TypeName {
    if go_ty.starts_with('[') || go_ty.starts_with('*') || go_ty.starts_with("map[") {
        TypeName::raw(go_ty)
    } else {
        TypeName::raw(&format!("*{go_ty}"))
    }
}

// ---------------------------------------------------------------------------
// Response payload fields
// ---------------------------------------------------------------------------

fn response_field_name(status: &str) -> String {
    if status == "default" {
        "Default".to_string()
    } else if let Ok(code) = status.parse::<u16>() {
        format!("Status{code}")
    } else {
        format!("Status{}", status.to_uppercase())
    }
}

fn pick_response_type(r: &IrResponse) -> Option<IrTypeExpr> {
    r.content
        .get("application/json")
        .cloned()
        .or_else(|| r.content.values().next().cloned())
}

fn pick_body_type(body: &IrRequestBody) -> Option<IrTypeExpr> {
    body.content
        .get("application/json")
        .cloned()
        .or_else(|| body.content.values().next().cloned())
}

// ---------------------------------------------------------------------------
// Param -> Go identifier, value -> Go string expression
// ---------------------------------------------------------------------------

fn go_ident(name: &str) -> String {
    let camel = name.to_lower_camel_case();
    if camel.is_empty() {
        return "arg".to_string();
    }
    match camel.as_str() {
        "type" | "func" | "var" | "const" | "map" | "range" | "select" | "case" | "default"
        | "chan" | "go" | "if" | "else" | "for" | "return" | "break" | "continue" | "switch"
        | "interface" | "struct" | "package" | "import" | "fallthrough" | "goto" | "defer" => {
            format!("{camel}_")
        }
        _ => camel,
    }
}

fn render_value_as_string(value_expr: &str, t: &IrTypeExpr) -> String {
    match t {
        IrTypeExpr::Primitive(
            IrPrimitive::String
            | IrPrimitive::Date
            | IrPrimitive::DateTime
            | IrPrimitive::Uuid
            | IrPrimitive::StringWithFormat(_),
        )
        | IrTypeExpr::StringLiteral(_)
        | IrTypeExpr::StringEnum(_) => value_expr.to_string(),
        IrTypeExpr::Primitive(IrPrimitive::Boolean) => {
            format!("strconv.FormatBool({value_expr})")
        }
        IrTypeExpr::Primitive(IrPrimitive::Integer)
        | IrTypeExpr::Primitive(IrPrimitive::IntegerWithFormat(_)) => {
            format!("strconv.FormatInt(int64({value_expr}), 10)")
        }
        IrTypeExpr::Primitive(IrPrimitive::Number)
        | IrTypeExpr::Primitive(IrPrimitive::NumberWithFormat(_)) => {
            format!("strconv.FormatFloat(float64({value_expr}), 'f', -1, 64)")
        }
        IrTypeExpr::Nullable(inner) => render_value_as_string(value_expr, inner),
        IrTypeExpr::Named(_) => {
            format!("string({value_expr})")
        }
        _ => format!("fmt.Sprintf(\"%%v\", {value_expr})"),
    }
}

// ---------------------------------------------------------------------------
// Type-expr -> Go type
// ---------------------------------------------------------------------------

fn go_type_str(expr: &IrTypeExpr) -> String {
    match expr {
        IrTypeExpr::Named(name) => format!("models.{}", name.to_pascal_case()),
        IrTypeExpr::Primitive(p) => go_primitive(p).to_string(),
        IrTypeExpr::Array(inner) => format!("[]{}", go_type_str(inner)),
        IrTypeExpr::Map(inner) => format!("map[string]{}", go_type_str(inner)),
        IrTypeExpr::Nullable(inner) => format!("*{}", go_type_str(inner)),
        IrTypeExpr::StringLiteral(_) | IrTypeExpr::StringEnum(_) => "string".to_string(),
        IrTypeExpr::Union(_) => "any".to_string(),
        IrTypeExpr::Any => "any".to_string(),
    }
}

fn go_primitive(p: &IrPrimitive) -> &'static str {
    match p {
        IrPrimitive::String
        | IrPrimitive::Date
        | IrPrimitive::DateTime
        | IrPrimitive::Uuid
        | IrPrimitive::StringWithFormat(_) => "string",
        IrPrimitive::Binary => "[]byte",
        IrPrimitive::Integer => "int",
        IrPrimitive::IntegerWithFormat(format) => match format.as_str() {
            "int32" => "int32",
            "int64" => "int64",
            _ => "int",
        },
        IrPrimitive::Number => "float64",
        IrPrimitive::NumberWithFormat(format) => match format.as_str() {
            "float" => "float32",
            _ => "float64",
        },
        IrPrimitive::Boolean => "bool",
    }
}

// ---------------------------------------------------------------------------
// Misc
// ---------------------------------------------------------------------------

fn sanitize_operation_id(op_id: &str, method: &str, path: &str) -> String {
    if !op_id.is_empty() {
        return op_id.to_string();
    }
    let path_part: String = path
        .chars()
        .map(|c| if c.is_alphanumeric() { c } else { '_' })
        .collect();
    format!("{method}_{path_part}")
}