Skip to content

Commit

Permalink
Implement MessagePackEncoder and MessagePackDecoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumoboy007 committed Sep 14, 2023
0 parents commit 141bd61
Show file tree
Hide file tree
Showing 87 changed files with 9,390 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

on: [push]

# TODO: Add Windows job after Swift is added to the Windows images [1] or after
# `swift-actions/setup-swift` supports Swift 5.8+ on Windows [2].
# 1. https://github.com/actions/runner-images/issues/8281
# 2. https://github.com/swift-actions/setup-swift/pull/470#issuecomment-1718406382
jobs:
test-macos:
name: Run Tests on macOS
# TODO: Use `macos-latest` after the macOS 13 image graduates to GA.
# https://github.com/actions/runner-images/issues/7508#issuecomment-1718206371
runs-on: macos-13

steps:
- name: Install msgpack-c
run: |
brew update
brew install msgpack
- name: Print Swift compiler version
run: "swift --version"
- uses: actions/checkout@v3
- name: Run tests
run: "swift test --configuration release -Xswiftc -cross-module-optimization"

test-linux:
name: Run Tests on Linux
runs-on: ubuntu-latest

steps:
- name: Install msgpack-c
run: |
sudo apt-get update
sudo apt-get install libmsgpack-dev
- name: Print Swift compiler version
run: "swift --version"
- uses: actions/checkout@v3
- name: Run tests
run: "swift test --configuration release -Xswiftc -cross-module-optimization"
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
116 changes: 116 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/msgpack-swift.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MessagePack"
BuildableName = "MessagePack"
BlueprintName = "MessagePack"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Benchmarks"
BuildableName = "Benchmarks"
BlueprintName = "Benchmarks"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MessagePackTests"
BuildableName = "MessagePackTests"
BlueprintName = "MessagePackTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Benchmarks"
BuildableName = "Benchmarks"
BlueprintName = "Benchmarks"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MessagePackTests"
BuildableName = "MessagePackTests"
BlueprintName = "MessagePackTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Debug"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MessagePack"
BuildableName = "MessagePack"
BlueprintName = "MessagePack"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MIT License

Copyright © 2023 Darren Mo.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"pins" : [
{
"identity" : "messagepacker",
"kind" : "remoteSourceControl",
"location" : "https://github.com/hirotakan/MessagePacker.git",
"state" : {
"revision" : "4d8346c6bc579347e4df0429493760691c5aeca2",
"version" : "0.4.7"
}
},
{
"identity" : "messagepackreferenceimplementation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/fumoboy007/MessagePackReferenceImplementation.git",
"state" : {
"revision" : "4e12ca378131671a0d1d89b0e1c7b622164a6c99",
"version" : "1.0.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version" : "1.5.3"
}
}
],
"version" : 2
}
48 changes: 48 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// swift-tools-version: 5.8

import PackageDescription

let package = Package(
name: "msgpack-swift",
platforms: [
.macOS(.v13),
.macCatalyst(.v16),
.iOS(.v16),
.tvOS(.v16),
.watchOS(.v9),
],
products: [
.library(
name: "MessagePack",
targets: [
"MessagePack",
]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"),
.package(url: "https://github.com/fumoboy007/MessagePackReferenceImplementation.git", from: "1.0.0"),
.package(url: "https://github.com/hirotakan/MessagePacker.git", from: "0.4.7"),
],
targets: [
.target(
name: "MessagePack"
),

.testTarget(
name: "MessagePackTests",
dependencies: [
"MessagePack",
"MessagePackReferenceImplementation",
]
),
.testTarget(
name: "Benchmarks",
dependencies: [
.product(name: "Logging", package: "swift-log"),
"MessagePack",
"MessagePacker",
]
)
]
)
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# msgpack-swift

An efficient, full-featured, and compliant [MessagePack](https://msgpack.org) implementation for Swift.

![Swift 5.8](https://img.shields.io/badge/Swift-5.8-rgb(240%2C%2081%2C%2055))
![Linux, macOS 13, iOS 16, tvOS 16, watchOS 9](https://img.shields.io/badge/Platforms-Linux%20%7C%20macOS%2013%20%7C%20iOS%2016%20%7C%20tvOS%2016%20%7C%20watchOS%209-blue)
![MIT License](https://img.shields.io/github/license/fumoboy007/msgpack-swift)
![Automated Tests Workflow Status](https://img.shields.io/github/actions/workflow/status/fumoboy007/msgpack-swift/tests.yml)

## Features

- Full integration with Swift’s `Codable` serialization system.
- Support for standard `Foundation` value types such as `Date`, `URL`, `Data`, and `Decimal`.
- Support for MessagePack extension types like timestamp and application-specific extension types.
- Automated tests verify compliance with the MessagePack [specification](https://github.com/msgpack/msgpack/blob/8aa09e2a6a9180a49fc62ecfefe149f063cc5e4b/spec.md) by testing against the `msgpack-c` [reference implementation](https://github.com/fumoboy007/MessagePackReferenceImplementation).
- Highly optimized for performance.

## Compared to the Competition

(As of September 2023.)

| Library | Swift Integration | MessagePack Compliance | Performance\* |
| --- | --- | --- | --- |
| [`hirotakan/MessagePacker`](https://github.com/hirotakan/MessagePacker) | ⚠️ [Does not support](https://github.com/hirotakan/MessagePacker/pull/54) complex nested container setups. | ⚠️ [Missing](https://github.com/hirotakan/MessagePacker/pull/57) some validation logic. | ❌ Up to 2× slower. |
| [`swiftstack/messagepack`](https://github.com/swiftstack/messagepack) | ❌ Timestamp type is not `Codable`. |||
| [`a2/MessagePack.swift`](https://github.com/a2/MessagePack.swift) | ❌ Does not support `Codable`. |||
| [`michael-yuji/YSMessagePack`](https://github.com/michael-yuji/YSMessagePack) | ❌ Does not support `Codable`. |||
| [`briandw/SwiftPack`](https://github.com/briandw/SwiftPack) | ❌ Does not have a Swift package manifest. |||

\* Tested using real-world messages that are involved in high throughput or low latency use cases. Pull requests to [`Benchmarks.swift`](Tests/Benchmarks/Benchmarks.swift) are welcome if you know of similar use cases!

## Usage

Below is a basic example.

```swift
import MessagePack

struct MyMessage: Codable {
let myBool: Bool
let myOptionalDecimal: Decimal?
let myStringArray: [String]
let myTimestamp: MessagePackTimestamp
}
let myMessage = MyMessage(
myBool: true,
myOptionalDecimal: nil,
myStringArray: ["hello", "world"],
myTimestamp: MessagePackTimestamp(secondsComponent: 1694398799,
nanosecondsComponent: 123)
)

let encoder = MessagePackEncoder()
let myMessageBytes = try encoder.encode(myMessage)

let decoder = MessagePackDecoder()
let myMessageDecoded = try decoder.decode(MyMessage.self, from: myMessageBytes)
```
23 changes: 23 additions & 0 deletions Sources/MessagePack/CodableAsMessagePackExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// MIT License
//
// Copyright © 2023 Darren Mo.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

public typealias CodableAsMessagePackExtension = EncodableAsMessagePackExtension & DecodableAsMessagePackExtension
Loading

0 comments on commit 141bd61

Please sign in to comment.