Skip to content

Commit

Permalink
Correct mapping for JSONCodableScalar
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Jul 31, 2024
1 parent d8c9408 commit f1dd87e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Sources/PubNub/APIs/Objects+PubNub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public extension PubNub {
if uuidFields { includes.append(.uuid) }
if uuidCustomFields { includes.append(.uuidCustom) }
if uuidTypeField { includes.append(.uuidType) }

return includes.isEmpty ? nil : includes
}
}
Expand Down
26 changes: 22 additions & 4 deletions Sources/PubNub/KMM/PubNubObjC+AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ extension PubNubObjC {
)
}

private func convertDictionaryToScalars(_ dictionary: [String: Any]?) -> [String: JSONCodableScalar]? {
dictionary?.compactMapValues { item -> JSONCodableScalar? in
if let number = item as? NSNumber {
if let intValue = number as? Int {
return intValue
} else if let doubleValue = number as? Double {
return doubleValue
} else if let boolValue = number as? Bool {
return boolValue
} else {
return nil
}
} else {
return item as? JSONCodableScalar
}
}
}

// TODO: Swift SDK allows to sort by the status field, it's not present in KMP

private func mapToMembershipSortFields(from array: [String]) -> [PubNub.MembershipSortField] {
Expand Down Expand Up @@ -118,7 +136,7 @@ public extension PubNubObjC {
type: type,
status: status,
channelDescription: description,
custom: (custom?.asMap())?.compactMapValues { $0 as? JSONCodableScalar }
custom: convertDictionaryToScalars(custom?.asMap())
),
include: includeCustom
) {
Expand Down Expand Up @@ -214,7 +232,7 @@ public extension PubNubObjC {
externalId: externalId,
profileURL: profileUrl,
email: email,
custom: (custom?.asMap())?.compactMapValues { $0 as? JSONCodableScalar }
custom: convertDictionaryToScalars(custom?.asMap())
),
include: includeCustom
) {
Expand Down Expand Up @@ -305,7 +323,7 @@ public extension PubNubObjC {
uuidMetadataId: uuid ?? pubnub.configuration.userId,
channelMetadataId: $0.id,
status: $0.status,
custom: $0.custom?.compactMapValues { $0 as? JSONCodableScalar }
custom: convertDictionaryToScalars($0.custom)
)
},
include: .init(
Expand Down Expand Up @@ -444,7 +462,7 @@ public extension PubNubObjC {
uuidMetadataId: $0.id,
channelMetadataId: channel,
status: $0.status,
custom: $0.custom?.compactMapValues { $0 as? JSONCodableScalar }
custom: convertDictionaryToScalars($0.custom)
)
},
include: .init(
Expand Down
25 changes: 12 additions & 13 deletions Sources/PubNub/KMM/PubNubObjC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,19 @@ public extension PubNubObjC {

@objc
public class PubNubConfigurationObjC: NSObject {
let configuration: PubNubConfiguration
let configuration: PubNubConfiguration

public init(configuration: PubNubConfiguration) {
self.configuration = configuration
}

@objc
public var userId: String {
configuration.userId
}
public init(configuration: PubNubConfiguration) {
self.configuration = configuration
}

@objc
public var authKey: String? {
configuration.authKey
}
@objc
public var userId: String {
configuration.userId
}

@objc
public var authKey: String? {
configuration.authKey
}
}

0 comments on commit f1dd87e

Please sign in to comment.