cloudevents / sdk-go

Go SDK for CloudEvents
https://cloudevents.github.io/sdk-go/
Apache License 2.0
837 stars 223 forks source link

Support structured syntax suffixes for custom content types #990

Closed dan-j closed 7 months ago

dan-j commented 11 months ago

We have data coming into our system which can have dynamic content types, and we attempt to handle it regardless of the underlying format. For example we have an application which is feeding us data using a vendor-specific content type, i.e. application/vnd.my-app+json, and we'd like to pass this into the cloudevents SDK.

The problem is that the current codec implementation expects an exact match on the content type in order to load up the Encoder/Decoder, see https://github.com/cloudevents/sdk-go/blob/main/v2/event/datacodec/codec.go

We would like to support the standard set of IANA Structured Syntax Suffixes, so if the codec receives a custom content type, but has a +json, +xml suffix etc., then we use the correct codec.

Happy for one of our team to contribute a PR, just thought I'd open it up for discussion first.

embano1 commented 11 months ago

That's an interesting one. @duglin is the SPEC strict in those cases or does it give us flexibility to implement what @dan-j is asking for?

Perhaps alternatively, we could allow for custom encoders/decoders in the SDK e.g., by overwriting the default(s) or not fail on unknown codecs if a user registered a corresponding handler function?

duglin commented 11 months ago

@dan-j couple of questions for ya...

dan-j commented 10 months ago

Sorry @duglin, missed the notification on this.

These are not CloudEvent content types. What I want to do is put the received data into a CloudEvent and have it correctly marshal/unmarshal based on the provided content type.

For example, if I make a request to GitHub's API with the header Accept: application/vnd.github+json, I will get Content-Type: application/vnd.github+json in the response. I want to then create a CloudEvent like so:

{
  // start: core attributes
  // end: core attributes
  "datacontenttype": "application/vnd.github+json",
  "data": {
    // the GitHub JSON response
  }
}

And I want to be able to use the CloudEvents SDK to event.SetData("application/vnd.github+json", response) and then subsequently event.DataAs(obj) and it correctly use the JSON encoder/decoder. If the content type is +xml, then use the XML encoder/decoder etc.

GitHub is just an example, we have numerous APIs being invoked which use IANA Structured Syntax Suffixes in the content-type responses. We don't necessarily have prior knowledge of what the specific content types are, but if they contain a +json or +xml suffix I believe the cloudevents SDK should handle it.

duglin commented 10 months ago

@dan-j What do you plan on doing with the CloudEvent golang struct once it's created?

dan-j commented 10 months ago

No structs, everything is dynamic, so it's just an any, and our application handles if it results in being a map[string]any, string, bool etc.

duglin commented 10 months ago

@dan-j and I chatted off-line (and he was very patient with my questions...) and I think I understand it better now and it sounds reasonable to me!