Skip to content

Commit 65902f1

Browse files
committed
Remove PopulatorConstructable
1 parent 3347269 commit 65902f1

File tree

6 files changed

+38
-67
lines changed

6 files changed

+38
-67
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, PopulatorConstructable {
16+
public struct JSONEncodingOptions: Sendable {
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ internal struct JSONEncodingVisitor: Visitor {
4242
throw JSONEncodingError.missingFieldNames
4343
}
4444
self.options = options
45-
traversalOptions = TraversalOptions.with {
46-
$0.alwaysVisitPrimitiveFields = options.alwaysPrintPrimitiveFields
47-
}
45+
var traversalOptions = TraversalOptions()
46+
traversalOptions.alwaysVisitPrimitiveFields = options.alwaysPrintPrimitiveFields
47+
self.traversalOptions = traversalOptions
4848
}
4949

5050
mutating func startArray() {

Sources/SwiftProtobuf/Message.swift

Lines changed: 23 additions & 1 deletion
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, PopulatorConstructable {
34+
public protocol Message: _CommonMessageConformances {
3535
/// Creates a new message with all of its fields initialized to their default
3636
/// values.
3737
init()
@@ -150,6 +150,28 @@ 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+
}
153175
}
154176

155177
/// Implementation base for all messages; not intended for client use.

Sources/SwiftProtobuf/PopulatorConstructable.swift

Lines changed: 0 additions & 46 deletions
This file was deleted.

Sources/SwiftProtobuf/Visitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public protocol Visitor {
449449
}
450450

451451
/// Provides options for how visitor traversal should be carried out
452-
public struct TraversalOptions: PopulatorConstructable {
452+
public struct TraversalOptions {
453453

454454
/// Determines if non-optional fields that are equal to their default values should be visited.
455455
/// Defaults to `false`.

Tests/SwiftProtobufTests/Test_JSON.swift

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

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

10691068
let msg = SwiftProtoTesting_Test3_TestJsonAllTypesProto3()
10701069
XCTAssertEqual(try msg.jsonString(options: encodingOptions), expected)
@@ -1162,9 +1161,8 @@ final class Test_JSONDefaultValues: XCTestCase {
11621161
+ #""FieldName18":0"#
11631162
+ "}"
11641163

1165-
let encodingOptions = JSONEncodingOptions.with {
1166-
$0.alwaysPrintPrimitiveFields = true
1167-
}
1164+
var encodingOptions = JSONEncodingOptions()
1165+
encodingOptions.alwaysPrintPrimitiveFields = true
11681166

11691167
let msg = SwiftProtoTesting_Test3_TestJsonAllTypesProto3.with {
11701168
$0.optionalNestedMessage = .init()
@@ -1245,9 +1243,8 @@ final class Test_JSONDefaultValues: XCTestCase {
12451243
+ #""defaultCord":"123""#
12461244
+ "}"
12471245

1248-
let encodingOptions = JSONEncodingOptions.with {
1249-
$0.alwaysPrintPrimitiveFields = true
1250-
}
1246+
var encodingOptions = JSONEncodingOptions()
1247+
encodingOptions.alwaysPrintPrimitiveFields = true
12511248
let msg = SwiftProtoTesting_TestAllTypes()
12521249
XCTAssertEqual(try msg.jsonString(options: encodingOptions), expected)
12531250
}
@@ -1321,9 +1318,8 @@ final class Test_JSONDefaultValues: XCTestCase {
13211318
+ #""defaultCord":"123""#
13221319
+ "}"
13231320

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

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

0 commit comments

Comments
 (0)