Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize explicit empty instance name #71

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved
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