From 07a2debae0c00aba1590398bceb5bea381e9332d Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Thu, 30 Nov 2023 11:52:57 +0600 Subject: [PATCH] Logger inject to OptimizelyConfigImp to log warning for uplicate keys. --- Sources/Optimizely/OptimizelyClient.swift | 2 +- Sources/Optimizely/OptimizelyConfig.swift | 9 ++++----- .../OptimizelyClientTests_OptimizelyConfig.swift | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/Optimizely/OptimizelyClient.swift b/Sources/Optimizely/OptimizelyClient.swift index 38524deb..665bc0af 100644 --- a/Sources/Optimizely/OptimizelyClient.swift +++ b/Sources/Optimizely/OptimizelyClient.swift @@ -782,7 +782,7 @@ open class OptimizelyClient: NSObject { public func getOptimizelyConfig() throws -> OptimizelyConfig { guard let config = self.config else { throw OptimizelyError.sdkNotReady } - return OptimizelyConfigImp(projectConfig: config) + return OptimizelyConfigImp(projectConfig: config, logger: logger) } } diff --git a/Sources/Optimizely/OptimizelyConfig.swift b/Sources/Optimizely/OptimizelyConfig.swift index c38dee5c..e04d3002 100644 --- a/Sources/Optimizely/OptimizelyConfig.swift +++ b/Sources/Optimizely/OptimizelyConfig.swift @@ -95,9 +95,8 @@ struct OptimizelyConfigImp: OptimizelyConfig { var attributes: [OptimizelyAttribute] = [] var audiences: [OptimizelyAudience] = [] var events: [OptimizelyEvent] = [] - var logger = OPTLoggerFactory.getLogger() - - init(projectConfig: ProjectConfig) { + + init(projectConfig: ProjectConfig, logger: OPTLogger = DefaultLogger()) { guard let project = projectConfig.project else { return } self.environmentKey = project.environmentKey ?? "" @@ -140,7 +139,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { return updatedRollout } - self.experimentsMap = makeExperimentsMap(project: project, experiments: updatedExperiments) + self.experimentsMap = makeExperimentsMap(project: project, experiments: updatedExperiments, logger: logger) self.featuresMap = makeFeaturesMap(project: project, experiments: updatedExperiments, rollouts: updatedRollouts) } } @@ -149,7 +148,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { extension OptimizelyConfigImp { - func makeExperimentsMap(project: Project, experiments: [Experiment]) -> [String: Experiment] { + func makeExperimentsMap(project: Project, experiments: [Experiment], logger: OPTLogger) -> [String: Experiment] { var map = [String: Experiment]() experiments.forEach { if map.keys.contains($0.key) { diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index 13dc2d3c..52865d70 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -313,7 +313,7 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let optiConfig = try! optimizely.getOptimizelyConfig() let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap - let logger = (optiConfig as! OptimizelyConfigImp).logger as! TestLogger + let logger = optimizely.logger as! TestLogger XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"]) XCTAssertEqual(optimizelyExpMap.count, 1)