-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add flag evaluation metadata to evaluation details (#111)
Signed-off-by: Nicklas Lundin <nicklaslundin@gmail.com> Signed-off-by: Nicklas Lundin <nicklasl@spotify.com>
- Loading branch information
Showing
6 changed files
with
137 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
android/src/main/java/dev/openfeature/sdk/FlagEvaluationMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package dev.openfeature.sdk | ||
|
||
class EvaluationMetadata internal constructor(private val values: Map<String, Any>) { | ||
|
||
fun getString(key: String): String? = values[key] as? String | ||
|
||
fun getBoolean(key: String): Boolean? = values[key] as? Boolean | ||
|
||
fun getInt(key: String): Int? = values[key] as? Int | ||
|
||
fun getDouble(key: String): Double? = values[key] as? Double | ||
|
||
companion object { | ||
fun builder(): Builder { | ||
return Builder() | ||
} | ||
|
||
val EMPTY = EvaluationMetadata(emptyMap()) | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as EvaluationMetadata | ||
|
||
return values == other.values | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return values.hashCode() | ||
} | ||
} | ||
|
||
class Builder { | ||
private val values: MutableMap<String, Any> = mutableMapOf() | ||
|
||
fun putString(key: String, value: String): Builder { | ||
values[key] = value | ||
return this | ||
} | ||
|
||
fun putInt(key: String, value: Int): Builder { | ||
values[key] = value | ||
return this | ||
} | ||
|
||
fun putDouble(key: String, value: Double): Builder { | ||
values[key] = value | ||
return this | ||
} | ||
|
||
fun putBoolean(key: String, value: Boolean): Builder { | ||
values[key] = value | ||
return this | ||
} | ||
|
||
fun build(): EvaluationMetadata { | ||
return EvaluationMetadata(values.toMap()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
android/src/test/java/dev/openfeature/sdk/EvaluationMetadataTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.openfeature.sdk | ||
|
||
import org.junit.Assert | ||
import org.junit.Test | ||
|
||
class EvaluationMetadataTest { | ||
|
||
private val metadata = EvaluationMetadata.builder() | ||
.putString("key1", "value1") | ||
.putInt("key2", 42) | ||
.putBoolean("key3", true) | ||
.putDouble("key4", 2.71828) | ||
.build() | ||
|
||
@Test | ||
fun testAddAndGet() { | ||
Assert.assertEquals("value1", metadata.getString("key1")) | ||
Assert.assertEquals(42, metadata.getInt("key2")) | ||
Assert.assertEquals(true, metadata.getBoolean("key3")) | ||
Assert.assertEquals(2.71828, metadata.getDouble("key4")) | ||
} | ||
|
||
@Test | ||
fun testGetNonExistentKey() { | ||
Assert.assertNull(metadata.getString("key5")) | ||
} | ||
|
||
@Test | ||
fun testInvalidType() { | ||
Assert.assertNull(metadata.getString("key2")) | ||
Assert.assertNull(metadata.getInt("key3")) | ||
Assert.assertNull(metadata.getBoolean("key4")) | ||
Assert.assertNull(metadata.getDouble("key1")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters