olai-codegen 0.0.13

Proto-driven code generation for REST handlers, clients, and resource registries
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
//! Ergonomic resource-scoped client generation.
//!
//! For a resource-scoped service this emits a thin client (e.g. `CatalogClient`) that captures the
//! resource's name components and exposes:
//! - constructors: `new(components…, client)` and — for nested resources — `from_full_name(full_name,
//!   client)`, which splits the dot-joined name into components once and forwards to `new`;
//! - name-inspection accessors: `name(&self) -> &str` for the resource's own leaf, a qualified
//!   `<ancestor>(&self) -> &str` per ancestor component (e.g. `schema.catalog_name()`), and
//!   `full_name(&self) -> String` (the dot-joined components);
//! - instance operations (`get`/`update`/`delete` and resource-targeted custom RPCs), each
//!   returning the matching generated builder with the captured components injected as the path arg;
//! - child-navigation accessors (`catalog.schema(name) -> SchemaClient`), child-create methods
//!   (`catalog.create_schema(name) -> CreateSchemaBuilder`), and child-list methods
//!   (`catalog.list_schemas(…) -> ListSchemasBuilder`, with `.into_stream()` for pagination) for each
//!   direct child resource, reusing the parent's captured components as the filter/path values.
//!
//! Consuming crates don't hand-write scoped clients. The struct (with
//! `pub(crate)` fields) is generated into the consuming crate's source tree alongside the low-level
//! client and builders, so hand-written extension `impl` blocks (pagination streams, bespoke
//! helpers) in that crate compose with it as additional inherent-impl blocks and can read the
//! captured components + low-level client.

use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use super::{MethodHandler, ServiceHandler, doc_tokens, format_tokens};
use crate::analysis::{ChildLink, GenerationPlan, RequestParam, RequestType};
use crate::error::Result;

/// Generate the `resource.rs` module for one resource-scoped service.
///
/// Returns `None` for resource-less services (they have no scoped client — their methods live on
/// the aggregate/root client). `plan` is used to resolve direct-child services for the navigation
/// and create accessors.
pub(crate) fn generate(
    service: &ServiceHandler<'_>,
    plan: &GenerationPlan,
) -> Result<Option<String>> {
    let Some(scoped_ident) = service.scoped_client_type() else {
        return Ok(None);
    };
    let Some(spec) = service.accessor_spec() else {
        return Ok(None);
    };

    // A resource whose scoped methods carry a non-string path parameter (e.g. a model
    // version, addressed by its parent's composite `{full_name}` plus an integer
    // `{version}`) cannot be expressed by the string-component scoped client. Skip it;
    // its methods remain available on the low-level service client.
    if !service.supports_scoped_client() {
        return Ok(None);
    }

    let low_level_ident = service.low_level_client_type(crate::codegen::ClientProtocol::Rest);
    let components: Vec<_> = spec.params.iter().map(|p| format_ident!("{}", p)).collect();
    let join_format = spec.join_format();

    let struct_def = scoped_struct(&components);
    let constructor = scoped_constructor(&components, &low_level_ident);
    // For nested resources, also offer a `from_full_name` constructor that splits a dot-joined
    // full name into the component fields once (no round-trip). Flat resources skip it — it would
    // duplicate `new`.
    let from_full_name = if spec.nested {
        from_full_name_constructor(&components, &low_level_ident, &spec.singular)
    } else {
        quote! {}
    };
    let name_accessors = name_accessors(&components, &join_format);
    let methods = instance_methods(service, &components, &join_format);
    let child_methods = child_methods(service, plan, &components);

    // Child-create methods take parameter types (enums/messages) from each child's own models, so
    // import those models modules in addition to this service's. Deduped, excluding our own path.
    let child_model_imports = child_model_import_paths(service, plan);

    let singular_doc = format!(" A client scoped to a single `{}`.", spec.singular);
    let mod_path = service.models_path();

    let tokens = quote! {
        // The model/builder glob imports below are convenience-wide and not every
        // resource-scoped client references every imported name (more so under buffa,
        // where field shapes differ), so silence unused-import noise for the module.
        #![allow(unused_imports)]
        use #mod_path::*;
        #(#child_model_imports)*
        use super::builders::*;
        use super::client::#low_level_ident;

        #[doc = #singular_doc]
        #[derive(Clone)]
        pub struct #scoped_ident {
            #struct_def
            pub(crate) client: #low_level_ident,
        }

        impl #scoped_ident {
            #constructor
            #from_full_name
            #name_accessors
            #(#methods)*
            #(#child_methods)*
        }
    };

    Ok(Some(format_tokens(tokens)?))
}

