You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a failing spec in my project that is coming from a null typeclass, and I suspect this is a bug in Magnolia
[info] - should derive a case class *** FAILED ***
[info] java.lang.NullPointerException: type class is null!
[info] at java.base/java.util.Objects.requireNonNull(Objects.java:246)
[info] at com.tersesystems.echopraxia.plusscala.api.Derivation.$anonfun$joinCaseClass$2(Derivation.scala:75)
[info] at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:286)
[info] at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36)
[info] at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33)
[info] at scala.collection.mutable.WrappedArray.foreach(WrappedArray.scala:38)
[info] at scala.collection.TraversableLike.map(TraversableLike.scala:286)
[info] at scala.collection.TraversableLike.map$(TraversableLike.scala:279)
[info] at scala.collection.AbstractTraversable.map(Traversable.scala:108)
[info] at com.tersesystems.echopraxia.plusscala.api.Derivation.com$tersesystems$echopraxia$plusscala$api$Derivation$$$anonfun$joinCaseClass$1(Derivation.scala:72)
Here's the code for it:
sealedtraitDerivationextendsValueTypeClasses {
typeTypeclass[T] =ToValue[T]
typeCaseClass[T] = magnolia1.CaseClass[Typeclass, T]
typeSealedTrait[T] = magnolia1.SealedTrait[Typeclass, T]
finaldefjoin[T](ctx: CaseClass[T]):Typeclass[T] = {
if (ctx.isValueClass) {
joinValueClass(ctx)
} elseif (ctx.isObject) {
joinCaseObject(ctx)
} else {
joinCaseClass(ctx)
}
}
// this is a regular case classprotecteddefjoinCaseClass[T](ctx: CaseClass[T]):Typeclass[T] = { obj =>valtypeInfo=Field.keyValue("@type", ToValue(ctx.typeName.full))
valfields:Seq[Field] = ctx.parameters.map { p =>valname:String= p.label
valattribute= p.dereference(obj)
valtypeclassInstance=Objects.requireNonNull(p.typeclass, "type class is null!")
valvalue:Value[_] = typeclassInstance.toValue(attribute)
Field.keyValue(name, value)
}
ToObjectValue(typeInfo +: fields)
}
// this is a case object, we can't do anything with it.protecteddefjoinCaseObject[T](ctx: CaseClass[T]):Typeclass[T] = {
// ctx has no parameters, so we're better off just passing it straight through.
value =>Value.string(value.toString)
}
// this is a value class aka AnyVal, we should pass it through.protecteddefjoinValueClass[T](ctx: CaseClass[T]):Typeclass[T] = {
valparam= ctx.parameters.head
value => param.typeclass.toValue(param.dereference(value))
}
// this is a sealed traitdefsplit[T](ctx: SealedTrait[T]):Typeclass[T] = (value: T) => {
ctx.split(value) { sub =>
sub.typeclass.toValue(sub.cast(value))
}
}
}
traitSemiAutoDerivationextendsDerivation {
finaldefgen[T]:Typeclass[T] = macro Magnolia.gen[T]
}
It looks like a simple workaround is to define generated implicits using implicit lazy val fooToValue = gen[Foo] and that will resolve the NPE (presumably because by then the instant implicit is in scope)
I have a failing spec in my project that is coming from a null typeclass, and I suspect this is a bug in Magnolia
Here's the code for it:
The PR at tersesystems/echopraxia-plusscala#1 (comment)
This is taken directly from https://github.com/softwaremill/magnolia/blob/scala2/examples/src/main/scala/magnolia1/examples/print.scala#L22 so I don't think this should be possible.
The text was updated successfully, but these errors were encountered: