Skip to content

Commit

Permalink
Logger inject to OptimizelyConfigImp to log warning for uplicate keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
muzahidul-opti committed Nov 30, 2023
1 parent 037a4f2 commit 07a2deb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Expand Down
9 changes: 4 additions & 5 deletions Sources/Optimizely/OptimizelyConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? ""
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 07a2deb

Please sign in to comment.