Unleash / unleash-edge

MIT License
48 stars 8 forks source link

fix: added PostContext handling properties and context as nested variants #510

Closed chriswk closed 1 month ago

github-actions[bot] commented 1 month ago

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

sighphyre commented 1 month ago

This is a very small change in terms of code but it's quite a large and significant change in terms of where Edge goes from this point on and how that impacts the evolution of the SDK ecosytem. I have some thoughts (oh no!).

Some suggestions though. If you're willing to tighten up the new behavior going forward without impacting the old behavior, I think we can do this:

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PostContext {
    pub context: Option<Context>,
    #[serde(flatten)]
    pub properties: Option<Context>,
    #[serde(flatten)]
    pub extra_properties: HashMap<String, String>,
}

impl From<PostContext> for Context {
    fn from(input: PostContext) -> Self {
        if let Some(context) = input.context {
            context
        } else {
            IncomingContext {
                context: input.properties.unwrap_or_default(),
                extra_properties: input.extra_properties,
            }
            .into()
        }
    }
}

This lets us maintain the existing legacy behavior and reuse that code path, while denying that same behavior where we don't need it. It's also lets us use the Rust type system a bit more effectively rather than falling back to a string map. Works with existing tests but I do think we need to cover the weird cases a bit more effectively.

I'd also like to suggest renaming the properties field to something like flat_context or something, just so it's clear what we expect to be contained in that mapping.

Gosh, you write 10 lines and code and suddenly your colleague has 500 lines of opinions. I'm so sorry.

chriswk commented 1 month ago

No need to be sorry here. I think your suggestion makes sense. Let's have a f2f sync on this, I was struggling to come up with other cases here. My one argument is that the one client we know uses this is the proxy-client, which we promise should work with just swapping from a proxy backend to the edge backend.

But I think your suggestion is less invasive than my solution. Let's see if we can get it to work?

sighphyre commented 1 month ago

No need to be sorry here. I think your suggestion makes sense. Let's have a f2f sync on this, I was struggling to come up with other cases here. My one argument is that the one client we know uses this is the proxy-client, which we promise should work with just swapping from a proxy backend to the edge backend.

But I think your suggestion is less invasive than my solution. Let's see if we can get it to work?

Ye! I'm excited!