95 lines
3.1 KiB
Rust
95 lines
3.1 KiB
Rust
use crank_mapping::{MappingRule, MappingSet};
|
|
use crank_schema::{Schema, SchemaKind};
|
|
|
|
use crate::rest::model::RestParameterLocation;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct BodyFieldMapping {
|
|
pub input_name: String,
|
|
pub body_path: String,
|
|
pub required: bool,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct ParameterFieldMapping {
|
|
pub input_name: String,
|
|
pub wire_name: String,
|
|
pub location: RestParameterLocation,
|
|
pub required: bool,
|
|
pub default_value: Option<serde_json::Value>,
|
|
}
|
|
|
|
pub fn input_mapping(
|
|
parameters: &[ParameterFieldMapping],
|
|
body_fields: &[BodyFieldMapping],
|
|
) -> MappingSet {
|
|
let mut rules = Vec::new();
|
|
for parameter in parameters {
|
|
let target_root = match parameter.location {
|
|
RestParameterLocation::Path => "$.request.path",
|
|
RestParameterLocation::Query => "$.request.query",
|
|
RestParameterLocation::Header => "$.request.headers",
|
|
// Cookies travel through a distinct mapping root. Runtime turns
|
|
// this object into the single wire `Cookie` header at the adapter
|
|
// boundary; source values cannot inject an arbitrary header.
|
|
RestParameterLocation::Cookie => "$.request.cookies",
|
|
};
|
|
rules.push(MappingRule {
|
|
source: format!("$.mcp.{}", parameter.input_name),
|
|
target: format!("{target_root}.{}", parameter.wire_name),
|
|
required: parameter.required,
|
|
default_value: parameter.default_value.clone(),
|
|
transform: None,
|
|
condition: None,
|
|
notes: None,
|
|
});
|
|
}
|
|
for field in body_fields {
|
|
let target = if field.body_path == "__content_type" {
|
|
"$.request.body_content_type".to_owned()
|
|
} else if field.body_path.is_empty() || field.body_path == "__body" {
|
|
"$.request.body".to_owned()
|
|
} else {
|
|
format!("$.request.body.{}", field.body_path)
|
|
};
|
|
rules.push(MappingRule {
|
|
source: format!("$.mcp.{}", field.input_name),
|
|
target,
|
|
required: field.required,
|
|
default_value: None,
|
|
transform: None,
|
|
condition: None,
|
|
notes: None,
|
|
});
|
|
}
|
|
MappingSet { rules }
|
|
}
|
|
|
|
pub fn output_mapping(output_schema: &Schema) -> MappingSet {
|
|
let mut rules = Vec::new();
|
|
if output_schema.kind == SchemaKind::Object && !output_schema.fields.is_empty() {
|
|
for (name, field) in &output_schema.fields {
|
|
rules.push(MappingRule {
|
|
source: format!("$.response.body.{name}"),
|
|
target: format!("$.output.{name}"),
|
|
required: field.required,
|
|
default_value: None,
|
|
transform: None,
|
|
condition: None,
|
|
notes: None,
|
|
});
|
|
}
|
|
} else {
|
|
rules.push(MappingRule {
|
|
source: "$.response.body".to_owned(),
|
|
target: "$.output".to_owned(),
|
|
required: true,
|
|
default_value: None,
|
|
transform: None,
|
|
condition: None,
|
|
notes: None,
|
|
});
|
|
}
|
|
MappingSet { rules }
|
|
}
|