Skip to content

Commit 01fd5c7

Browse files
committed
update code review
1 parent dea53e0 commit 01fd5c7

File tree

4 files changed

+50
-110
lines changed

4 files changed

+50
-110
lines changed

Source/AwsCommonRuntimeKit/auth/credentials/CredentialsProvider.swift

-30
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,6 @@ public struct CognitoLoginPair: CStruct {
3434
}
3535
}
3636

37-
extension Array where Element: CStruct {
38-
func withAWSArrayList<Result>(_ body: (OpaquePointer?) throws -> Result) rethrows -> Result {
39-
if capacity == 0 {
40-
return try body(nil)
41-
}
42-
43-
let array_list: UnsafeMutablePointer<aws_array_list> = allocator.allocate(capacity: 1)
44-
defer {
45-
aws_array_list_clean_up(array_list)
46-
allocator.release(array_list)
47-
}
48-
guard aws_array_list_init_dynamic(
49-
array_list,
50-
allocator.rawValue,
51-
count,
52-
MemoryLayout<Element.RawType>.size) == AWS_OP_SUCCESS else {
53-
fatalError("Unable to initialize array of user properties")
54-
}
55-
forEach {
56-
$0.withCPointer {
57-
// `aws_array_list_push_back` will do a memory copy of $0 into array_list
58-
guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else {
59-
fatalError("Unable to add user property")
60-
}
61-
}
62-
}
63-
return try body(OpaquePointer(array_list.pointee.data))
64-
}
65-
}
66-
6737
public class CredentialsProvider: CredentialsProviding {
6838

6939
let rawValue: UnsafeMutablePointer<aws_credentials_provider>

Source/AwsCommonRuntimeKit/crt/CStruct.swift

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AwsCCommon
55

66
// This file defines the protocols & helper functions for C Structs.
77
// Instances implementing this protocol should define RawType as their C Struct.
8+
89
protocol CStruct {
910
associatedtype RawType
1011
func withCStruct<Result>(_ body: (RawType) -> Result) -> Result
@@ -18,6 +19,39 @@ extension CStruct {
1819
}
1920
}
2021

22+
extension Array where Element: CStruct {
23+
/// Convert a CStruct Array into raw c pointer. The function would not do a deep copy of the CStruct element. If you required a deep copy
24+
/// make sure to clean up the memory underneath.
25+
func withAWSArrayList<Result>(_ body: (OpaquePointer?) throws -> Result) rethrows -> Result {
26+
guard capacity != 0 else {
27+
return try body(nil)
28+
}
29+
30+
let array_list: UnsafeMutablePointer<aws_array_list> = allocator.allocate(capacity: 1)
31+
defer {
32+
aws_array_list_clean_up(array_list)
33+
allocator.release(array_list)
34+
}
35+
guard aws_array_list_init_dynamic(
36+
array_list,
37+
allocator.rawValue,
38+
count,
39+
MemoryLayout<Element.RawType>.size) == AWS_OP_SUCCESS else {
40+
fatalError("Unable to initialize array of user properties")
41+
}
42+
forEach {
43+
$0.withCPointer {
44+
// `aws_array_list_push_back` will do a memory copy of $0 into array_list, but it would
45+
// not do a deep copy there.
46+
guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else {
47+
fatalError("Unable to add user property")
48+
}
49+
}
50+
}
51+
return try body(OpaquePointer(array_list.pointee.data))
52+
}
53+
}
54+
2155
protocol CStructWithShutdownOptions: CStruct {
2256
func withCStruct<Result>(shutdownOptions: aws_shutdown_callback_options, _ body: (RawType) -> Result) -> Result
2357
}

Source/AwsCommonRuntimeKit/mqtt/Mqtt5Options.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class MqttConnectOptions: CStruct {
7979
public let will: PublishPacket?
8080

8181
/// Array of MQTT5 user properties included with the packet.
82-
public let userProperties: [UserProperty]?
82+
public let userProperties: [UserProperty]
8383

8484
public init (keepAliveInterval: TimeInterval? = nil,
8585
clientId: String? = nil,
@@ -92,7 +92,7 @@ public class MqttConnectOptions: CStruct {
9292
maximumPacketSize: UInt32? = nil,
9393
willDelayInterval: TimeInterval? = nil,
9494
will: PublishPacket? = nil,
95-
userProperties: [UserProperty]? = nil) {
95+
userProperties: [UserProperty] = []) {
9696
self.keepAliveInterval = keepAliveInterval
9797
self.clientId = clientId
9898
self.username = username
@@ -171,11 +171,11 @@ public class MqttConnectOptions: CStruct {
171171
raw_connect_options.client_id = cClientId
172172

173173
// handle user property
174-
return withOptionalUserPropertyArray(of: userProperties) { cUserProperties in
175-
if let cUserProperties = cUserProperties {
176-
raw_connect_options.user_property_count = userProperties!.count
177-
raw_connect_options.user_properties = UnsafePointer<aws_mqtt5_user_property>(cUserProperties)
178-
}
174+
return userProperties.withAWSArrayList { cUserPropertiesPointer in
175+
176+
raw_connect_options.user_property_count = userProperties.count
177+
raw_connect_options.user_properties = UnsafePointer<aws_mqtt5_user_property>(cUserPropertiesPointer)
178+
179179
return withOptionalByteCursorPointerFromString(username) { cUsernamePointer in
180180
raw_connect_options.username = cUsernamePointer
181181
return withAWSByteCursorPointerFromOptionalData(to: password) { cPasswordPointer in

Source/AwsCommonRuntimeKit/mqtt/Mqtt5Packets.swift

+9-73
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,6 @@ public class UserProperty: CStruct {
4444
}
4545
}
4646

47-
extension Array where Element == UserProperty {
48-
func withCMqttUserProperties<Result>(_ body: (OpaquePointer) throws -> Result) rethrows -> Result {
49-
let array_list: UnsafeMutablePointer<aws_array_list> = allocator.allocate(capacity: 1)
50-
defer {
51-
aws_array_list_clean_up(array_list)
52-
allocator.release(array_list)
53-
}
54-
guard aws_array_list_init_dynamic(
55-
array_list,
56-
allocator.rawValue,
57-
count,
58-
MemoryLayout<aws_mqtt5_user_property>.size) == AWS_OP_SUCCESS else {
59-
fatalError("Unable to initialize array of user properties")
60-
}
61-
forEach {
62-
$0.withCPointer {
63-
// `aws_array_list_push_back` will do a memory copy of $0 into array_list
64-
guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else {
65-
fatalError("Unable to add user property")
66-
}
67-
}
68-
}
69-
return try body(OpaquePointer(array_list.pointee.data))
70-
}
71-
}
72-
73-
/// Helper function to convert Swift [UserProperty]? into a native aws_mqtt5_user_property pointer
74-
func withOptionalUserPropertyArray<Result>(
75-
of array: Array<UserProperty>?,
76-
_ body: (OpaquePointer?) throws -> Result) rethrows -> Result {
77-
guard let _array = array else {
78-
return try body(nil)
79-
}
80-
return try _array.withCMqttUserProperties { opaquePointer in
81-
return try body(opaquePointer)
82-
}
83-
}
84-
8547
/// Convert a native aws_mqtt5_user_property pointer into a Swift [UserProperty]?
8648
func convertOptionalUserProperties(count: size_t, userPropertiesPointer: UnsafePointer<aws_mqtt5_user_property>?) -> [UserProperty]? {
8749

@@ -367,32 +329,6 @@ public class Subscription: CStruct {
367329

368330
}
369331

370-
extension Array where Element == Subscription {
371-
func withCSubscriptions<Result>(_ body: (OpaquePointer) throws -> Result) rethrows -> Result {
372-
let array_list: UnsafeMutablePointer<aws_array_list> = allocator.allocate(capacity: 1)
373-
defer {
374-
aws_array_list_clean_up(array_list)
375-
allocator.release(array_list)
376-
}
377-
guard aws_array_list_init_dynamic(
378-
array_list,
379-
allocator.rawValue,
380-
count,
381-
MemoryLayout<Element.RawType>.size) == AWS_OP_SUCCESS else {
382-
fatalError("Unable to initialize array of user properties")
383-
}
384-
forEach {
385-
$0.withCPointer {
386-
// `aws_array_list_push_back` will do a memory copy of $0 into array_list
387-
guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else {
388-
fatalError("Unable to add user property")
389-
}
390-
}
391-
}
392-
return try body(OpaquePointer(array_list.pointee.data))
393-
}
394-
}
395-
396332
/// Data model of an `MQTT5 SUBSCRIBE <https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901161>`_ packet.
397333
public class SubscribePacket: CStruct {
398334

@@ -441,26 +377,26 @@ public class SubscribePacket: CStruct {
441377

442378
typealias RawType = aws_mqtt5_packet_subscribe_view
443379
func withCStruct<Result>(_ body: (RawType) -> Result) -> Result {
444-
var raw_subscrbe_view = aws_mqtt5_packet_subscribe_view()
445-
return self.subscriptions.withCSubscriptions { subscriptionPointer in
446-
raw_subscrbe_view.subscriptions =
380+
var raw_subscribe_view = aws_mqtt5_packet_subscribe_view()
381+
return self.subscriptions.withAWSArrayList {subscriptionPointer in
382+
raw_subscribe_view.subscriptions =
447383
UnsafePointer<aws_mqtt5_subscription_view>(subscriptionPointer)
448-
raw_subscrbe_view.subscription_count = self.subscriptions.count
384+
raw_subscribe_view.subscription_count = self.subscriptions.count
449385

450386
return withOptionalUserPropertyArray(
451387
of: userProperties) { userPropertyPointer in
452388

453389
if let userPropertyPointer,
454390
let userPropertyCount = userProperties?.count {
455-
raw_subscrbe_view.user_property_count = userPropertyCount
456-
raw_subscrbe_view.user_properties =
391+
raw_subscribe_view.user_property_count = userPropertyCount
392+
raw_subscribe_view.user_properties =
457393
UnsafePointer<aws_mqtt5_user_property>(userPropertyPointer)
458394
}
459395

460396
return withOptionalUnsafePointer(
461-
to: self.subscriptionIdentifier) { identiferPointer in
462-
raw_subscrbe_view.subscription_identifier = identiferPointer
463-
return body(raw_subscrbe_view)
397+
to: self.subscriptionIdentifier) { identifierPointer in
398+
raw_subscribe_view.subscription_identifier = identifierPointer
399+
return body(raw_subscribe_view)
464400
}
465401
}
466402
}

0 commit comments

Comments
 (0)