Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created March 3, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkfrog26/d10f33ea1edf73460524c5f72b2051f9 to your computer and use it in GitHub Desktop.
Save darkfrog26/d10f33ea1edf73460524c5f72b2051f9 to your computer and use it in GitHub Desktop.
inline def fromMapElems[A <: Product, T <: Tuple, L <: Tuple](map: Map[String, Value], index: Int, arr: Array[Any]): Unit = {
inline erasedValue[T] match
case _: (hd *: tl) =>
inline erasedValue[L] match
case _: (hdLabel *: tlLabels) =>
import fabric.rw.given
val hdLabelValue = constValue[hdLabel].asInstanceOf[String]
val hdValueOption = map.get(hdLabelValue)
val hdWritable = summonInline[Writer[hd]]
val valueOption = hdWritable.map(_.write(hdValue))
def default = {
val defaultOption = ???
defaultOption.getOrElse(throw new RuntimeException(s"Unable to find field $hd.${hdLabelValue} (and no defaults set) in ${Obj(map)}"))
}
val value = valueOption.getOrElse(default)
arr(index) = value
fromMapElems[A, tl, tlLabels](map, index + 1, arr)
case EmptyTuple => sys.error("Not possible")
case EmptyTuple => // Finished
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment