Skip to content

Commit 1bf32a1

Browse files
committed
fix: extract IsJson() check to content_type
Signed-off-by: Alan Kan <[email protected]>
1 parent 8ad2c06 commit 1bf32a1

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

v2/event/content_type.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ const (
1414
ApplicationCloudEventsBatchJSON = "application/cloudevents-batch+json"
1515
)
1616

17+
type ContentType string
18+
19+
// IsJSON returns true if the content type is a JSON type.
20+
func (c ContentType) IsJSON() bool {
21+
switch c {
22+
case ApplicationJSON, TextJSON, ApplicationCloudEventsJSON, ApplicationCloudEventsBatchJSON:
23+
return true
24+
case "":
25+
return true // Empty content type assumes json
26+
default:
27+
return false
28+
}
29+
}
30+
1731
// StringOfApplicationJSON returns a string pointer to "application/json"
1832
func StringOfApplicationJSON() *string {
1933
a := ApplicationJSON

v2/event/content_type_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,27 @@ func TestStringOfApplicationCloudEventsBatchJSON(t *testing.T) {
5757
t.Errorf("unexpected string (-want, +got) = %v", diff)
5858
}
5959
}
60+
61+
func TestContentTypeIsJSON(t *testing.T) {
62+
tests := []struct {
63+
name string
64+
ct event.ContentType
65+
expected bool
66+
}{
67+
{name: "Empty", ct: "", expected: true},
68+
{name: "ApplicationJSON", ct: event.ApplicationJSON, expected: true},
69+
{name: "TextJSON", ct: event.TextJSON, expected: true},
70+
{name: "ApplicationCloudEventsJSON", ct: event.ApplicationCloudEventsJSON, expected: true},
71+
{name: "ApplicationCloudEventsBatchJSON", ct: event.ApplicationCloudEventsBatchJSON, expected: true},
72+
{name: "ApplicationXML", ct: event.ApplicationXML, expected: false},
73+
{name: "TextPlain", ct: event.TextPlain, expected: false},
74+
}
75+
76+
for _, tc := range tests {
77+
t.Run(tc.name, func(t *testing.T) {
78+
if got := tc.ct.IsJSON(); got != tc.expected {
79+
t.Errorf("ContentType.IsJSON() = %v, want %v", got, tc.expected)
80+
}
81+
})
82+
}
83+
}

v2/event/event_marshal.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,8 @@ func WriteJson(in *Event, writer io.Writer) error {
150150
mediaType = strings.TrimSpace(strings.ToLower(contentType[0:i]))
151151
}
152152

153-
jsonMediaTypes := map[string]bool{
154-
"": true,
155-
ApplicationJSON: true,
156-
TextJSON: true,
157-
ApplicationCloudEventsJSON: true,
158-
ApplicationCloudEventsBatchJSON: true,
159-
}
160-
// If isJson and no encoding to base64, we don't need to perform additional steps
161-
if jsonMediaTypes[mediaType] && !isBase64 {
153+
// If IsJSON and no encoding to base64, we don't need to perform additional steps
154+
if ContentType(mediaType).IsJSON() && !isBase64 {
162155
stream.WriteObjectField("data")
163156
_, err := stream.Write(in.DataEncoded)
164157
if err != nil {

v2/event/event_unmarshal.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func consumeDataAsBytes(e *Event, isBase64 bool, b []byte) error {
365365
}
366366

367367
mt, _ := e.Context.GetDataMediaType()
368-
if !isJson(mt) {
368+
if !ContentType(mt).IsJSON() {
369369
// If not json, then data is encoded as string
370370
iter := jsoniter.ParseBytes(jsoniter.ConfigFastest, b)
371371
src := iter.ReadString() // handles escaping
@@ -401,7 +401,7 @@ func consumeData(e *Event, isBase64 bool, iter *jsoniter.Iterator) error {
401401
}
402402

403403
mt, _ := e.Context.GetDataMediaType()
404-
if !isJson(mt) {
404+
if !ContentType(mt).IsJSON() {
405405
// If not json, then data is encoded as string
406406
src := iter.ReadString() // handles escaping
407407
e.DataEncoded = []byte(src)
@@ -415,16 +415,6 @@ func consumeData(e *Event, isBase64 bool, iter *jsoniter.Iterator) error {
415415
return nil
416416
}
417417

418-
func isJson(contentType string) bool {
419-
return map[string]bool{
420-
"": true, // Empty content type assumes json
421-
ApplicationJSON: true,
422-
TextJSON: true,
423-
ApplicationCloudEventsJSON: true,
424-
ApplicationCloudEventsBatchJSON: true,
425-
}[contentType]
426-
}
427-
428418
func readUriRef(iter *jsoniter.Iterator) types.URIRef {
429419
str := iter.ReadString()
430420
uriRef := types.ParseURIRef(str)

0 commit comments

Comments
 (0)