Skip to content

Commit f96bb58

Browse files
committed
Use empty initializer for options structs, create PopulatorConstructable
1 parent 9e48ebc commit f96bb58

File tree

6 files changed

+71
-43
lines changed

6 files changed

+71
-43
lines changed

Sources/SwiftProtobuf/JSONEncodingOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// -----------------------------------------------------------------------------
1414

1515
/// Options for JSONEncoding.
16-
public struct JSONEncodingOptions: Sendable {
16+
public struct JSONEncodingOptions: Sendable, PopulatorConstructable {
1717

1818
/// Always prints int64s values as numbers.
1919
/// By default, they are printed as strings as per proto3 JSON mapping rules.

Sources/SwiftProtobuf/JSONEncodingVisitor.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ internal struct JSONEncodingVisitor: Visitor {
4242
throw JSONEncodingError.missingFieldNames
4343
}
4444
self.options = options
45-
traversalOptions = TraversalOptions(alwaysVisitPrimitiveFields: options.alwaysPrintPrimitiveFields)
45+
traversalOptions = TraversalOptions.with {
46+
$0.alwaysVisitPrimitiveFields = options.alwaysPrintPrimitiveFields
47+
}
4648
}
4749

4850
mutating func startArray() {

Sources/SwiftProtobuf/Message.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
///
3232
/// The actual functionality is implemented either in the generated code or in
3333
/// default implementations of the below methods and properties.
34-
public protocol Message: _CommonMessageConformances {
34+
public protocol Message: _CommonMessageConformances, PopulatorConstructable {
3535
/// Creates a new message with all of its fields initialized to their default
3636
/// values.
3737
init()
@@ -150,28 +150,6 @@ extension Message {
150150
return header + textFormatString()
151151
}
152152
#endif
153-
154-
/// Creates an instance of the message type on which this method is called,
155-
/// executes the given block passing the message in as its sole `inout`
156-
/// argument, and then returns the message.
157-
///
158-
/// This method acts essentially as a "builder" in that the initialization of
159-
/// the message is captured within the block, allowing the returned value to
160-
/// be set in an immutable variable. For example,
161-
///
162-
/// let msg = MyMessage.with { $0.myField = "foo" }
163-
/// msg.myOtherField = 5 // error: msg is immutable
164-
///
165-
/// - Parameter populator: A block or function that populates the new message,
166-
/// which is passed into the block as an `inout` argument.
167-
/// - Returns: The message after execution of the block.
168-
public static func with(
169-
_ populator: (inout Self) throws -> ()
170-
) rethrows -> Self {
171-
var message = Self()
172-
try populator(&message)
173-
return message
174-
}
175153
}
176154

177155
/// Implementation base for all messages; not intended for client use.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Sources/SwiftProtobuf/PopulatorConstructable.swift
2+
//
3+
// Copyright (c) 2014 - 2023 Apple Inc. and the project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See LICENSE.txt for license information:
7+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
8+
//
9+
10+
11+
import Foundation
12+
13+
public protocol PopulatorConstructable {
14+
15+
init()
16+
17+
/// Creates an instance of the `Self` on which this method is called,
18+
/// executes the given block passing the message in as its sole `inout`
19+
/// argument, and then returns the message.
20+
///
21+
/// This method acts essentially as a "builder" in that the initialization of
22+
/// the message is captured within the block, allowing the returned value to
23+
/// be set in an immutable variable. For example,
24+
///
25+
/// let msg = MyMessage.with { $0.myField = "foo" }
26+
/// msg.myOtherField = 5 // error: msg is immutable
27+
///
28+
/// - Parameter populator: A block or function that populates the new message,
29+
/// which is passed into the block as an `inout` argument.
30+
/// - Returns: The `Self` instance after execution of the block.
31+
static func with(
32+
_ populator: (inout Self) throws -> ()
33+
) rethrows -> Self
34+
}
35+
36+
37+
public extension PopulatorConstructable {
38+
39+
static func with(
40+
_ populator: (inout Self) throws -> ()
41+
) rethrows -> Self {
42+
var message = Self()
43+
try populator(&message)
44+
return message
45+
}
46+
}

Sources/SwiftProtobuf/Visitor.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,7 @@ public protocol Visitor {
449449
}
450450

451451
/// Provides options for how visitor traversal should be carried out
452-
public struct TraversalOptions {
453-
static let `default` = TraversalOptions()
452+
public struct TraversalOptions: PopulatorConstructable {
454453

455454
/// Determines if non-optional fields that are equal to their default values should be visited.
456455
/// Defaults to `false`.
@@ -460,11 +459,9 @@ public struct TraversalOptions {
460459
/// - map fields: Will always be visited, even if they are empty.
461460
/// - singular message and group fields: Will be visited only if they are set.
462461
/// - singular primitive fields: Will always be visited under proto2. Under proto3 unset optional primitive fields will not be visited.
463-
public var alwaysVisitPrimitiveFields: Bool
462+
public var alwaysVisitPrimitiveFields: Bool = false
464463

465-
public init(alwaysVisitPrimitiveFields: Bool = false) {
466-
self.alwaysVisitPrimitiveFields = alwaysVisitPrimitiveFields
467-
}
464+
public init() {}
468465
}
469466

470467

@@ -473,7 +470,7 @@ extension Visitor {
473470

474471
// Use the default traversal options if not set
475472
public var traversalOptions: TraversalOptions {
476-
.default
473+
TraversalOptions()
477474
}
478475

479476
// Default definitions of numeric serializations.

Tests/SwiftProtobufTests/Test_JSON.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,9 @@ final class Test_JSONDefaultValues: XCTestCase {
10621062
+ #""FieldName18":0"#
10631063
+ "}"
10641064

1065-
var encodingOptions = JSONEncodingOptions()
1066-
encodingOptions.alwaysPrintPrimitiveFields = true
1065+
let encodingOptions = JSONEncodingOptions.with {
1066+
$0.alwaysPrintPrimitiveFields = true
1067+
}
10671068

10681069
let msg = SwiftProtoTesting_Test3_TestJsonAllTypesProto3()
10691070
XCTAssertEqual(try msg.jsonString(options: encodingOptions), expected)
@@ -1161,9 +1162,10 @@ final class Test_JSONDefaultValues: XCTestCase {
11611162
+ #""FieldName18":0"#
11621163
+ "}"
11631164

1164-
var encodingOptions = JSONEncodingOptions()
1165-
encodingOptions.alwaysPrintPrimitiveFields = true
1166-
1165+
let encodingOptions = JSONEncodingOptions.with {
1166+
$0.alwaysPrintPrimitiveFields = true
1167+
}
1168+
11671169
let msg = SwiftProtoTesting_Test3_TestJsonAllTypesProto3.with {
11681170
$0.optionalNestedMessage = .init()
11691171
$0.optionalString = "i am a test string value"
@@ -1243,8 +1245,9 @@ final class Test_JSONDefaultValues: XCTestCase {
12431245
+ #""defaultCord":"123""#
12441246
+ "}"
12451247

1246-
var encodingOptions = JSONEncodingOptions()
1247-
encodingOptions.alwaysPrintPrimitiveFields = true
1248+
let encodingOptions = JSONEncodingOptions.with {
1249+
$0.alwaysPrintPrimitiveFields = true
1250+
}
12481251
let msg = SwiftProtoTesting_TestAllTypes()
12491252
XCTAssertEqual(try msg.jsonString(options: encodingOptions), expected)
12501253
}
@@ -1318,8 +1321,9 @@ final class Test_JSONDefaultValues: XCTestCase {
13181321
+ #""defaultCord":"123""#
13191322
+ "}"
13201323

1321-
var encodingOptions = JSONEncodingOptions()
1322-
encodingOptions.alwaysPrintPrimitiveFields = true
1324+
let encodingOptions = JSONEncodingOptions.with {
1325+
$0.alwaysPrintPrimitiveFields = true
1326+
}
13231327
let msg = SwiftProtoTesting_TestAllTypes.with {
13241328
$0.optionalNestedMessage = .init()
13251329
$0.optionalString = "i am a test string value"
@@ -1360,8 +1364,9 @@ final class Test_JSONDefaultValues: XCTestCase {
13601364
+ #""replacementString":"${unknown}""#
13611365
+ "}"
13621366

1363-
var encodingOptions = JSONEncodingOptions()
1364-
encodingOptions.alwaysPrintPrimitiveFields = true
1367+
let encodingOptions = JSONEncodingOptions.with {
1368+
$0.alwaysPrintPrimitiveFields = true
1369+
}
13651370
let msg = SwiftProtoTesting_TestExtremeDefaultValues()
13661371
XCTAssertEqual(try msg.jsonString(options: encodingOptions), expected)
13671372
}

0 commit comments

Comments
 (0)