/// `use <child models path>::*;` imports for every direct child, deduped and excluding this
/// service's own models path (already imported by the caller).
///
/// Child-create methods reference enum/message parameter types from the *child's* models (e.g.
/// `SchemaClient::create_function` uses `ParameterStyle`/`RoutineBody` from the functions models),
/// so those modules must be in scope. A child whose Create method takes only scalars still gets an
/// (unused) glob import — harmless and simpler than tracking per-type provenance.
fn child_model_import_paths(
    service: &ServiceHandler<'_>,
    plan: &GenerationPlan,
) -> Vec<TokenStream> {
    let own = service.models_path();
    let own_str = quote! { #own }.to_string();

    let mut seen = std::collections::BTreeSet::new();
    let mut imports = Vec::new();
    for (_, child) in direct_children(service, plan) {
        let path = child.models_path();
        let key = quote! { #path }.to_string();
        if key == own_str || !seen.insert(key) {
            continue;
        }
        let cfg = child.client_feature_cfg();
        imports.push(quote! { #cfg use #path::*; });
    }
    imports
}

/// Emit child-navigation and child-create methods for each direct child of this resource.
///
/// `parent_components` are the parent's captured struct fields (in order); by the prefix relation
/// they are exactly the leading components of every child's accessor params.
fn child_methods(
    service: &ServiceHandler<'_>,
    plan: &GenerationPlan,
    parent_components: &[proc_macro2::Ident],
) -> Vec<TokenStream> {
    // Names of the parent's captured components, for matching the child builders' params.
    let parent_names: Vec<String> = parent_components.iter().map(|c| c.to_string()).collect();
    let mut out = Vec::new();
    for (link, child) in direct_children(service, plan) {
        // Child methods name the child's client and builders, so they share its feature gate.
        let cfg = child.client_feature_cfg();
        let methods = [
            child_nav_method(&child, link, parent_components),
            child_builder_method(&child, link, &parent_names, RequestType::Create),
            child_builder_method(&child, link, &parent_names, RequestType::List),
        ];
        out.extend(methods.into_iter().flatten().map(|m| quote! { #cfg #m }));
    }
    out
}

/// Each direct child of `service` with a handler for its plan (children without one are skipped).
fn direct_children<'a>(
    service: &ServiceHandler<'a>,
    plan: &'a GenerationPlan,
) -> impl Iterator<Item = (&'a ChildLink, ServiceHandler<'a>)> {
    service.plan.direct_children.iter().filter_map(move |link| {
        let child_plan = plan
            .services
            .iter()
            .find(|s| s.base_path == link.child_base_path)?;
        Some((link, service.with_plan(child_plan)))
    })
}

/// `pub fn <child>(&self, <leaf>: impl Into<String>) -> crate::codegen::<base>::<Child>Client`.
///
/// Forwards the parent's captured components plus the one new trailing component to the child's
/// scoped-client `new`, building the child's low-level client from this client's cloud client + URL.
fn child_nav_method(
    child: &ServiceHandler<'_>,
    link: &ChildLink,
    parent_components: &[proc_macro2::Ident],
) -> Option<TokenStream> {
    let child_scoped = child.scoped_client_type()?;
    let child_low_level = child.low_level_client_type(crate::codegen::ClientProtocol::Rest);
    let module = format_ident!("{}", link.child_base_path);

    let method_name = format_ident!("{}", link.child_singular);
    // The child's accessor params are parent params + one trailing leaf; the leaf is the new arg.
    let leaf = link.child_accessor_params.last()?;
    let leaf_ident = format_ident!("{}", leaf);

    let parent_args = parent_components.iter().map(|c| quote! { &self.#c });
    let low_level = child_low_level_ctor(&module, &child_low_level);
    let doc = format!(" Access a `{}` within this resource.", link.child_singular);

    Some(quote! {
        #[doc = #doc]
        pub fn #method_name(&self, #leaf_ident: impl Into<String>) -> crate::codegen::#module::#child_scoped {
            crate::codegen::#module::#child_scoped::new(
                #(#parent_args,)*
                #leaf_ident,
                #low_level,
            )
        }
    })
}

/// The child's Create or List builder constructor, or `None` when the child has no such method:
///
/// - `pub fn create_<child>(&self, <args>) -> crate::codegen::<base>::Create<Child>Builder`: the
///   child's own name + other required fields become method arguments.
/// - `pub fn list_<children>(&self, <args>) -> crate::codegen::<base>::List<Child>Builder` (the
///   aggregate's list method name): lists the child resource scoped to this resource. The returned
///   builder has `into_stream()` for paginated iteration.
///
/// The builder's `::new` takes the method's required params in order. The parent's captured
/// components (`parent_names`) pre-fill the matching path or filter params (e.g. `catalog_name`);
/// the rest (e.g. a required `max_results`) become method args, in the builder's `::new` order.
/// When the proto marks pagination fields `optional`, they're `with_*` setters and drop out here.
fn child_builder_method(
    child: &ServiceHandler<'_>,
    link: &ChildLink,
    parent_names: &[String],
    request_type: RequestType,
) -> Option<TokenStream> {
    let method = child
        .methods()
        .find(|m| m.plan.request_type == request_type)?;
    let (method_name, doc) = match request_type {
        RequestType::List => (
            method.binding_method_name(),
            format!(
                " List `{}` resources within this resource.",
                link.child_singular
            ),
        ),
        _ => (
            format_ident!("create_{}", link.child_singular),
            format!(" Create a `{}` within this resource.", link.child_singular),
        ),
    };
    let child_low_level = child.low_level_client_type(crate::codegen::ClientProtocol::Rest);
    let module = format_ident!("{}", link.child_base_path);
    let builder_ty = method.builder_type();

    let required: Vec<&RequestParam> = method.required_parameters().collect();
    let (new_args, method_param_defs) = render_child_builder_args(&method, &required, parent_names);

    let low_level = child_low_level_ctor(&module, &child_low_level);
    Some(quote! {
        #[doc = #doc]
        pub fn #method_name(&self, #(#method_param_defs),*) -> crate::codegen::#module::#builder_ty {
            crate::codegen::#module::#builder_ty::new(
                #low_level,
                #(#new_args),*
            )
        }
    })
}

/// How a child builder (`Create*`/`List*`) argument is supplied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BuilderArg {
    /// Filled from the parent's captured component of the same name (`&self.<name>`).
    ParentComponent,
    /// Supplied by the caller as a method argument.
    MethodArg,
}

/// Classify each of a child builder's required `::new` params (in order) as either a parent-component
/// fill or a caller-supplied method argument.
///
/// A param whose name matches one of the parent's captured component names is a `ParentComponent`
/// (e.g. `catalog_name` on a child Create/List); everything else (the child's own name, pagination
/// knobs, other required fields) is a `MethodArg`. Shared by the child create and list emitters.
fn classify_builder_args(required: &[&RequestParam], parent_names: &[String]) -> Vec<BuilderArg> {
    required
        .iter()
        .map(|p| {
            if parent_names.iter().any(|n| n == p.name()) {
                BuilderArg::ParentComponent
            } else {
                BuilderArg::MethodArg
            }
        })
        .collect()
}

/// `crate::codegen::<module>::<LowLevel>::new(self.client.client.clone(), self.client.base_url.clone())`
/// — the child's low-level client built from this scoped client's cloud client + base URL. Shared by
/// the child nav/create/list emitters.
fn child_low_level_ctor(
    module: &proc_macro2::Ident,
    child_low_level: &proc_macro2::Ident,
) -> TokenStream {
    quote! {
        crate::codegen::#module::#child_low_level::new(
            self.client.client.clone(),
            self.client.base_url.clone(),
        )
    }
}

/// Split a child builder's required `::new` params into the builder's positional `new` args and the
/// caller-facing method parameter definitions, classifying each via [`classify_builder_args`].
///
/// Parent-component params become `&self.<field>`; the rest become `<ident>: <ty>` params (typed via
/// `method.field_type(.., Constructor)`). Returns `(new_args, method_param_defs)`. Shared by the child
/// create and list emitters — they differ only in which `MethodHandler` supplies the types.
fn render_child_builder_args(
    method: &MethodHandler<'_>,
    required: &[&RequestParam],
    parent_names: &[String],
) -> (Vec<TokenStream>, Vec<TokenStream>) {
    let mut new_args: Vec<TokenStream> = Vec::new();
    let mut method_param_defs: Vec<TokenStream> = Vec::new();
    for (param, source) in required
        .iter()
        .zip(classify_builder_args(required, parent_names))
    {
        match source {
            BuilderArg::ParentComponent => {
                let field = param.field_ident();
                new_args.push(quote! { &self.#field });
            }
            BuilderArg::MethodArg => {
                let (def, arg) = method_arg(method, param);
                method_param_defs.push(def);
                new_args.push(arg);
            }
        }
    }
    (new_args, method_param_defs)
}

/// A caller-supplied builder argument: its method parameter definition (`<ident>: <ty>`, typed like
/// the builder's constructor expects) and the forwarded argument.
fn method_arg(method: &MethodHandler<'_>, param: &RequestParam) -> (TokenStream, TokenStream) {
    let ident = param.field_ident();
    let ty = method.field_type(
        param.field_type(),
        crate::parsing::types::RenderContext::Constructor,
    );
    (quote! { #ident: #ty }, quote! { #ident })
}

/// The struct's captured-component fields (each a `String`).
fn scoped_struct(components: &[proc_macro2::Ident]) -> TokenStream {
    // Fields are `pub(crate)` so hand-written extension impls in the consuming crate can read the
    // captured name components (and the low-level `client`) when adding bespoke methods.
    let fields = components.iter().map(|c| quote! { pub(crate) #c: String, });
    quote! { #(#fields)* }
}

/// `pub fn new(<component>: impl Into<String>, …, client: <LowLevel>) -> Self`.
fn scoped_constructor(
    components: &[proc_macro2::Ident],
    low_level_ident: &proc_macro2::Ident,
) -> TokenStream {
    let params = components.iter().map(|c| quote! { #c: impl Into<String> });
    let assigns = components.iter().map(|c| quote! { #c: #c.into() });
    quote! {
        /// Create a client bound to the resource's name components.
        pub fn new(#(#params,)* client: #low_level_ident) -> Self {
            Self {
                #(#assigns,)*
                client,
            }
        }
    }
}

/// `pub fn from_full_name(full_name, client) -> Self` for a nested resource.
///
/// Splits the dot-joined `full_name` into its component fields **once** (`splitn(N, '.')`, so the
/// final component keeps any trailing dots) and forwards to [`Self::new`] — no parse-then-rejoin
/// round-trip. Only emitted for nested resources (`components.len() > 1`); for a flat resource it
/// would duplicate `new`.
fn from_full_name_constructor(
    components: &[proc_macro2::Ident],
    low_level_ident: &proc_macro2::Ident,
    singular: &str,
) -> TokenStream {
    let n = components.len();
    // Bind each component from the split iterator, in order.
    let bindings = components.iter().map(|c| {
        quote! { let #c = parts.next().unwrap_or_default(); }
    });
    let args = components.iter().map(|c| quote! { #c });
    let doc = format!(
        " Create a `{singular}` client from its dot-joined full name (e.g. `\"{}\"`).",
        components
            .iter()
            .map(|c| c.to_string())
            .collect::<Vec<_>>()
            .join(".")
    );
    quote! {
        #[doc = #doc]
        pub fn from_full_name(full_name: impl Into<String>, client: #low_level_ident) -> Self {
            let full_name = full_name.into();
            let mut parts = full_name.splitn(#n, '.');
            #(#bindings)*
            Self::new(#(#args,)* client)
        }
    }
}

/// Name-inspection accessors over the captured components:
/// - the resource's **own** leaf component (always the last) is exposed as `pub fn name(&self) ->
///   &str` — generic, so `catalog.name()` / `schema.name()` rather than the stuttering
///   `catalog.catalog_name()`;
/// - each **ancestor** component keeps its qualified accessor `pub fn <ancestor>(&self) -> &str`
///   (e.g. `schema.catalog_name()`);
/// - `pub fn full_name(&self) -> String` returns the dot-joined components (just the leaf for a flat
///   resource).
///
/// The leaf accessor uses the literal name `name`, which can't collide with an ancestor (ancestors
/// are always `<parent>_name`). If the leaf component is itself literally named `name`, the qualified
/// and generic names coincide — emit it once.
fn name_accessors(components: &[proc_macro2::Ident], join_format: &str) -> TokenStream {
    let Some((leaf, ancestors)) = components.split_last() else {
        return quote! {};
    };

    // Ancestor accessors: qualified by field name (e.g. `catalog_name`).
    let ancestor_accessors = ancestors.iter().map(|c| {
        let doc = format!(" The `{c}` component of this resource's name.");
        quote! {
            #[doc = #doc]
            pub fn #c(&self) -> &str {
                &self.#c
            }
        }
    });

    // Own leaf accessor: exposed generically as `name()`.
    let name_ident = format_ident!("name");
    let leaf_accessor = quote! {
        /// This resource's own name (the leaf component).
        pub fn #name_ident(&self) -> &str {
            &self.#leaf
        }
    };

    // `full_name()`: the dot-joined components (just the leaf for a flat resource). Skip when a
    // component is literally named `full_name` (its own accessor already exposes it).
    let full_name = if components.iter().any(|c| *c == "full_name") {
        quote! {}
    } else if components.len() == 1 {
        quote! {
            /// The fully-qualified name of this resource.
            pub fn full_name(&self) -> String {
                self.#leaf.clone()
            }
        }
    } else {
        let field_refs = components.iter().map(|c| quote! { self.#c });
        quote! {
            /// The fully-qualified name of this resource (its dot-joined name components).
            pub fn full_name(&self) -> String {
                format!(#join_format, #(#field_refs),*)
            }
        }
    };

    quote! {
        #(#ancestor_accessors)*
        #leaf_accessor
        #full_name
    }
}

/// One method per `is_scoped_instance_method()` RPC, returning its builder.
fn instance_methods(
    service: &ServiceHandler<'_>,
    components: &[proc_macro2::Ident],
    join_format: &str,
) -> Vec<TokenStream> {
    service
        .methods()
        .filter(|m| m.is_scoped_instance_method())
        .map(|m| instance_method(&m, components, join_format))
        .collect()
}

/// Emit a single instance method: `pub fn <verb>(&self, <non-path args>) -> <Builder> { … }`.
///
/// The builder's `::new` takes the method's required params in order (path params + required body
/// fields). Path params are filled from the captured components; the remaining required params
/// become arguments of the generated method.
fn instance_method(
    method: &MethodHandler<'_>,
    components: &[proc_macro2::Ident],
    join_format: &str,
) -> TokenStream {
    let doc = doc_tokens(method.plan.metadata.documentation.as_deref());
    let method_name = method.plan.resource_client_method();
    let builder_ty = method.builder_type();

    let required: Vec<&RequestParam> = method.required_parameters().collect();
    let path_param_count = required.iter().filter(|p| p.is_path_param()).count();

    // Build the ordered argument expressions for `<Builder>::new(self.client.clone(), <args>)`, and
    // collect the non-path required params that must become method arguments.
    let mut new_args: Vec<TokenStream> = Vec::new();
    let mut method_param_defs: Vec<TokenStream> = Vec::new();
    for param in &required {
        if param.is_path_param() {
            new_args.push(path_arg_expr(
                param,
                components,
                join_format,
                path_param_count,
            ));
        } else {
            // A required non-path field (e.g. a required body field) becomes a method argument.
            let (def, arg) = method_arg(method, param);
            method_param_defs.push(def);
            new_args.push(arg);
        }
    }

    quote! {
        #doc
        pub fn #method_name(&self, #(#method_param_defs),*) -> #builder_ty {
            #builder_ty::new(self.client.clone(), #(#new_args),*)
        }
    }
}

/// The expression filling a single path parameter from the captured components.
///
/// - **Composite**: a single path param on a multi-component resource is the dot-joined full name
///   (e.g. `format!("{}.{}", self.catalog_name, self.schema_name)`).
/// - **Direct**: otherwise the path param maps to the same-named captured component (`&self.name`).
fn path_arg_expr(
    param: &RequestParam,
    components: &[proc_macro2::Ident],
    join_format: &str,
    path_param_count: usize,
) -> TokenStream {
    if path_param_count == 1 && components.len() > 1 {
        // Composite full name: join all captured components in order.
        let field_refs = components.iter().map(|c| quote! { self.#c });
        quote! { format!(#join_format, #(#field_refs),*) }
    } else {
        // Direct: this path param corresponds to a captured component of the same name. Fall back to
        // the first component if no exact name match (single-component resources name their path
        // param `name` while the component is e.g. `catalog_name`).
        let name = param.name();
        let field = components
            .iter()
            .find(|c| *c == name)
            .cloned()
            .unwrap_or_else(|| components[0].clone());
        quote! { &self.#field }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::{PathParam, QueryParam};
    use crate::parsing::types::{BaseType, UnifiedType};

    fn idents(names: &[&str]) -> Vec<proc_macro2::Ident> {
        names.iter().map(|n| format_ident!("{}", n)).collect()
    }

    fn string_type() -> UnifiedType {
        UnifiedType {
            base_type: BaseType::String,
            is_optional: false,
            is_repeated: false,
        }
    }

    fn path(name: &str) -> RequestParam {
        RequestParam::Path(PathParam {
            name: name.to_string(),
            field_type: string_type(),
            documentation: None,
        })
    }

    fn join_format(n: usize) -> String {
        crate::codegen::dot_format(n)
    }

    /// Flat resource: one component, one path param named `name` (mismatch with the component name)
    /// → falls back to the single captured component by reference.
    #[test]
    fn flat_single_component_direct_ref() {
        let components = idents(&["catalog_name"]);
        let expr = path_arg_expr(&path("name"), &components, &join_format(1), 1);
        assert_eq!(expr.to_string(), quote! { &self.catalog_name }.to_string());
    }

    /// Flat resource where the path param name matches the component → direct ref to that component.
    #[test]
    fn flat_matching_name_direct_ref() {
        let components = idents(&["catalog_name"]);
        let expr = path_arg_expr(&path("catalog_name"), &components, &join_format(1), 1);
        assert_eq!(expr.to_string(), quote! { &self.catalog_name }.to_string());
    }

    /// Nested resource with a single composite path param (e.g. `full_name`) and multiple captured
    /// components → dot-joined `format!`.
    #[test]
    fn nested_single_path_param_joins_components() {
        let components = idents(&["catalog_name", "schema_name"]);
        let expr = path_arg_expr(&path("full_name"), &components, &join_format(2), 1);
        assert_eq!(
            expr.to_string(),
            quote! { format!("{}.{}", self.catalog_name, self.schema_name) }.to_string()
        );
    }

    /// Nested resource whose builder takes separate path params (count > 1) → each path param maps
    /// to its same-named captured component by reference (no join).
    #[test]
    fn nested_separate_path_params_map_by_name() {
        let components = idents(&["catalog_name", "schema_name"]);
        let catalog = path_arg_expr(&path("catalog_name"), &components, &join_format(2), 2);
        let schema = path_arg_expr(&path("schema_name"), &components, &join_format(2), 2);
        assert_eq!(
            catalog.to_string(),
            quote! { &self.catalog_name }.to_string()
        );
        assert_eq!(schema.to_string(), quote! { &self.schema_name }.to_string());
    }

    /// A non-path param is never routed through `path_arg_expr`; this guards the classifier we rely
    /// on (`is_path_param`) so the instance-method split stays correct.
    #[test]
    fn query_param_is_not_a_path_param() {
        let q = RequestParam::Query(QueryParam {
            name: "page_token".to_string(),
            json_name: None,
            field_type: string_type(),
            documentation: None,
            resource_reference: None,
        });
        assert!(!q.is_path_param());
        assert!(path("name").is_path_param());
    }

    // ── child-create arg classification ──────────────────────────────────────────────────────

    /// CreateSchema builder `new(client, name, catalog_name)`: `name` is the child's own (method
    /// arg), `catalog_name` is the parent component (filled from `&self.catalog_name`).
    #[test]
    fn create_schema_args_split_by_parent_components() {
        let required = [path("name"), path("catalog_name")];
        let refs: Vec<&RequestParam> = required.iter().collect();
        let parents = vec!["catalog_name".to_string()];
        assert_eq!(
            classify_builder_args(&refs, &parents),
            vec![BuilderArg::MethodArg, BuilderArg::ParentComponent]
        );
    }

    /// CreateTable builder `new(client, name, schema_name, catalog_name)`: only `name` is a method
    /// arg; both `schema_name` and `catalog_name` are parent components. Order is preserved.
    #[test]
    fn create_table_args_split_preserves_builder_order() {
        let required = [path("name"), path("schema_name"), path("catalog_name")];
        let refs: Vec<&RequestParam> = required.iter().collect();
        let parents = vec!["catalog_name".to_string(), "schema_name".to_string()];
        assert_eq!(
            classify_builder_args(&refs, &parents),
            vec![
                BuilderArg::MethodArg,
                BuilderArg::ParentComponent,
                BuilderArg::ParentComponent,
            ]
        );
    }

    /// Extra required non-name fields (e.g. `table_type`) stay method args.
    #[test]
    fn create_args_extra_required_fields_are_method_args() {
        let required = [path("name"), path("catalog_name"), path("table_type")];
        let refs: Vec<&RequestParam> = required.iter().collect();
        let parents = vec!["catalog_name".to_string()];
        assert_eq!(
            classify_builder_args(&refs, &parents),
            vec![
                BuilderArg::MethodArg,
                BuilderArg::ParentComponent,
                BuilderArg::MethodArg,
            ]
        );
    }

    /// ListSchemas builder `new(client, catalog_name, max_results, page_token)` (fixtures: pagination
    /// is required): `catalog_name` is the parent filter (filled from `&self.catalog_name`); the
    /// pagination knobs become method args. Order is preserved.
    #[test]
    fn list_args_parent_filter_then_pagination() {
        let required = [
            path("catalog_name"),
            path("max_results"),
            path("page_token"),
        ];
        let refs: Vec<&RequestParam> = required.iter().collect();
        let parents = vec!["catalog_name".to_string()];
        assert_eq!(
            classify_builder_args(&refs, &parents),
            vec![
                BuilderArg::ParentComponent,
                BuilderArg::MethodArg,
                BuilderArg::MethodArg,
            ]
        );
    }

    /// Nested list (e.g. tables under a schema): both `catalog_name` and `schema_name` are parent
    /// filters, each filled from `self`.
    #[test]
    fn list_args_multi_parent_filters() {
        let required = [
            path("catalog_name"),
            path("schema_name"),
            path("max_results"),
        ];
        let refs: Vec<&RequestParam> = required.iter().collect();
        let parents = vec!["catalog_name".to_string(), "schema_name".to_string()];
        assert_eq!(
            classify_builder_args(&refs, &parents),
            vec![
                BuilderArg::ParentComponent,
                BuilderArg::ParentComponent,
                BuilderArg::MethodArg,
            ]
        );
    }

    // ── from_full_name constructor ───────────────────────────────────────────────────────────

    /// A two-component resource splits the full name with `splitn(2, '.')` and forwards the parts to
    /// `new` in order.
    #[test]
    fn from_full_name_splits_into_components() {
        let components = idents(&["catalog_name", "schema_name"]);
        let low = format_ident!("SchemaServiceClient");
        let out = from_full_name_constructor(&components, &low, "schema").to_string();

        assert!(out.contains("splitn (2usize"), "expected splitn(2): {out}");
        assert!(out.contains("let catalog_name = parts . next ()"), "{out}");
        assert!(out.contains("let schema_name = parts . next ()"), "{out}");
        assert!(
            out.contains("Self :: new (catalog_name , schema_name , client)"),
            "forwards parts in order to new: {out}"
        );
    }

    /// A three-component resource splits with `splitn(3, '.')`.
    #[test]
    fn from_full_name_three_components() {
        let components = idents(&["catalog_name", "schema_name", "table_name"]);
        let low = format_ident!("TableServiceClient");
        let out = from_full_name_constructor(&components, &low, "table").to_string();
        assert!(out.contains("splitn (3usize"), "expected splitn(3): {out}");
        assert!(
            out.contains("Self :: new (catalog_name , schema_name , table_name , client)"),
            "{out}"
        );
    }

    // ── child model imports ──────────────────────────────────────────────────────────────────

    use crate::analysis::{ChildLink, GenerationPlan, ServicePlan};
    use crate::codegen::{CodeGenConfig, CodeGenOutput, ServiceHandler};
    use crate::parsing::CodeGenMetadata;

    fn test_config() -> CodeGenConfig {
        CodeGenConfig {
            context_type_path: "crate::Context".into(),
            result_type_path: "crate::Result".into(),
            models_path_template: "common::models::{service}::v1".into(),
            models_path_crate_template: "crate::models::{service}::v1".into(),
            resource_store_crate_name: "olai_store".into(),
            runtime: crate::Runtime::Prost,
            transport_type_path: crate::DEFAULT_TRANSPORT_TYPE_PATH.into(),
            dual_transport: false,
            client_protocols: crate::ClientProtocols::default(),
            connect_client_path: None,
            rest_client: Default::default(),
            output: CodeGenOutput {
                common: Some("/tmp/c".into()),
                models: None,
                models_subdir: "_gen".into(),
                server: None,
                client: None,
                python: None,
                node: None,
                node_ts: None,
                wasm: None,
                generate_resource_clients: true,
            },
            generate_resource_enum: false,
            generate_store_integration: false,
            error_type_path: None,
            generate_object_conversions: false,
            bindings: None,
        }
    }

    /// A resource-scoped `ServicePlan` with the given service name (→ base_path/package) and accessor
    /// params, plus one direct child described by `children`.
    fn svc(service_name: &str, package: &str, children: Vec<ChildLink>) -> ServicePlan {
        use crate::analysis::ManagedResource;
        use crate::google::api::ResourceDescriptor;
        let base_path = crate::utils::strings::service_to_base_path(service_name);
        ServicePlan {
            service_name: service_name.to_string(),
            handler_name: crate::utils::strings::service_to_handler_name(service_name),
            base_path,
            package: package.to_string(),
            methods: vec![],
            managed_resources: vec![ManagedResource {
                type_name: "X".to_string(),
                descriptor: ResourceDescriptor::default(),
            }],
            documentation: None,
            hierarchy: vec![],
            resource_accessor_params: Some(vec!["name".to_string()]),
            direct_children: children,
        }
    }

    /// The child models module is imported, the parent's own path is not re-imported, and the
    /// import list is deduped.
    #[test]
    fn child_model_imports_pull_in_child_models() {
        let child_link = ChildLink {
            child_singular: "schema".to_string(),
            child_base_path: "schema".to_string(),
            child_accessor_params: vec!["name".to_string(), "schema_name".to_string()],
        };
        let plan = GenerationPlan {
            services: vec![
                svc("CatalogService", "example.catalog.v1", vec![child_link]),
                svc("SchemaService", "example.schema.v1", vec![]),
            ],
            skipped_methods: vec![],
        };
        let metadata = CodeGenMetadata::default();
        let config = test_config();
        let parent = ServiceHandler::new(&plan.services[0], &metadata, &config);

        let imports: Vec<String> = child_model_import_paths(&parent, &plan)
            .iter()
            .map(|t| t.to_string())
            .collect();

        // Exactly one import: the child's (schema) models module. The parent's own (catalog) path
        // is excluded.
        assert_eq!(
            imports.len(),
            1,
            "expected one child import, got {imports:?}"
        );
        assert!(
            imports[0].contains("schema"),
            "child schema models import missing: {imports:?}"
        );
        assert!(
            !imports[0].contains("catalog"),
            "parent's own models path should not be re-imported: {imports:?}"
        );
    }

    // ── name-inspection accessors ────────────────────────────────────────────────────────────

    /// Flat resource: its single (leaf) component is exposed as the generic `name()`, not the
    /// stuttering `catalog_name()`; `full_name()` clones the leaf field.
    #[test]
    fn name_accessors_flat_uses_generic_name() {
        let out = name_accessors(&idents(&["catalog_name"]), &join_format(1)).to_string();
        assert!(
            out.contains("pub fn name (& self) -> & str { & self . catalog_name }"),
            "{out}"
        );
        // No stuttering `catalog_name()` accessor.
        assert!(!out.contains("pub fn catalog_name"), "{out}");
        assert!(
            out.contains("pub fn full_name (& self) -> String { self . catalog_name . clone () }"),
            "{out}"
        );
    }

    /// Nested resource: ancestors keep qualified accessors; the own leaf is `name()`; `full_name()`
    /// dot-joins all components.
    #[test]
    fn name_accessors_nested_ancestors_qualified_leaf_generic() {
        let out =
            name_accessors(&idents(&["catalog_name", "schema_name"]), &join_format(2)).to_string();
        // Ancestor stays qualified.
        assert!(
            out.contains("pub fn catalog_name (& self) -> & str"),
            "{out}"
        );
        // Own leaf is the generic `name()` reading the schema_name field, not `schema_name()`.
        assert!(
            out.contains("pub fn name (& self) -> & str { & self . schema_name }"),
            "{out}"
        );
        assert!(!out.contains("pub fn schema_name"), "{out}");
        assert!(
            out.contains("format ! (\"{}.{}\" , self . catalog_name , self . schema_name)"),
            "{out}"
        );
    }

    /// A leaf component literally named `full_name` is exposed as `name()`; no synthesized
    /// `full_name()` String method is emitted (would duplicate / mismatch the return type).
    #[test]
    fn name_accessors_skip_synth_full_name_on_collision() {
        let out = name_accessors(&idents(&["full_name"]), &join_format(1)).to_string();
        assert!(
            out.contains("pub fn name (& self) -> & str { & self . full_name }"),
            "{out}"
        );
        assert_eq!(out.matches("fn full_name").count(), 0, "{out}");
    }
}