Skip to content

Commit

Permalink
Refactor: Function getVariableObject returns dictionary instead of …
Browse files Browse the repository at this point in the history
…a generic object
  • Loading branch information
polok committed Nov 4, 2023
1 parent 5e19a20 commit 8d6021b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
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? {

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

0 comments on commit 8d6021b

Please sign in to comment.