Skip to content

Commit bb544e0

Browse files
committed
fix: simplify isJSON
Signed-off-by: Alan Kan <[email protected]>
1 parent 1bf32a1 commit bb544e0

File tree

4 files changed

+6
-32
lines changed

4 files changed

+6
-32
lines changed

v2/event/content_type.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ 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 {
17+
// isJSON returns true if the content type is a JSON type.
18+
func isJSON(contentType string) bool {
19+
switch contentType {
2220
case ApplicationJSON, TextJSON, ApplicationCloudEventsJSON, ApplicationCloudEventsBatchJSON:
2321
return true
2422
case "":

v2/event/content_type_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,3 @@ 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func WriteJson(in *Event, writer io.Writer) error {
151151
}
152152

153153
// If IsJSON and no encoding to base64, we don't need to perform additional steps
154-
if ContentType(mediaType).IsJSON() && !isBase64 {
154+
if isJSON(mediaType) && !isBase64 {
155155
stream.WriteObjectField("data")
156156
_, err := stream.Write(in.DataEncoded)
157157
if err != nil {

v2/event/event_unmarshal.go

Lines changed: 2 additions & 2 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 !ContentType(mt).IsJSON() {
368+
if !isJSON(mt) {
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 !ContentType(mt).IsJSON() {
404+
if !isJSON(mt) {
405405
// If not json, then data is encoded as string
406406
src := iter.ReadString() // handles escaping
407407
e.DataEncoded = []byte(src)

0 commit comments

Comments
 (0)