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

Refactor: Function getVariableObject returns dictionary instead of a generic object #54

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 3 additions & 9 deletions Sources/FeaturevisorSDK/Instance+Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,19 @@ extension FeaturevisorInstance {
as? [String]
}

public func getVariableObject<T: Decodable>(
public func getVariableObject(
featureKey: FeatureKey,
variableKey: String,
context: Context
) -> T? {
) -> VariableObjectValue? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, let's leave it as it is for Swift SDK as well. Closing this PR.


let object =
return
getVariable(
featureKey: featureKey,
variableKey: variableKey,
context: context
)?
.value as? VariableObjectValue

guard let data = try? JSONEncoder().encode(object) else {
return nil
}

return try? JSONDecoder().decode(T.self, from: data)
}

public func getVariableJSON<T: Decodable>(
Expand Down
14 changes: 4 additions & 10 deletions Tests/FeaturevisorSDKTests/InstanceTests+Variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ extension FeaturevisorInstanceTests {
func testGetVariableObjectReturnsValidObject() {

// GIVEN
class CustomObject: Decodable {
let title: String
let subtitle: String
let score: Int
}

let variable = Variable(
key: "hero",
value: .object([
Expand Down Expand Up @@ -84,15 +78,15 @@ extension FeaturevisorInstanceTests {
let sdk = try! createInstance(options: options)

// WHEN
let object: CustomObject = sdk.getVariableObject(
let object: VariableObjectValue = sdk.getVariableObject(
featureKey: "e_bar",
variableKey: "hero",
context: [:]
)!

// THEN
XCTAssertEqual(object.title, "Hero Title for B")
XCTAssertEqual(object.subtitle, "Hero Subtitle for B")
XCTAssertEqual(object.score, 10)
XCTAssertEqual(object["title"]?.value as! String, "Hero Title for B")
XCTAssertEqual(object["subtitle"]?.value as! String, "Hero Subtitle for B")
XCTAssertEqual(object["score"]?.value as! Int, 10)
}
}
16 changes: 8 additions & 8 deletions Tests/FeaturevisorSDKTests/InstanceVariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ final class InstanceVariablesTests: XCTestCase {
let sdk = try! createInstance(options: options)

// WHEN
let object: CustomObject = sdk.getVariableObject(
let object: VariableObjectValue? = sdk.getVariableObject(
featureKey: "e_bar",
variableKey: "hero",
context: [:]
)!

// THEN
XCTAssertEqual(object.title, "Hero Title for B")
XCTAssertEqual(object.subtitle, "Hero Subtitle for B")
XCTAssertEqual(object.score, 10)
XCTAssertEqual(object?["title"]?.value as! String, "Hero Title for B")
XCTAssertEqual(object?["subtitle"]?.value as! String, "Hero Subtitle for B")
XCTAssertEqual(object?["score"]?.value as! Int, 10)
}

func testGetVariableJSONReturnsValidObject() {
Expand Down Expand Up @@ -158,16 +158,16 @@ final class InstanceVariablesTests: XCTestCase {
let sdk = try! createInstance(options: options)

// WHEN
let object: CustomObject = sdk.getVariableJSON(
let object: VariableObjectValue? = sdk.getVariableJSON(
featureKey: "e_bar",
variableKey: "hero",
context: [:]
)!

// THEN
XCTAssertEqual(object.title, "Hero Title for B")
XCTAssertEqual(object.subtitle, "Hero Subtitle for B")
XCTAssertEqual(object.score, 10)
XCTAssertEqual(object?["title"]?.value as! String, "Hero Title for B")
XCTAssertEqual(object?["subtitle"]?.value as! String, "Hero Subtitle for B")
XCTAssertEqual(object?["score"]?.value as! Int, 10)
}
}

Expand Down