Skip to content

feat: Add support for MsgPack #118

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

Merged
merged 3 commits into from
Jul 4, 2025
Merged

Conversation

hotrungnhan
Copy link
Contributor

@hotrungnhan hotrungnhan commented Jun 20, 2025

Summary by CodeRabbit

  • New Features
    • Introduced new type definitions for MsgPack encoding and decoding functionality.
  • Tests
    • Added unit tests to verify MsgPack encoding and decoding operations.
  • Chores
    • Added MIME type mapping for the "msgpack" file extension.

@hotrungnhan hotrungnhan requested a review from a team as a code owner June 20, 2025 04:16
@hotrungnhan hotrungnhan requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team June 20, 2025 04:16
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @hotrungnhan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces full support for MsgPack, a binary serialization format, into the project. This includes adding the necessary external library, registering its MIME type for HTTP handling, defining clear utility types for marshaling and unmarshaling, and providing robust unit tests to validate the implementation.

Highlights

  • MsgPack Dependency: I've added the github.com/shamaton/msgpack/v2 library as a new dependency in go.mod to enable MsgPack serialization and deserialization.
  • MIME Type Registration: The http.go file has been updated to include application/msgpack in the mimeExtensions map, ensuring proper content type handling for MsgPack data.
  • MsgPack Utility Types: A new file, msgpack.go, introduces MsgPackMarshal and MsgPackUnmarshal function types. These types provide clear interfaces for encoding and decoding data using MsgPack.
  • Unit Tests for MsgPack: Comprehensive unit tests have been added in msgpack_test.go to verify the correct functionality of both MsgPack encoding and decoding processes, ensuring the new integration works as expected.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for MessagePack (MsgPack) by adding the github.com/shamaton/msgpack/v2 dependency, defining MsgPackMarshal and MsgPackUnmarshal function types in a new msgpack.go file, and registering the application/msgpack MIME type in http.go. The changes are generally well-implemented and clear.

One critical issue was identified in msgpack_test.go: a mismatch between the defined struct name (sampleStructure2) and the struct name used in the test functions (sampleStructure) will lead to a compilation error. This needs to be corrected for the tests to pass and validate the MsgPack integration.

@gaby
Copy link
Member

gaby commented Jun 20, 2025

We already support msgp via https://github.com/gofiber/fiber/blob/3b2af61152d0dc9bb75bd9451e506274266b638e/go.mod#L12 using the most used library for golang.

The one added on this PR is barely used by projects

@hotrungnhan
Copy link
Contributor Author

We already support msgp via https://github.com/gofiber/fiber/blob/3b2af61152d0dc9bb75bd9451e506274266b638e/go.mod#L12 using the most used library for golang.

The one added on this PR is barely used by projects

ah okay, i'll closed this PR.

@gaby
Copy link
Member

gaby commented Jun 20, 2025

We do need this, but lets us discuss internally which library to use.

@gaby gaby reopened this Jun 20, 2025
@hotrungnhan
Copy link
Contributor Author

hotrungnhan commented Jun 20, 2025

We do need this, but lets us discuss internally which library to use.

My goal is to improve the performance of JSON serialization in our microservices. When comparing Protobuf and Msgpack, I found that Msgpack is noticeably faster than JSON, though still slower than Protobuf. However, that trade-off is acceptable — Msgpack offers a good balance of speed and convenience, as it can serve as a drop-in replacement with minimal changes required.

I chose shamaton/msgpack because its API is quite similar to the standard JSON API, making it easier to adopt. Its performance is also solid, although i currently lack of omit empty, LOL. I’ve benchmarked several libraries, as shown below:

image

I didn’t have time to include tinylib/msgp in my own benchmarks, but Serializer Benchmark shows that it also performs well. While tinylib/msgp is around 3x faster than shamaton/msgpack (reflect version), it requires code generation for the models. In contrast, shamaton/msgpack can be used as a drop-in replacement for JSON without extra steps.

I've also implemented support for Binding, Autoformat, and Msgpack Encoder API in this fork:
https://github.com/hotrungnhan/fiber

Note: I still need to update the utils dependency to include this change before opening a PR.

Copy link
Contributor

coderabbitai bot commented Jun 20, 2025

Walkthrough

The changes update the MIME type for the "msgpack" file extension in the HTTP utility, introduce new function types for MsgPack marshaling and unmarshaling in a utility package, and add unit tests for MsgPack encoding and decoding using an external library.

Changes

