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

chore: added builder pattern to BKTUser #14

Merged
merged 4 commits into from
Jul 4, 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
4 changes: 4 additions & 0 deletions Bucketeer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
93AC8F8028E351C500A4719B /* ScheduledTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93AC8F7F28E351C500A4719B /* ScheduledTask.swift */; };
941E007E2A4FD964002CBFBB /* BKTConfigTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 941E007D2A4FD964002CBFBB /* BKTConfigTests.swift */; };
949542E42A3AEBC0008D0C60 /* MetricsEventUniqueKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 949542E32A3AEBC0008D0C60 /* MetricsEventUniqueKeyTests.swift */; };
E2BE019D2A531C120040F40F /* BKTUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2BE019C2A531C120040F40F /* BKTUserTests.swift */; };
EB2310E2209D91640023A98D /* SecondViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB2310E1209D91640023A98D /* SecondViewController.swift */; };
EB2310E4209D92570023A98D /* ThirdViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB2310E3209D92570023A98D /* ThirdViewController.swift */; };
EB26128D209863F100D62282 /* SplashViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB26128C209863F100D62282 /* SplashViewController.swift */; };
Expand Down Expand Up @@ -287,6 +288,7 @@
93AC8F7F28E351C500A4719B /* ScheduledTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScheduledTask.swift; sourceTree = "<group>"; };
941E007D2A4FD964002CBFBB /* BKTConfigTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BKTConfigTests.swift; sourceTree = "<group>"; };
949542E32A3AEBC0008D0C60 /* MetricsEventUniqueKeyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricsEventUniqueKeyTests.swift; sourceTree = "<group>"; };
E2BE019C2A531C120040F40F /* BKTUserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BKTUserTests.swift; sourceTree = "<group>"; };
EB2310E1209D91640023A98D /* SecondViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondViewController.swift; sourceTree = "<group>"; };
EB2310E3209D92570023A98D /* ThirdViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThirdViewController.swift; sourceTree = "<group>"; };
EB26128C209863F100D62282 /* SplashViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -367,6 +369,7 @@
935D9AB128F5B607007775F5 /* BKTClientTests.swift */,
941E007D2A4FD964002CBFBB /* BKTConfigTests.swift */,
006E85DB28E026EE00B5D90D /* BKTErrorTests.swift */,
E2BE019C2A531C120040F40F /* BKTUserTests.swift */,
0065C3CA28C7BB76002D92A2 /* BucketeerTests.swift */,
9340CA0828D9D1DD00E690CC /* EvaluationDaoTests.swift */,
934889C628EB0ADB007BA05C /* EvaluationForegroundTaskTests.swift */,
Expand Down Expand Up @@ -969,6 +972,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E2BE019D2A531C120040F40F /* BKTUserTests.swift in Sources */,
006E85BD28DEAF7900B5D90D /* ApiClientTests.swift in Sources */,
935D9AB228F5B607007775F5 /* BKTClientTests.swift in Sources */,
006E85DC28E026EE00B5D90D /* BKTErrorTests.swift in Sources */,
Expand Down
13 changes: 7 additions & 6 deletions Bucketeer/Sources/Public/BKTConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public struct BKTConfig {
extension BKTConfig {
// Public init - that will support to init of the BKTConfig like before we add the Builder.
// So it will not create breaking changes
@available(*, deprecated, message: "Use the Builder class instead. Check the documentation for more information.")
public init(
apiKey: String,
apiEndpoint: String,
Expand All @@ -94,7 +95,7 @@ extension BKTConfig {
throw BKTError.illegalArgument(message: "apiKey is required")
}
guard let apiEndpointURL = URL(string: apiEndpoint) else {
throw BKTError.illegalArgument(message: "endpoint is required")
throw BKTError.illegalArgument(message: "apiEndpoint is required")
}
guard !featureTag.isEmpty else {
throw BKTError.illegalArgument(message: "featureTag is required")
Expand Down Expand Up @@ -131,11 +132,11 @@ extension BKTConfig {
}

private init(with builder: Builder) throws {
guard let apiKeyForSDK = builder.apiKey, apiKeyForSDK.isNotEmpty() else {
guard let apiKey = builder.apiKey, apiKey.isNotEmpty() else {
throw BKTError.illegalArgument(message: "apiKey is required")
}
guard let endpoint = builder.apiEndpoint else {
throw BKTError.illegalArgument(message: "endpoint is required")
guard let apiEndpoint = builder.apiEndpoint, apiEndpoint.isNotEmpty() else {
throw BKTError.illegalArgument(message: "apiEndpoint is required")
}
guard let featureTag = builder.featureTag, featureTag.isNotEmpty() else {
throw BKTError.illegalArgument(message: "featureTag is required")
Expand All @@ -151,8 +152,8 @@ extension BKTConfig {
let eventsMaxQueueSize = builder.eventsMaxQueueSize ?? Constant.DEFAULT_MAX_QUEUE_SIZE

// Use the current init method
try self.init(apiKey: apiKeyForSDK,
apiEndpoint: endpoint,
try self.init(apiKey: apiKey,
apiEndpoint: apiEndpoint,
featureTag: featureTag,
eventsFlushInterval: eventsFlushInterval,
eventsMaxQueueSize: eventsMaxQueueSize,
Expand Down
29 changes: 29 additions & 0 deletions Bucketeer/Sources/Public/BKTUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@ import Foundation
public struct BKTUser {
public let id: String
public let attr: [String: String]

public class Builder {
private(set) var id: String?
private(set) var attributes: [String: String] = [:]

public init() {}

public func with(id: String) -> Builder {
self.id = id
return self
}

public func with(attributes: [String: String]) -> Builder {
self.attributes = attributes
return self
}

public func build() throws -> BKTUser {
return try BKTUser.init(with: self)
}
}
}

extension BKTUser {
@available(*, deprecated, message: "Use the Builder class instead. Check the documentation for more information.")
public init(
id: String,
attributes: [String: String] = [:]
Expand All @@ -15,4 +37,11 @@ extension BKTUser {
}
self = BKTUser(id: id, attr: attributes)
}

private init(with builder: Builder) throws {
guard let userId = builder.id, !userId.isEmpty else {
throw BKTError.illegalArgument(message: "The user id is required.")
}
self = BKTUser(id: userId, attr: builder.attributes)
}
}
2 changes: 1 addition & 1 deletion BucketeerTests/BKTConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ final class BKTConfigTests: XCTestCase {
do {
_ = try builder.build()
} catch BKTError.illegalArgument(let message) {
XCTAssertEqual("endpoint is required", message)
XCTAssertEqual("apiEndpoint is required", message)
expectation.fulfill()
} catch {
print("Unexpected error: \(error).")
Expand Down
54 changes: 54 additions & 0 deletions BucketeerTests/BKTUserTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import XCTest
@testable import Bucketeer

final class BKTUserTests: XCTestCase {

func testUserIdRequiredUsingBuilder() {
let expectation = XCTestExpectation()
expectation.expectedFulfillmentCount = 1
let builders = [
BKTUser.Builder()
.with(id: ""),
BKTUser.Builder()
.with(id: "user-id"),
BKTUser.Builder()
.with(id: "user-id")
.with(attributes: [:])
]

builders.forEach { builder in
do {
_ = try builder.build()
} catch BKTError.illegalArgument(let message) {
XCTAssertEqual("The user id is required.", message)
expectation.fulfill()
} catch {
print("Unexpected error: \(error).")
XCTFail("Unexpected error: \(error).")
}
}
wait(for: [expectation], timeout: 0.1)
}

func testUserIdRequired() {
let expectation = XCTestExpectation()
expectation.expectedFulfillmentCount = 1
let userIds = [
"",
"user-id"
]

userIds.forEach { userId in
do {
_ = try BKTUser(id: userId, attributes: [:])
} catch BKTError.illegalArgument(let message) {
XCTAssertEqual("The user id is required.", message)
expectation.fulfill()
} catch {
print("Unexpected error: \(error).")
XCTFail("Unexpected error: \(error).")
}
}
wait(for: [expectation], timeout: 0.1)
}
}
2 changes: 1 addition & 1 deletion BucketeerTests/E2E/BucketeerE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class BucketeerE2ETests: XCTestCase {
UserDefaults.standard.removeObject(forKey: "bucketeer_user_evaluations_id")

let config = try BKTConfig.e2e()
let user = try BKTUser(id: USER_ID)
let user = try BKTUser.Builder().with(id: USER_ID).build()
try await BKTClient.initialize(
config: config,
user: user
Expand Down
2 changes: 1 addition & 1 deletion BucketeerTests/EvaluationForegroundTaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class EvaluationForegroundTaskTests: XCTestCase {
)
task.start()

wait(for: [expectation], timeout: 0.1)
wait(for: [expectation], timeout: 0.3)
}

func testStartAndReceiveError() {
Expand Down
8 changes: 6 additions & 2 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

let user = try! BKTUser(id: "001")
let user = try! BKTUser.Builder()
.with(id: "001")
.with(attributes: [:])
.build()

BKTClient.initialize(
config: self.makeConfig(),
config: self.makeConfigUsingBuilder(),
user: user
) { error in
if let error {
Expand Down
10 changes: 7 additions & 3 deletions ExampleTVOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

let user = try! BKTUser(id: "001", attributes: [:])
let user = try! BKTUser.Builder()
.with(id: "001")
.with(attributes: [:])
.build()

BKTClient.initialize(
config: self.makeConfig(),
config: self.makeConfigUsingBuilder(),
user: user
) { error in
if let error {
Expand Down Expand Up @@ -40,7 +44,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

return true
}

private func makeConfig() -> BKTConfig {
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: "Info", ofType: "plist")!
Expand Down