Skip to content

feat: Encode function can optimize byte array handling#42

Merged
appleboy merged 1 commit into
gin-contrib:masterfrom
KScaesar:master
Dec 31, 2024
Merged

feat: Encode function can optimize byte array handling#42
appleboy merged 1 commit into
gin-contrib:masterfrom
KScaesar:master

Conversation

@KScaesar

@KScaesar KScaesar commented Sep 5, 2024

Copy link
Copy Markdown

In scenarios where byte array JSON messages are forwarded (e.g., from MQ), the previous approach required unmarshalling into an object before encoding for SSE.

The updated logic allows []byte to be passed directly, simplifying message forwarding and reducing unnecessary conversions.

// before
byteData := messageFromMQ()
data := make(map[string]any)
json.Unmarshal(byteData, &data)
sse.Encode(new(bytes.Buffer), sse.Event{
	Data: data,
})

// after
byteData := messageFromMQ()
sse.Encode(new(bytes.Buffer), sse.Event{
	Data: byteData,
})

…rwarding

In scenarios where byte array JSON messages are forwarded (e.g., from MQ), the previous approach required unmarshalling into an object before encoding for SSE.

The updated logic allows []byte to be passed directly, simplifying message forwarding and reducing unnecessary conversions.

	// before
	byteData := messageFromMQ()
	data := make(map[string]any)
	json.Unmarshal(byteData, &data)
	sse.Encode(new(bytes.Buffer), sse.Event{
		Data: data,
	})

	// after
	byteData := messageFromMQ()
	sse.Encode(new(bytes.Buffer), sse.Event{
		Data: byteData,
	})
@appleboy appleboy merged commit 3f42f2e into gin-contrib:master Dec 31, 2024
@appleboy

Copy link
Copy Markdown
Member

@KScaesar Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants