Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from spotify/rename-instant-value-class
Browse files Browse the repository at this point in the history
Rename the Instant value to Date
  • Loading branch information
nicklasl authored Jul 6, 2023
2 parents 708732c + a629490 commit c4af5b3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MutableStructure(private var attributes: MutableMap<String, Value> = mutab
is Value.String -> value.asString()
is Value.Boolean -> value.asBoolean()
is Value.Integer -> value.asInteger()
is Value.Instant -> value.asInstant()
is Value.Date -> value.asDate()
is Value.Double -> value.asDouble()
}
}
Expand Down
6 changes: 3 additions & 3 deletions OpenFeature/src/main/java/dev/openfeature/sdk/Value.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sealed interface Value {
fun asBoolean(): kotlin.Boolean? = if (this is Boolean) boolean else null
fun asInteger(): Int? = if (this is Integer) integer else null
fun asDouble(): kotlin.Double? = if (this is Double) double else null
fun asInstant(): Date? = if (this is Instant) instant else null
fun asDate(): java.util.Date? = if (this is Date) date else null
fun asList(): kotlin.collections.List<Value>? = if (this is List) list else null
fun asStructure(): Map<kotlin.String, Value>? = if (this is Structure) structure else null
fun isNull(): kotlin.Boolean = this is Null
Expand All @@ -40,7 +40,7 @@ sealed interface Value {
data class Double(val double: kotlin.Double) : Value

@Serializable
data class Instant(@Serializable(DateSerializer::class) val instant: Date) : Value
data class Date(@Serializable(DateSerializer::class) val date: java.util.Date) : Value

@Serializable
data class Structure(val structure: Map<kotlin.String, Value>) : Value
Expand All @@ -67,7 +67,7 @@ object ValueSerializer : JsonContentPolymorphicSerializer<Value>(Value::class) {
setOf("boolean") -> Value.Boolean.serializer()
setOf("integer") -> Value.Integer.serializer()
setOf("double") -> Value.Double.serializer()
setOf("instant") -> Value.Instant.serializer()
setOf("date") -> Value.Date.serializer()
setOf("list") -> Value.List.serializer()
setOf("structure") -> Value.Structure.serializer()
else -> throw OpenFeatureError.ParseError("couldn't find deserialization key for Value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class EvalContextTests {
Assert.assertEquals(3, ctx.getValue("int")?.asInteger())
ctx.add("double", Value.Double(3.14))
Assert.assertEquals(3.14, ctx.getValue("double")?.asDouble())
ctx.add("instant", Value.Instant(now))
Assert.assertEquals(now, ctx.getValue("instant")?.asInstant())
ctx.add("date", Value.Date(now))
Assert.assertEquals(now, ctx.getValue("date")?.asDate())
}

@Test
Expand Down Expand Up @@ -74,7 +74,7 @@ class EvalContextTests {
ctx.add("bool2", Value.Boolean(false))
ctx.add("int1", Value.Integer(4))
ctx.add("int2", Value.Integer(2))
ctx.add("dt", Value.Instant(now))
ctx.add("dt", Value.Date(now))
ctx.add("obj", Value.Structure(mapOf("val1" to Value.Integer(1), "val2" to Value.String("2"))))

val map = ctx.asMap()
Expand All @@ -85,7 +85,7 @@ class EvalContextTests {
Assert.assertEquals(false, map["bool2"]?.asBoolean())
Assert.assertEquals(4, map["int1"]?.asInteger())
Assert.assertEquals(2, map["int2"]?.asInteger())
Assert.assertEquals(now, map["dt"]?.asInstant())
Assert.assertEquals(now, map["dt"]?.asDate())
Assert.assertEquals(1, structure?.get("val1")?.asInteger())
Assert.assertEquals("2", structure?.get("val2")?.asString())
}
Expand Down Expand Up @@ -132,7 +132,7 @@ class EvalContextTests {
ctx.add("bool", Value.Boolean(false))
ctx.add("integer", Value.Integer(1))
ctx.add("double", Value.Double(1.2))
ctx.add("date", Value.Instant(now))
ctx.add("date", Value.Date(now))
ctx.add("null", Value.Null)
ctx.add("list", Value.List(listOf(Value.String("item1"), Value.Boolean(true))))
ctx.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class StructureTests {
structure.add("string", Value.String("val"))
structure.add("int", Value.Integer(13))
structure.add("double", Value.Double(0.5))
structure.add("date", Value.Instant(now))
structure.add("date", Value.Date(now))
structure.add("list", Value.List(listOf()))
structure.add("structure", Value.Structure(mapOf()))

Assert.assertEquals(true, structure.getValue("bool")?.asBoolean())
Assert.assertEquals("val", structure.getValue("string")?.asString())
Assert.assertEquals(13, structure.getValue("int")?.asInteger())
Assert.assertEquals(0.5, structure.getValue("double")?.asDouble())
Assert.assertEquals(now, structure.getValue("date")?.asInstant())
Assert.assertEquals(now, structure.getValue("date")?.asDate())
Assert.assertEquals(listOf<Value>(), structure.getValue("list")?.asList())
Assert.assertEquals(mapOf<String, Value>(), structure.getValue("structure")?.asStructure())
}
Expand Down
8 changes: 4 additions & 4 deletions OpenFeature/src/test/java/dev/openfeature/sdk/ValueTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ValueTests {
"bool" to Value.Boolean(true),
"int" to Value.Integer(3),
"double" to Value.Double(4.5),
"date" to Value.Instant(date),
"date" to Value.Date(date),
"list" to Value.List(listOf(Value.Boolean(false), Value.Integer(4))),
"structure" to Value.Structure(mapOf("int" to Value.Integer(5)))
)
Expand All @@ -86,7 +86,7 @@ class ValueTests {

@Test
fun testJsonDecode() {
val stringInstant = "2023-03-01T14:01:46.321Z"
val stringDateTime = "2023-03-01T14:01:46Z"
val json = "{" +
" \"structure\": {" +
" \"null\": {}," +
Expand All @@ -103,7 +103,7 @@ class ValueTests {
" \"double\": 4.5" +
" }," +
" \"date\": {" +
" \"instant\": \"$stringInstant\"" +
" \"date\": \"$stringDateTime\"" +
" }," +
" \"list\": {" +
" \"list\": [" +
Expand Down Expand Up @@ -132,7 +132,7 @@ class ValueTests {
"bool" to Value.Boolean(true),
"int" to Value.Integer(3),
"double" to Value.Double(4.5),
"date" to Value.Instant(Date.from(Instant.parse(stringInstant))),
"date" to Value.Date(Date.from(Instant.parse(stringDateTime))),
"list" to Value.List(listOf(Value.Boolean(false), Value.Integer(4))),
"structure" to Value.Structure(mapOf("int" to Value.Integer(5)))
)
Expand Down

0 comments on commit c4af5b3

Please sign in to comment.