Skip to content

Commit

Permalink
Keep formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nox213 committed Dec 7, 2024
1 parent 4319c72 commit fc3ba46
Showing 1 changed file with 75 additions and 74 deletions.
149 changes: 75 additions & 74 deletions upickle/implicits/src-2/upickle/implicits/internal/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ object Macros {
def deriveDefaults(companion: c.Tree, hasDefaults: Seq[Boolean]): Seq[c.Tree] = {
val defaults =
for((hasDefault, i) <- hasDefaults.zipWithIndex)
yield {
val defaultName = newTermName("apply$default$" + (i + 1))
if (!hasDefault) q"null"
else q"$companion.$defaultName"
}
yield {
val defaultName = newTermName("apply$default$" + (i + 1))
if (!hasDefault) q"null"
else q"$companion.$defaultName"
}
defaults
}

/**
* If a super-type is generic, find all the subtypes, but at the same time
* fill in all the generic type parameters that are based on the super-type's
* concrete type
*/
* If a super-type is generic, find all the subtypes, but at the same time
* fill in all the generic type parameters that are based on the super-type's
* concrete type
*/
def fleshedOutSubtypes(tpe: Type) = {
for{
subtypeSym <- tpe.typeSymbol.asClass.knownDirectSubclasses.filter(!_.toString.contains("<local child>"))
Expand Down Expand Up @@ -193,24 +193,24 @@ object Macros {
companion.tpe.member(TermName("apply")).info

val derive =
// Otherwise, reading and writing are kinda identical
wrapCaseN(
companion,
rawArgs,
argSyms,
mappedArgs,
argSyms.map(_.typeSignature).map(func),
hasDefaults,
tpe,
argSyms.exists(_.typeSignature.typeSymbol == definitions.RepeatedParamClass)
)
// Otherwise, reading and writing are kinda identical
wrapCaseN(
companion,
rawArgs,
argSyms,
mappedArgs,
argSyms.map(_.typeSignature).map(func),
hasDefaults,
tpe,
argSyms.exists(_.typeSignature.typeSymbol == definitions.RepeatedParamClass)
)

annotate(tpe)(derive)
}
}
/** If there is a sealed base class, annotate the derived tree in the JSON
* representation with a class label.
*/
* representation with a class label.
*/
def annotate(tpe: c.Type)(derived: c.universe.Tree) = {
val sealedParents = tpe.baseClasses.filter(_.asClass.isSealed)

Expand All @@ -228,8 +228,8 @@ object Macros {
val segments =
sealedClassSymbol.toList.map(_.fullName.split('.')) ++
sealedParents
.flatMap(_.asClass.knownDirectSubclasses)
.map(_.fullName.split('.'))
.flatMap(_.asClass.knownDirectSubclasses)
.map(_.fullName.split('.'))


// -1 because even if there is only one subclass, and so no name segments
Expand Down Expand Up @@ -258,16 +258,16 @@ object Macros {
}

def customKey(sym: c.Symbol): Option[String] = {
sym.annotations
.find(_.tpe == typeOf[key])
.flatMap(_.scalaArgs.headOption)
.map{case Literal(Constant(s)) => s.toString}
sym.annotations
.find(_.tpe == typeOf[key])
.flatMap(_.scalaArgs.headOption)
.map{case Literal(Constant(s)) => s.toString}
}
def serializeDefaults(sym: c.Symbol): Option[Boolean] = {
sym.annotations
.find(_.tpe == typeOf[upickle.implicits.serializeDefaults])
.flatMap(_.scalaArgs.headOption)
.map{case Literal(Constant(s)) => s.asInstanceOf[Boolean]}
sym.annotations
.find(_.tpe == typeOf[upickle.implicits.serializeDefaults])
.flatMap(_.scalaArgs.headOption)
.map{case Literal(Constant(s)) => s.asInstanceOf[Boolean]}
}

def wrapObject(obj: Tree): Tree
Expand Down Expand Up @@ -308,74 +308,74 @@ object Macros {
val aggregates = for (i <- rawArgs.indices) yield TermName("aggregated" + i)
q"""
..${
for (i <- rawArgs.indices)
for (i <- rawArgs.indices)
yield q"private[this] lazy val ${localReaders(i)} = implicitly[${c.prefix}.Reader[${argTypes(i)}]]"
}
}
new ${c.prefix}.CaseClassReader[$targetType]{
override def visitObject(length: Int, jsonableKeys: Boolean, index: Int) = new ${if (rawArgs.size <= 64) tq"_root_.upickle.implicits.CaseObjectContext[$targetType]" else tq"_root_.upickle.implicits.HugeCaseObjectContext[$targetType]"}(${rawArgs.size}){
..${
for (i <- rawArgs.indices)
yield q"private[this] var ${aggregates(i)}: ${argTypes(i)} = _"
}
for (i <- rawArgs.indices)
yield q"private[this] var ${aggregates(i)}: ${argTypes(i)} = _"
}
def storeAggregatedValue(currentIndex: Int, v: Any): Unit = currentIndex match{
case ..${
for (i <- rawArgs.indices)
yield cq"$i => ${aggregates(i)} = v.asInstanceOf[${argTypes(i)}]"
}
for (i <- rawArgs.indices)
yield cq"$i => ${aggregates(i)} = v.asInstanceOf[${argTypes(i)}]"
}
case _ => throw new java.lang.IndexOutOfBoundsException(currentIndex.toString)
}

def visitKeyValue(s: Any) = {
currentIndex = ${c.prefix}.objectAttributeKeyReadMap(s.toString).toString match {
case ..${
for(i <- mappedArgs.indices)
yield cq"${mappedArgs(i)} => $i"
}
for(i <- mappedArgs.indices)
yield cq"${mappedArgs(i)} => $i"
}
case _ =>
${
allowUnknownKeysAnnotation match {
case None =>
q"""
allowUnknownKeysAnnotation match {
case None =>
q"""
if (${c.prefix}.allowUnknownKeys) -1
else throw new _root_.upickle.core.Abort("Unknown Key: " + s.toString)
"""
case Some(false) => q"""throw new _root_.upickle.core.Abort("Unknown Key: " + s.toString)"""
case Some(true) => q"-1"
}
}
case Some(false) => q"""throw new _root_.upickle.core.Abort("Unknown Key: " + s.toString)"""
case Some(true) => q"-1"
}
}
}
}

