@@ -98,8 +98,8 @@ trait JsonSchemas extends algebra.JsonSchemas:
98
98
* trait’s `ClassTag`.
99
99
*/
100
100
inline def genericTagged [A ](using sumOf : Mirror .SumOf [A ]): Tagged [A ] =
101
- summonTagged[sumOf.MirroredElemTypes , sumOf.MirroredElemLabels ](0 ).asInstanceOf [Tagged [sumOf. MirroredElemTypes ]] // FIXME Remove asInstanceOf
102
- .xmap[A ](tuple => last(tuple). asInstanceOf [ A ])( a => asTuple( sumOf.ordinal(a), a). asInstanceOf [sumOf. MirroredElemTypes ] )
101
+ summonTagged[A , sumOf.MirroredElemTypes , sumOf.MirroredElemLabels ](0 ).asInstanceOf [Tagged [( Int , A ) ]] // FIXME Remove asInstanceOf
102
+ .xmap[A ]{ case (_, a) => a}( a => sumOf.ordinal(a) -> a )
103
103
.pipe(tagged => summonName[A ].fold(tagged)(tagged.named))
104
104
.withDiscriminator(summonDiscriminator[A ])
105
105
.pipe(tagged => summonDescription[A ].fold(tagged)(tagged.withDescription))
@@ -212,18 +212,6 @@ trait JsonSchemas extends algebra.JsonSchemas:
212
212
case _ => defaultDiscriminatorName
213
213
}
214
214
215
- /** Retrieve the last element of a tuple */
216
- private def last (tuple : Tuple ): Any =
217
- tuple match
218
- case h *: EmptyTuple => h
219
- case () *: t => last(t)
220
-
221
- /** Build a tuple with `n` elements, the last one containing the value `a` */
222
- private def asTuple [A ](n : Int , a : A ): Tuple =
223
- n match
224
- case 0 => a *: EmptyTuple
225
- case _ => () *: asTuple(n - 1 , a)
226
-
227
215
/** Summon the [[Record ]] schema of a concrete type of a sealed trait.
228
216
* If there is a given instance of `GenericJsonSchema.GenericRecord[A]`,
229
217
* it is used as the schema, otherwise we derive the schema.
@@ -235,33 +223,29 @@ trait JsonSchemas extends algebra.JsonSchemas:
235
223
}
236
224
237
225
/** Summon a [[Tagged ]] schema for the alternatives `Types`.
226
+ * @tparam A The sum type
238
227
* @tparam Types List of record types (e.g. `(Circle, Rectangle)`)
239
228
* @tparam Labels List of record labels’ types (e.g. `("Circle".type, "Rectangle".type)`)
240
229
* @param i Index of the head alternative
241
- *
242
- * We encode the ordinal `i` of each alternative as a tuple containing `i - 1` `Unit`
243
- * followed by the actual value.
244
230
*/
245
- private [generic] inline def summonTagged [Types <: Tuple , Labels <: Tuple ](i : Int ): Tagged [Types ] =
231
+ private [generic] inline def summonTagged [A , Types <: Tuple , Labels <: Tuple ](i : Int ): Tagged [( Int , A ) ] =
246
232
inline erasedValue[(Types , Labels )] match
247
233
// Last alternative
248
234
case _ : (head *: EmptyTuple , labelHead *: EmptyTuple ) =>
249
235
summonTaggedRecord[head].asInstanceOf [Record [head]] // FIXME Remove asInstanceOf
250
- .tagged(constValue[labelHead].asInstanceOf [String ]) // TODO Remove asInstanceOf
251
- .xmap[head *: EmptyTuple ](h => h *: EmptyTuple ) { case h *: EmptyTuple => h }
252
- .asInstanceOf [Tagged [Types ]] // FIXME Remove asInstanceOf
236
+ .tagged(constValue[labelHead].asInstanceOf [String ])
237
+ .xmap(h => i -> h.asInstanceOf [A ]) { case (_, h) => h.asInstanceOf [head] }
253
238
case _ : (head *: tail, labelHead *: labelsTail) =>
254
239
summonTaggedRecord[head].asInstanceOf [Record [head]] // FIXME Remove asInstanceOf
255
- .tagged(constValue[labelHead].asInstanceOf [String ]) // TODO Remove asInstanceOf
256
- .orElse(summonTagged[tail, labelsTail](i + 1 ).asInstanceOf [Tagged [tail]]) // FIXME Remove asInstanceOf
240
+ .tagged(constValue[labelHead].asInstanceOf [String ])
241
+ .orElse(summonTagged[A , tail, labelsTail](i + 1 ).asInstanceOf [Tagged [( Int , tail) ]]) // FIXME Remove asInstanceOf
257
242
.xmap {
258
- case Left (h) => h *: EmptyTuple
259
- case Right (t) => () *: t
243
+ case Left (h) => i -> h. asInstanceOf [ A ]
244
+ case Right (t) => t. asInstanceOf [( Int , A )]
260
245
} {
261
- case h *: EmptyTuple => Left (h.asInstanceOf [head])
262
- case () *: t => Right (t.asInstanceOf [tail])
246
+ case (`i`, h) => Left (h.asInstanceOf [head])
247
+ case t => Right (t.asInstanceOf [( Int , tail) ])
263
248
}
264
- .asInstanceOf [Tagged [Types ]]
265
249
266
250
extension [A <: Product ](schema : JsonSchema [A ])
267
251
/**
0 commit comments