forked from apple/swift-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
BinaryEncodingOptions
& option for deterministic ordering
Implements the same setting added for JSON in apple#1478 for binary serialization. Related to apple#1477.
- Loading branch information
Showing
5 changed files
with
186 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Sources/SwiftProtobuf/BinaryDecodingOptions.swift - Binary decoding options | ||
// | ||
// Copyright (c) 2014 - 2017 Apple Inc. and the project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See LICENSE.txt for license information: | ||
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt | ||
// | ||
// ----------------------------------------------------------------------------- | ||
/// | ||
/// Binary decoding options | ||
/// | ||
// ----------------------------------------------------------------------------- | ||
|
||
/// Options for binary encoding. | ||
public struct BinaryEncodingOptions: Sendable { | ||
/// Whether to use deterministic ordering when serializing. | ||
/// | ||
/// Note that the deterministic serialization is NOT canonical across languages. | ||
/// It is not guaranteed to remain stable over time. It is unstable across | ||
/// different builds with schema changes due to unknown fields. Users who need | ||
/// canonical serialization (e.g., persistent storage in a canonical form, | ||
/// fingerprinting, etc.) should define their own canonicalization specification | ||
/// and implement their own serializer rather than relying on this API. | ||
/// | ||
/// If deterministic serialization is requested, map entries will be sorted | ||
/// by keys in lexographical order. This is an implementation detail | ||
/// and subject to change. | ||
public var useDeterministicOrdering: Bool = false | ||
|
||
public init() {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Tests/SwiftProtobufTests/Test_JSONEncodingOptions.swift - Various JSON tests | ||
// | ||
// Copyright (c) 2014 - 2018 Apple Inc. and the project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See LICENSE.txt for license information: | ||
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt | ||
// | ||
// ----------------------------------------------------------------------------- | ||
/// | ||
/// Test for the use of BinaryEncodingOptions | ||
/// | ||
// ----------------------------------------------------------------------------- | ||
|
||
import Foundation | ||
import XCTest | ||
import SwiftProtobuf | ||
|
||
class Test_BinaryEncodingOptions: XCTestCase { | ||
|
||
func testUseDeterministicOrdering() throws { | ||
var options = BinaryEncodingOptions() | ||
options.useDeterministicOrdering = true | ||
|
||
let message1 = SwiftProtoTesting_Message3.with { | ||
$0.mapStringString = [ | ||
"b": "B", | ||
"a": "A", | ||
"0": "0", | ||
"UPPER": "v", | ||
"x": "X", | ||
] | ||
$0.mapInt32Message = [ | ||
5: .with { $0.optionalSint32 = 5 }, | ||
1: .with { $0.optionalSint32 = 1 }, | ||
3: .with { $0.optionalSint32 = 3 }, | ||
] | ||
$0.mapInt32Enum = [ | ||
5: .foo, | ||
3: .bar, | ||
0: .baz, | ||
1: .extra3, | ||
] | ||
} | ||
|
||
let message2 = SwiftProtoTesting_Message3.with { | ||
$0.mapStringString = [ | ||
"UPPER": "v", | ||
"a": "A", | ||
"b": "B", | ||
"x": "X", | ||
"0": "0", | ||
] | ||
$0.mapInt32Message = [ | ||
1: .with { $0.optionalSint32 = 1 }, | ||
3: .with { $0.optionalSint32 = 3 }, | ||
5: .with { $0.optionalSint32 = 5 }, | ||
] | ||
$0.mapInt32Enum = [ | ||
3: .bar, | ||
5: .foo, | ||
1: .extra3, | ||
0: .baz, | ||
] | ||
} | ||
|
||
// Approximation that serializing models with the same data (but initialized with keys in | ||
// different orders) consistently produces the same outputs. | ||
let expectedOutput = try message1.serializedData(options: options) | ||
for _ in 0..<10 { | ||
XCTAssertEqual(try message2.serializedData(options: options), expectedOutput) | ||
} | ||
} | ||
} |