def visitEnd(index: Int) = {
..${
for(i <- rawArgs.indices if hasDefaults(i))
yield q"this.storeValueIfNotFound($i, ${defaults(i)})"
}
for(i <- rawArgs.indices if hasDefaults(i))
yield q"this.storeValueIfNotFound($i, ${defaults(i)})"
}

// Special-case 64 because java bit shifting ignores any RHS values above 63
// https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19
if (${
if (rawArgs.length <= 64) q"this.checkErrorMissingKeys(${if (rawArgs.length == 64) -1 else (1L << rawArgs.length) - 1})"
else q"this.checkErrorMissingKeys(${rawArgs.length})"
}){
if (rawArgs.length <= 64) q"this.checkErrorMissingKeys(${if (rawArgs.length == 64) -1 else (1L << rawArgs.length) - 1})"
else q"this.checkErrorMissingKeys(${rawArgs.length})"
}){
this.errorMissingKeys(${rawArgs.length}, ${mappedArgs.toArray})
}
$companion.apply(
..${
for(i <- rawArgs.indices)
yield
if (i == rawArgs.length - 1 && varargs) q"${aggregates(i)}:_*"
else q"${aggregates(i)}"
}
for(i <- rawArgs.indices)
yield
if (i == rawArgs.length - 1 && varargs) q"${aggregates(i)}:_*"
else q"${aggregates(i)}"
}
)
}

def subVisitor: _root_.upickle.core.Visitor[_, _] = currentIndex match{
case -1 => _root_.upickle.core.NoOpVisitor
case ..${
for (i <- rawArgs.indices)
yield cq"$i => ${localReaders(i)} "
}
for (i <- rawArgs.indices)
yield cq"$i => ${localReaders(i)} "
}
case _ => throw new java.lang.IndexOutOfBoundsException(currentIndex.toString)
}
}
Expand Down Expand Up @@ -405,7 +405,7 @@ object Macros {
.map(newTermName(_))
.find(companion.tpe.member(_) != NoSymbol)
.getOrElse(c.abort(c.enclosingPosition, "None of the following methods " +
"were defined: unapply, unapplySeq"))
"were defined: unapply, unapplySeq"))
}

def internal = q"${c.prefix}.Internal"
Expand Down Expand Up @@ -443,13 +443,13 @@ object Macros {
new ${c.prefix}.CaseClassWriter[$targetType]{
def length(v: $targetType) = {
${
Range(0, rawArgs.length)
.map(i =>
if (!hasDefaults(i)) q"1"
else q"""if (${serDfltVals(i)} || v.${TermName(rawArgs(i))} != ${defaults(i)}) 1 else 0"""
)
.foldLeft[Tree](q"0"){case (prev, next) => q"$prev + $next"}
}
Range(0, rawArgs.length)
.map(i =>
if (!hasDefaults(i)) q"1"
else q"""if (${serDfltVals(i)} || v.${TermName(rawArgs(i))} != ${defaults(i)}) 1 else 0"""
)
.foldLeft[Tree](q"0"){case (prev, next) => q"$prev + $next"}
}
}
override def write0[R](out: _root_.upickle.core.Visitor[_, R], v: $targetType): R = {
if (v == null) out.visitNull(-1)
Expand Down Expand Up @@ -478,7 +478,7 @@ object Macros {
val c: c0.type = c0
def typeclass = e2
}.derive(e1.tpe)
// println(res)
// println(res)
c0.Expr[R[T]](res)
}

Expand All @@ -489,7 +489,8 @@ object Macros {
val c: c0.type = c0
def typeclass = e2
}.derive(e1.tpe)
// println(res)
// println(res)
c0.Expr[W[T]](res)
}
}

0 comments on commit fc3ba46

Please sign in to comment.