From 48a85316a4dbfee4998613dd03ae4c6c3274fec0 Mon Sep 17 00:00:00 2001 From: Harry Barber <106155934+hlbarber@users.noreply.github.com> Date: Thu, 11 Aug 2022 23:56:27 +0100 Subject: [PATCH] Make OperationExtension hold a &'static str for the absolute shape ID (#1631) --- .../ServerOperationHandlerGenerator.kt | 5 +-- .../aws-smithy-http-server/src/extension.rs | 40 +++++++++++++------ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationHandlerGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationHandlerGenerator.kt index 3998eab98..33f040b65 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationHandlerGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationHandlerGenerator.kt @@ -115,9 +115,8 @@ open class ServerOperationHandlerGenerator( $callImpl let output_wrapper: $outputWrapperName = output_inner.into(); let mut response = output_wrapper.into_response(); - response.extensions_mut().insert( - #{SmithyHttpServer}::extension::OperationExtension::new("${operation.id.namespace}", "$operationName") - ); + let operation_ext = #{SmithyHttpServer}::extension::OperationExtension::new("${operation.id.namespace}##$operationName").expect("malformed absolute shape ID"); + response.extensions_mut().insert(operation_ext); response.map(#{SmithyHttpServer}::body::boxed) } """, diff --git a/rust-runtime/aws-smithy-http-server/src/extension.rs b/rust-runtime/aws-smithy-http-server/src/extension.rs index 8dcb1ad58..d221d4a99 100644 --- a/rust-runtime/aws-smithy-http-server/src/extension.rs +++ b/rust-runtime/aws-smithy-http-server/src/extension.rs @@ -50,6 +50,8 @@ use std::ops::Deref; +use thiserror::Error; + use crate::request::RequestParts; /// Extension type used to store information about Smithy operations in HTTP responses. @@ -58,19 +60,31 @@ use crate::request::RequestParts; /// request fails to deserialize into the modeled operation input. #[derive(Debug, Clone)] pub struct OperationExtension { - /// Smithy model namespace. + absolute: &'static str, + namespace: &'static str, - /// Smithy operation name. - operation_name: &'static str, + name: &'static str, +} + +/// An error occurred when parsing an absolute operation shape ID. +#[derive(Debug, Clone, Error)] +#[non_exhaustive] +pub enum ParseError { + #[error("# was not found - missing namespace")] + MissingNamespace, } impl OperationExtension { - /// Creates a new `OperationExtension`. - pub fn new(namespace: &'static str, operation_name: &'static str) -> Self { - Self { + /// Creates a new [`OperationExtension`] from the absolute shape ID of the operation. + pub fn new(absolute_operation_id: &'static str) -> Result { + let (namespace, name) = absolute_operation_id + .split_once('#') + .ok_or(ParseError::MissingNamespace)?; + Ok(Self { + absolute: absolute_operation_id, namespace, - operation_name, - } + name, + }) } /// Returns the Smithy model namespace. @@ -79,13 +93,13 @@ impl OperationExtension { } /// Returns the Smithy operation name. - pub fn operation_name(&self) -> &'static str { - self.operation_name + pub fn name(&self) -> &'static str { + self.name } - /// Returns the current operation formatted as `#`. - pub fn operation(&self) -> String { - format!("{}#{}", self.namespace, self.operation_name) + /// Returns the absolute operation shape ID. + pub fn absolute(&self) -> &'static str { + self.absolute } } -- GitLab