Skip to content

Commit

Permalink
fix: from Simon's suggested avoidance of three levels of nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Sep 3, 2024
1 parent a884740 commit 0f2489e
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions server/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,22 @@ impl From<IncomingContext> for Context {
#[serde(rename_all = "camelCase")]
pub struct PostContext {
pub context: Option<Context>,
pub properties: Option<HashMap<String, String>>,
#[serde(flatten)]
pub flattened_context: Option<Context>,
#[serde(flatten)]
pub extra_properties: HashMap<String, String>,
}

impl From<PostContext> for Context {
fn from(input: PostContext) -> Self {
let props = if let Some(context_props) = input.context.and_then(|c| c.properties) {
let mut props = HashMap::new();
props.extend(context_props);
props.extend(input.extra_properties);
props.extend(input.properties.unwrap_or_default());
props
if let Some(context) = input.context {
context
} else {
let mut props = HashMap::new();
props.extend(input.extra_properties);
props.extend(input.properties.unwrap_or_default());
props
};

Context {
properties: Some(props.clone()),
user_id: props.get("userId").cloned(),
session_id: props.get("sessionId").cloned(),
remote_address: props.get("remoteAddress").cloned(),
environment: props.get("environment").cloned(),
app_name: props.get("appName").cloned(),
current_time: props.get("currentTime").cloned(),
IncomingContext {
context: input.flattened_context.unwrap_or_default(),
extra_properties: input.extra_properties,
}
.into()
}
}
}
Expand Down

0 comments on commit 0f2489e

Please sign in to comment.