Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions compiler-rs/clients_schema/src/transform/expand_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ type GenericArgs = Vec<ValueOf>;
/// Mapping from generic arguments to values
type GenericMapping = HashMap<TypeName, ValueOf>;

// Special behavior cases
const QUERY_PARAMETERS_BEHAVIORS: [&str;2] = [
"CommonQueryParameters",
"CommonCatQueryParameters"
];

/// Expand all generics by creating new concrete types for every instanciation of a generic type.
///
/// The resulting model has no generics anymore. Top-level generic parameters (e.g. SearchRequest's TDocument) are
Expand Down Expand Up @@ -159,6 +165,10 @@ pub fn expand_generics(model: IndexedModel) -> anyhow::Result<IndexedModel> {
req.inherits = Some(expand_inherits(inherit, &mappings, model, ctx)?);
}

// This handles the specifics for attached_behaviors whom properties
// should be part of query parameters inside the request.
expand_attached_behaviors(&mut req.query, req.attached_behaviors.clone(), model)?;

expand_behaviors(&mut req.behaviors, &mappings, model, ctx)?;
expand_properties(&mut req.path, &mappings, model, ctx)?;
expand_properties(&mut req.query, &mappings, model, ctx)?;
Expand All @@ -167,6 +177,23 @@ pub fn expand_generics(model: IndexedModel) -> anyhow::Result<IndexedModel> {
Ok(req.into())
}

fn expand_attached_behaviors(query: &mut Vec<Property>, behaviors: Vec<String>, model: &IndexedModel) -> anyhow::Result<()> {
for behavior in &behaviors {
if QUERY_PARAMETERS_BEHAVIORS.contains(&behavior.as_str()) {
let new_type_name = TypeName {
namespace: "_spec_utils".into(),
name: behavior.into(),
};
if let Ok(def) = model.get_interface(&new_type_name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using let def model.get_interface(&new_type_name)? avoids to silently ignore a missing definition for the behavior.

for property in def.properties.clone() {
query.push(property);
}
}
}
}
Ok(())
}

fn expand_response(
resp: &Response,
args: GenericArgs,
Expand Down
Binary file modified compiler-rs/compiler-wasm-lib/pkg/compiler_wasm_lib_bg.wasm
Binary file not shown.