Skip to content

Commit 19d8ff9

Browse files
authored
Merge pull request #197 from morgen-peschke/bugfix-initialization-order
Fix initialization order bug
2 parents c86d91a + a4a1bfb commit 19d8ff9

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

core/src/main/scala/org/typelevel/catapult/codec/LDCodec.scala

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,26 @@ object LDCodec {
168168
typeName: String,
169169
toDouble: N => Double,
170170
fromDouble: Double => N,
171-
): LDCodec[N] = LDCodec[Double].imapV[N](
172-
n => {
171+
): LDCodec[N] = new LDCodec[N] {
172+
override def encode(n: N, history: LDCursorHistory): LDCodecResult[LDValue] = {
173173
val d = toDouble(n)
174-
Validated
175-
.condNec(toDouble(n) == d, d, LDReason.undecodableValue(LDValueType.NUMBER, typeName))
176-
},
177-
d => {
178-
val n = fromDouble(d)
179174
Validated.condNec(
180175
toDouble(n) == d,
181-
n,
182-
LDReason.unencodableValue(LDValueType.NUMBER, typeName),
176+
LDValue.of(d),
177+
LDCodecFailure(LDReason.undecodableValue(LDValueType.NUMBER, typeName), history),
183178
)
184-
},
185-
)
179+
}
180+
181+
override def decode(c: LDCursor): LDCodecResult[N] =
182+
c.checkType(LDValueType.NUMBER).map(_.value.doubleValue()).andThen { d =>
183+
val n = fromDouble(d)
184+
Validated.condNec(
185+
toDouble(n) == d,
186+
n,
187+
c.fail(LDReason.unencodableValue(LDValueType.NUMBER, typeName)),
188+
)
189+
}
190+
}
186191

187192
implicit val floatInstance: LDCodec[Float] = numericInstance("Float", _.toDouble, _.toFloat)
188193

core/src/main/scala/org/typelevel/catapult/codec/LDCodecWithInfallibleEncode.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ package org.typelevel.catapult.codec
1919
import cats.Invariant
2020
import cats.syntax.all.*
2121
import com.launchdarkly.sdk.{LDValue, LDValueType}
22-
import org.typelevel.catapult.codec.LDCodec.{
23-
LDCodecResult,
24-
withInfallibleEncode,
25-
withInfallibleEncodeFull,
26-
}
22+
import org.typelevel.catapult.codec.LDCodec.{LDCodecResult, withInfallibleEncode}
2723

2824
trait LDCodecWithInfallibleEncode[A] extends LDCodec[A] {
2925

@@ -58,19 +54,19 @@ object LDCodecWithInfallibleEncode {
5854
implicit val ldValueInstance: LDCodecWithInfallibleEncode[LDValue] =
5955
withInfallibleEncode(identity, _.value)
6056

61-
implicit val booleanInstance: LDCodecWithInfallibleEncode[Boolean] = withInfallibleEncodeFull(
57+
implicit val booleanInstance: LDCodecWithInfallibleEncode[Boolean] = instanceFull(
6258
LDValue.of,
6359
_.checkType(LDValueType.BOOLEAN).map(_.value.booleanValue()),
6460
)
6561

66-
implicit val stringInstance: LDCodecWithInfallibleEncode[String] = withInfallibleEncodeFull(
62+
implicit val stringInstance: LDCodecWithInfallibleEncode[String] = instanceFull(
6763
LDValue.of,
6864
_.checkType(LDValueType.STRING).map(_.value.stringValue()),
6965
)
7066

7167
// This is the canonical encoding of numbers in an LDValue, other
7268
// numerical types are derived from this because of this constraint.
73-
implicit val doubleInstance: LDCodecWithInfallibleEncode[Double] = withInfallibleEncodeFull(
69+
implicit val doubleInstance: LDCodecWithInfallibleEncode[Double] = instanceFull(
7470
LDValue.of,
7571
_.checkType(LDValueType.NUMBER).map(_.value.doubleValue()),
7672
)

0 commit comments

Comments
 (0)