-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f556099
commit 8b02e7b
Showing
13 changed files
with
198 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Snippets/Advanced Use Cases/ApplicationSpecificMessagePackExtensionTypes.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Implement an application-specific MessagePack extension type by conforming a type to ``CodableAsMessagePackExtension``. | ||
|
||
// snippet.hide | ||
|
||
import MessagePack | ||
|
||
// snippet.show | ||
|
||
struct MyExtensionType: CodableAsMessagePackExtension { | ||
static var extensionTypeID: Int8 { | ||
// Return some integer in the range `0...127`. | ||
// snippet.hide | ||
return 0 | ||
// snippet.show | ||
} | ||
|
||
// MARK: Serialization | ||
|
||
func validateForEncoding() throws { | ||
// Throw an error if the value is not able to be encoded. | ||
} | ||
|
||
var encodedByteCount: UInt32 { | ||
// Return the number of bytes that `encode(to:)` will write. | ||
// snippet.hide | ||
return 0 | ||
// snippet.show | ||
} | ||
|
||
func encode(to messageWriter: inout MessageWriter) { | ||
// Encode the value and use `messageWriter` to append the bytes to the message. | ||
} | ||
|
||
// MARK: Deserialization | ||
|
||
init(decoding bytes: UnsafeRawBufferPointer) throws { | ||
// Decode the value from the given bytes. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Use ``MessagePackDecoder`` to deserialize `Codable` values from MessagePack bytes. | ||
|
||
// snippet.hide | ||
|
||
import Foundation | ||
import MessagePack | ||
|
||
struct MyMessage: Decodable { | ||
} | ||
|
||
let mySerializedMessage = Data() | ||
|
||
// snippet.show | ||
|
||
let decoder = MessagePackDecoder() | ||
let message = try decoder.decode(MyMessage.self, from: mySerializedMessage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// The ``MessagePackTimestamp`` structure represents the MessagePack timestamp extension type. | ||
|
||
// snippet.hide | ||
|
||
import Foundation | ||
import MessagePack | ||
|
||
// snippet.show | ||
|
||
// snippet.codable | ||
|
||
struct MyMessage: Codable { | ||
let myTimestamp: MessagePackTimestamp | ||
} | ||
|
||
// snippet.init-with-components | ||
|
||
var timestamp = MessagePackTimestamp(secondsComponent: 1719102009, | ||
nanosecondsComponent: 781666897) | ||
|
||
// snippet.init-with-rfc3339 | ||
|
||
timestamp = MessagePackTimestamp(internetDateTime: "2023-09-10T19:19:59.123456789-07:00")! | ||
|
||
// snippet.init-with-date | ||
|
||
timestamp = MessagePackTimestamp(Date.now) | ||
|
||
// snippet.date-from-timestamp | ||
|
||
let date = Date(timestamp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Use ``MessagePackEncoder`` to serialize `Codable` values to MessagePack bytes. | ||
|
||
// snippet.hide | ||
|
||
import MessagePack | ||
|
||
struct MyMessage: Encodable { | ||
} | ||
|
||
let myMessage = MyMessage() | ||
|
||
// snippet.show | ||
|
||
let encoder = MessagePackEncoder() | ||
let serializedMessage = try encoder.encode(myMessage) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Advanced Use Cases | ||
|
||
## Application-Specific MessagePack Extension Types | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Advanced Use Cases/ApplicationSpecificMessagePackExtensionTypes") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Common Use Cases | ||
|
||
## Serialization | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/Serialization") | ||
|
||
## Deserialization | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/Deserialization") | ||
|
||
## MessagePack Timestamp Extension Type | ||
|
||
The ``MessagePackTimestamp`` structure represents the MessagePack timestamp extension type. Prefer to use this type instead of `Date` when preserving precision is important. This type has nanosecond precision and uses integer values instead of floating-point values. | ||
|
||
The type conforms to `Codable`, so it can be easily nested within other `Codable` types as expected: | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/MessagePackTimestamp", slice: "codable") | ||
|
||
Initialize a timestamp with its component values: | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/MessagePackTimestamp", slice: "init-with-components") | ||
|
||
Or, initialize a timestamp from a string that follows the RFC 3339 Internet date/time format: | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/MessagePackTimestamp", slice: "init-with-rfc3339") | ||
|
||
Or, initialize a timestamp from a `Date` instance: | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/MessagePackTimestamp", slice: "init-with-date") | ||
|
||
Similarly, a `Date` instance can be initialized from a timestamp: | ||
|
||
@Snippet(path: "msgpack-swift/Snippets/Common Use Cases/MessagePackTimestamp", slice: "date-from-timestamp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters