Skip to content

Commit

Permalink
fix: normalize explicit empty instance name (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy authored Aug 11, 2023
1 parent 8f0e80f commit ac60e5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
18 changes: 10 additions & 8 deletions Sources/Amplitude/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import Foundation

public class Configuration {
public var apiKey: String
public internal(set) var apiKey: String
public var flushQueueSize: Int
public var flushIntervalMillis: Int
public var instanceName: String
public let instanceName: String
public var optOut: Bool
public var storageProvider: any Storage
public var identifyStorageProvider: any Storage
public let storageProvider: any Storage
public let identifyStorageProvider: any Storage
public var logLevel: LogLevelEnum
public var loggerProvider: any Logger
public var minIdLength: Int?
Expand All @@ -38,7 +38,7 @@ public class Configuration {
apiKey: String,
flushQueueSize: Int = Constants.Configuration.FLUSH_QUEUE_SIZE,
flushIntervalMillis: Int = Constants.Configuration.FLUSH_INTERVAL_MILLIS,
instanceName: String = Constants.Configuration.DEFAULT_INSTANCE,
instanceName: String = "",
optOut: Bool = false,
storageProvider: (any Storage)? = nil,
identifyStorageProvider: (any Storage)? = nil,
Expand All @@ -61,15 +61,17 @@ public class Configuration {
identifyBatchIntervalMillis: Int = Constants.Configuration.IDENTIFY_BATCH_INTERVAL_MILLIS,
migrateLegacyData: Bool = true
) {
let normalizedInstanceName = instanceName == "" ? Constants.Configuration.DEFAULT_INSTANCE : instanceName

self.apiKey = apiKey
self.flushQueueSize = flushQueueSize
self.flushIntervalMillis = flushIntervalMillis
self.instanceName = instanceName
self.instanceName = normalizedInstanceName
self.optOut = optOut
self.storageProvider = storageProvider
?? PersistentStorage(storagePrefix: "storage-\(instanceName)")
?? PersistentStorage(storagePrefix: "storage-\(normalizedInstanceName)")
self.identifyStorageProvider = identifyStorageProvider
?? PersistentStorage(storagePrefix: "identify-\(instanceName)")
?? PersistentStorage(storagePrefix: "identify-\(normalizedInstanceName)")
self.logLevel = logLevel
self.loggerProvider = loggerProvider
self.minIdLength = minIdLength
Expand Down
11 changes: 10 additions & 1 deletion Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ final class AmplitudeTests: XCTestCase {
)
}

func testInit() {
func testInit_defaultInstanceName() {
let configuration = Configuration(apiKey: "api-key")
XCTAssertEqual(
Amplitude(configuration: configuration).configuration.instanceName,
Constants.Configuration.DEFAULT_INSTANCE
)
}

func testInit_emptyInstanceName() {
let configuration = Configuration(apiKey: "api-key", instanceName: "")
XCTAssertEqual(
Amplitude(configuration: configuration).configuration.instanceName,
Constants.Configuration.DEFAULT_INSTANCE
Expand Down

0 comments on commit ac60e5e

Please sign in to comment.