Skip to content

Commit f483df7

Browse files
committed
feat: FlagEvalMetadata - add class
Signed-off-by: Nicklas Lundin <[email protected]>
1 parent 700e16b commit f483df7

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dev.openfeature.sdk
2+
3+
class EvaluationMetadata internal constructor(private val values: Map<String, Any>) {
4+
5+
fun getString(key: String): String? = values[key] as? String
6+
7+
fun getBoolean(key: String): Boolean? = values[key] as? Boolean
8+
9+
fun getInt(key: String): Int? = values[key] as? Int
10+
11+
fun getDouble(key: String): Double? = values[key] as? Double
12+
13+
fun getAny(key: String): Any? = values[key]
14+
15+
companion object {
16+
fun builder(): Builder {
17+
return Builder()
18+
}
19+
}
20+
21+
override fun equals(other: Any?): Boolean {
22+
if (this === other) return true
23+
if (javaClass != other?.javaClass) return false
24+
25+
other as EvaluationMetadata
26+
27+
return values == other.values
28+
}
29+
30+
override fun hashCode(): Int {
31+
return values.hashCode()
32+
}
33+
}
34+
35+
class Builder {
36+
private val values: MutableMap<String, Any> = mutableMapOf()
37+
38+
fun putString(key: String, value: String): Builder {
39+
values[key] = value
40+
return this
41+
}
42+
43+
fun putInt(key: String, value: Int): Builder {
44+
values[key] = value
45+
return this
46+
}
47+
48+
fun putDouble(key: String, value: Double): Builder {
49+
values[key] = value
50+
return this
51+
}
52+
53+
fun putBoolean(key: String, value: Boolean): Builder {
54+
values[key] = value
55+
return this
56+
}
57+
58+
fun build(): EvaluationMetadata {
59+
return EvaluationMetadata(values)
60+
}
61+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dev.openfeature.sdk
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class EvaluationMetadataTest {
7+
8+
private val metadata = EvaluationMetadata.builder()
9+
.putString("key1", "value1")
10+
.putInt("key2", 42)
11+
.putBoolean("key3", true)
12+
.putDouble("key4", 2.71828)
13+
.build()
14+
15+
@Test
16+
fun testAddAndGet() {
17+
Assert.assertEquals("value1", metadata.getString("key1"))
18+
Assert.assertEquals(42, metadata.getInt("key2"))
19+
Assert.assertEquals(true, metadata.getBoolean("key3"))
20+
Assert.assertEquals(2.71828, metadata.getDouble("key4"))
21+
}
22+
23+
@Test
24+
fun testGetNonExistentKey() {
25+
Assert.assertNull(metadata.getString("key5"))
26+
}
27+
28+
@Test
29+
fun testInvalidType() {
30+
Assert.assertNull(metadata.getString("key2"))
31+
Assert.assertNull(metadata.getInt("key3"))
32+
Assert.assertNull(metadata.getBoolean("key4"))
33+
Assert.assertNull(metadata.getDouble("key1"))
34+
}
35+
36+
@Test
37+
fun testGetAny() {
38+
val any: Any? = metadata.getAny("key1")
39+
Assert.assertEquals("value1", any as? String)
40+
}
41+
}

0 commit comments

Comments
 (0)