Skip to content

Commit

Permalink
Implement structure codec swizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcardell committed Sep 17, 2024
1 parent 36c9b70 commit 8c32af0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class JavaProviderItTest extends CatsEffectSuite with TestContainerForAll {
}
}

test("can resolve structure".ignore) {
test("can resolve structure") {
val expected = TestVariant("string", 33)

withContainers { containers =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.cardell.openfeature.StructureDecoderError
import io.cardell.openfeature.StructureEncoder
import io.cardell.openfeature.StructureEncoderError
import io.cardell.openfeature.FlagValue
import io.cardell.openfeature.FlagValue.{StringValue, IntValue}

case class TestVariant(field: String, intField: Int)

Expand All @@ -24,7 +25,12 @@ object TestVariant {

def encodeStructure(
in: TestVariant
): Either[StructureEncoderError, Map[String, FlagValue]] = ???
): Either[StructureEncoderError, Map[String, FlagValue]] = Right(
Map(
"field" -> StringValue(in.field),
"intField" -> IntValue(in.intField)
)
)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import dev.openfeature.sdk.{Value => JValue}
import scala.jdk.CollectionConverters.MapHasAsJava
import scala.util.Try

import dev.openfeature.sdk.{ImmutableStructure => JImmutableStructure}
import io.cardell.openfeature.ContextValue
import io.cardell.openfeature.ErrorCode
import io.cardell.openfeature.FlagValue
import io.cardell.openfeature.EvaluationContext
import io.cardell.openfeature.EvaluationReason
import io.cardell.openfeature.StructureCodec
Expand Down Expand Up @@ -98,6 +100,20 @@ private[java] class JavaProvider[F[_]: Sync](jProvider: JProvider)
)
}

private def toJStructure(map: Map[String, FlagValue]): JImmutableStructure =
new JImmutableStructure(map.map { case (k, v) => (k, toJValue(v)) }.asJava)

private def toJValue(f: FlagValue): JValue =
f match {
case FlagValue.BooleanValue(b) => new JValue(b)
case FlagValue.StringValue(s) => new JValue(s)
case FlagValue.IntValue(i) => new JValue(i)
case FlagValue.DoubleValue(d) => new JValue(d)
case FlagValue.StructureValue(_) => new JValue("")
// val attrs = toJValueMap(s.encoded.get)
// new JValue(attrs)
}

override def metadata: ProviderMetadata = {
lazy val default = ProviderMetadata(name = "java-default")

Expand Down Expand Up @@ -162,19 +178,21 @@ private[java] class JavaProvider[F[_]: Sync](jProvider: JProvider)
): F[ResolutionDetails[A]] = {
// TODO figure out how to get any case class to be a Value
// is this a use case for json
val value = new JValue(defaultValue)

Sync[F]
.blocking(
jProvider.getObjectEvaluation(
flagKey,
value,
toJContext(context)
)
)
.map(toResolutionDetails[JValue, A](_, _.asObject().asInstanceOf[A]))

???
val maybeValue = StructureCodec[A].encodeStructure(defaultValue)

maybeValue match {
case Left(e) => Sync[F].raiseError(e)
case Right(value) =>
Sync[F]
.blocking(
jProvider.getObjectEvaluation(
flagKey,
new JValue(toJStructure(value)),
toJContext(context)
)
)
.map(toResolutionDetails[JValue, A](_, _.asObject().asInstanceOf[A]))
}
}

}
Expand Down

0 comments on commit 8c32af0

Please sign in to comment.