Skip to content

Commit

Permalink
Switch to pickerWithoutName for ChartSpec.AggregationPeriod
Browse files Browse the repository at this point in the history
  • Loading branch information
nymanjens committed Nov 1, 2024
1 parent f006cd0 commit 6bba3ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 2 additions & 3 deletions app/shared/src/main/scala/app/api/Picklers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ object Picklers extends StandardPicklers {
}
}
implicit val chartSpecAggregationPeriodPickler: Pickler[ChartSpec.AggregationPeriod] =
enumPickler(
_.getClass.getSimpleName,
Seq(ChartSpec.AggregationPeriod.Month, ChartSpec.AggregationPeriod.Year),
enumPicklerWithoutName(
Seq(ChartSpec.AggregationPeriod.Month, ChartSpec.AggregationPeriod.Year)
)

override implicit val entityPickler: Pickler[Entity] = compositePickler[Entity]
Expand Down
24 changes: 23 additions & 1 deletion app/shared/src/main/scala/hydro/api/StandardPicklers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,29 @@ abstract class StandardPicklers {
state.pickle(valueToNumber.get(value))
}
override def unpickle(implicit state: UnpickleState): T = {
valueToNumber.inverse().get(state.unpickle[Int])
val intValue = state.unpickle[Int]
try {
valueToNumber.inverse().get(intValue)
} catch {
case t: Throwable =>
throw new RuntimeException(
s"Could not unpickle intValue=$intValue (valueToNumber=$valueToNumber)",
t,
)
}
}
}
}

def enumPicklerWithoutName[T](values: Seq[T]): Pickler[T] = {
val valueToNumber: Map[T, Int] = values.zipWithIndex.toMap

new Pickler[T] {
override def pickle(value: T)(implicit state: PickleState): Unit = {
state.pickle(valueToNumber(value))
}
override def unpickle(implicit state: UnpickleState): T = {
values(state.unpickle[Int])
}
}
}
Expand Down

0 comments on commit 6bba3ea

Please sign in to comment.