-
Notifications
You must be signed in to change notification settings - Fork 0
Add data_base64 support and simplify CE JSON marshaling #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,8 +2,14 @@ | |||||||||||||||||||||||||
| package cloudevent | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||
| "encoding/base64" | ||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||
| "mime" | ||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| "github.com/tidwall/sjson" | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||
|
|
@@ -95,10 +101,90 @@ type CloudEvent[A any] struct { | |||||||||||||||||||||||||
| CloudEventHeader | ||||||||||||||||||||||||||
| // Data contains domain-specific information about the event. | ||||||||||||||||||||||||||
| Data A `json:"data"` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| DataBase64 string `json:"data_base64,omitempty"` | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // RawEvent is a cloudevent with a json.RawMessage data field. | ||||||||||||||||||||||||||
| type RawEvent = CloudEvent[json.RawMessage] | ||||||||||||||||||||||||||
| // It supports both "data" and "data_base64" (CloudEvents JSON spec). | ||||||||||||||||||||||||||
| type RawEvent struct { | ||||||||||||||||||||||||||
| CloudEventHeader | ||||||||||||||||||||||||||
| Data json.RawMessage `json:"data,omitempty"` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // DataBase64 is the raw "data_base64" string when the event was received with | ||||||||||||||||||||||||||
| // data_base64 (CloudEvents spec). When set, MarshalJSON emits data_base64 for | ||||||||||||||||||||||||||
| // round-trip; otherwise wire form is chosen from DataContentType and Data. | ||||||||||||||||||||||||||
|
Comment on lines
+115
to
+116
|
||||||||||||||||||||||||||
| // data_base64 (CloudEvents spec). When set, MarshalJSON emits data_base64 for | |
| // round-trip; otherwise wire form is chosen from DataContentType and Data. | |
| // the data_base64 field per the CloudEvents JSON Event Format (for example, | |
| // CloudEvents v1.0.2). When set, MarshalJSON emits data_base64 for round-trip; | |
| // otherwise wire form is chosen from DataContentType and Data. |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The json tags on the Data and DataBase64 fields include omitempty, but these tags have no effect because RawEvent has a custom MarshalJSON method that explicitly controls which field to emit. Consider removing the json tags entirely or changing them to json:"-" to make it clear that these fields are not directly marshaled by the standard JSON encoder.
| Data json.RawMessage `json:"data,omitempty"` | |
| // DataBase64 is the raw "data_base64" string when the event was received with | |
| // data_base64 (CloudEvents spec). When set, MarshalJSON emits data_base64 for | |
| // round-trip; otherwise wire form is chosen from DataContentType and Data. | |
| DataBase64 string `json:"data_base64,omitempty"` | |
| Data json.RawMessage `json:"-"` | |
| // DataBase64 is the raw "data_base64" string when the event was received with | |
| // data_base64 (CloudEvents spec). When set, MarshalJSON emits data_base64 for | |
| // round-trip; otherwise wire form is chosen from DataContentType and Data. | |
| DataBase64 string `json:"-"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The json tag on the DataBase64 field includes omitempty, but this tag has no effect because CloudEvent[A] has a custom MarshalJSON method that explicitly controls which field to emit (data vs data_base64). Consider removing the json tag entirely or changing it to json:"-" to make it clear that this field is not directly marshaled by the standard JSON encoder, preventing potential confusion for future maintainers.