File(s) Change Summary
http.go Added MIME type mapping for "msgpack" as "application/vnd.msgpack" with updated comment.
utils/msgpack.go Added MsgPackMarshal and MsgPackUnmarshal function type declarations with comments.
utils/msgpack_test.go Added unit tests for MsgPack encoding and decoding using sample structs and testify assertions.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Function
    participant Struct as sampleStructure
    participant MsgPack as MsgPack Encoder/Decoder

    Test->>Struct: Create instance with data
    Test->>MsgPack: Marshal Struct to bytes
    MsgPack-->>Test: Return marshaled bytes
    Test->>MsgPack: Unmarshal bytes into new Struct
    MsgPack-->>Test: Return decoded Struct
    Test->>Test: Assert original and decoded data are equal
Loading

Poem

🐇
A hop and a skip, MIME types align,
MsgPack gets new types—oh, how divine!
With tests that ensure our bytes decode right,
Marshaled and unmarshaled, all through the night.
In the warren of code, we leap with delight!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 23bf122 and f438ee2.

📒 Files selected for processing (2)
  • http.go (2 hunks)
  • msgpack_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • http.go
  • msgpack_test.go
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
msgpack_test.go (1)

10-10: Fix struct name to match test usage.

The struct is defined as sampleStructure2 but the tests reference sampleStructure. This will cause compilation errors.

-type sampleStructure2 struct {
+type sampleStructure struct {
🧹 Nitpick comments (1)
http.go (1)

156-156: Fix inconsistency between comment and implementation.

The comment mentions "application/msgpack" but the actual MIME type used in the code is "application/vnd.msgpack" (line 191). The code is correct according to IANA standards, but the comment should match the implementation.

-// - Use "application/msgpack" instead of "application/x-msgpack"
+// - Use "application/vnd.msgpack" instead of "application/x-msgpack"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf0ca76 and 017c267.

⛔ Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (3)
  • http.go (2 hunks)
  • msgpack.go (1 hunks)
  • msgpack_test.go (1 hunks)
🔇 Additional comments (4)
msgpack.go (1)

1-9: LGTM! Well-designed function type definitions.

The function types follow Go conventions and provide a clean abstraction for MsgPack operations. The documentation clearly describes the expected behavior, including error handling for invalid unmarshal targets.

http.go (1)

191-191: LGTM! Correct MIME type according to IANA standards.

The update to use "application/vnd.msgpack" aligns with the official IANA media type registration.

msgpack_test.go (2)

14-31: LGTM! Well-structured test for MsgPack encoding.

The test properly validates both marshaling and unmarshaling operations with appropriate error checking and assertions.


33-51: LGTM! Comprehensive test for MsgPack decoding.

The test follows the same solid pattern as the encoder test, validating the complete encode-decode cycle with proper error handling.

@gaby
Copy link
Member

gaby commented Jun 20, 2025

@hotrungnhan Problem is that shamaton/msgpack is barely used by projects. We can't rely on a new library.

Our options are:

Ideally the interface is generic like JSON/XML, and we use tinylib/msgp as default.

@hotrungnhan
Copy link
Contributor Author

hotrungnhan commented Jun 22, 2025

@hotrungnhan Problem is that shamaton/msgpack is barely used by projects. We can't rely on a new library.

Our options are:

Ideally the interface is generic like JSON/XML, and we use tinylib/msgp as default.

It generates code and works differently from JSON. Do you have any suggestions on how we could integrate it?

@hotrungnhan
Copy link
Contributor Author

@gaby @ReneWerner87 do i still need change the lib ?

@ReneWerner87
Copy link
Member

good question, for me it would be ok
since the user can change the lib on his own an use it via the provided functions

@ReneWerner87
Copy link
Member

@gaby WDYT ?

@gaby
Copy link
Member

gaby commented Jul 4, 2025

@gaby WDYT ?

LGTM

Copy link

codecov bot commented Jul 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.03%. Comparing base (cf0ca76) to head (f438ee2).
Report is 13 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #118   +/-   ##
=======================================
  Coverage   96.03%   96.03%           
=======================================
  Files           9        9           
  Lines         505      505           
=======================================
  Hits          485      485           
  Misses         14       14           
  Partials        6        6           
Flag Coverage Δ
unittests 96.03% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby gaby changed the title feat: Support MsgPack feat: Add support for MsgPack Jul 4, 2025
@hotrungnhan
Copy link
Contributor Author

Shall we merge guy ?

@ReneWerner87 ReneWerner87 merged commit dad0ed9 into gofiber:master Jul 4, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants