From f1dd87e0af69b2a7b7195b41f9f0077152101fa0 Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Wed, 31 Jul 2024 15:45:01 +0200 Subject: [PATCH] Correct mapping for JSONCodableScalar --- Sources/PubNub/APIs/Objects+PubNub.swift | 2 +- .../PubNub/KMM/PubNubObjC+AppContext.swift | 26 ++++++++++++++++--- Sources/PubNub/KMM/PubNubObjC.swift | 25 +++++++++--------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/Sources/PubNub/APIs/Objects+PubNub.swift b/Sources/PubNub/APIs/Objects+PubNub.swift index 4bf122ec..546af151 100644 --- a/Sources/PubNub/APIs/Objects+PubNub.swift +++ b/Sources/PubNub/APIs/Objects+PubNub.swift @@ -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 } } diff --git a/Sources/PubNub/KMM/PubNubObjC+AppContext.swift b/Sources/PubNub/KMM/PubNubObjC+AppContext.swift index a5f150e8..552f905e 100644 --- a/Sources/PubNub/KMM/PubNubObjC+AppContext.swift +++ b/Sources/PubNub/KMM/PubNubObjC+AppContext.swift @@ -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] { @@ -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 ) { @@ -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 ) { @@ -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( @@ -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( diff --git a/Sources/PubNub/KMM/PubNubObjC.swift b/Sources/PubNub/KMM/PubNubObjC.swift index ee66b6c4..57a732e1 100644 --- a/Sources/PubNub/KMM/PubNubObjC.swift +++ b/Sources/PubNub/KMM/PubNubObjC.swift @@ -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 + } }