Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.avsystem.commons.serialization

import org.scalatest.funsuite.AnyFunSuite

final class NotUsedTransientDefault extends AnyFunSuite {
case class Valid(@transientDefault a: String = "default")
case class Invalid(@transientDefault a: String)

test("no warnings when @transientDefault is used properly") {
assertCompiles(
//language=Scala
s"""
|GenCodec.materialize[Valid]
|""".stripMargin
)
}

test("warnings when missing default value") {
assertDoesNotCompile(
//language=Scala
s"""
|GenCodec.materialize[Invalid]
|""".stripMargin
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ class GenCodecMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) with
}
}

def isTransientDefault(param: ApplyParam): Boolean =
param.defaultValue.nonEmpty && hasAnnotation(param.sym, TransientDefaultAnnotType)

def isTransientDefault(param: ApplyParam, warnIfDefaultNotProvided: Boolean = false): Boolean =
(hasAnnotation(param.sym, TransientDefaultAnnotType), param.defaultValue.nonEmpty, warnIfDefaultNotProvided) match {
case (true, false, true) =>
error(s"@transientDefault has no effect on parameter ${param.sym.name} because it has no default value")
false
case (hasAnnotation, noDefaultValue, _) => hasAnnotation && noDefaultValue
}
def isOptimizedPrimitive(param: ApplyParam): Boolean = {
val vt = param.valueType
vt =:= typeOf[Boolean] || vt =:= typeOf[Int] || vt =:= typeOf[Long] || vt =:= typeOf[Double]
Expand Down Expand Up @@ -251,13 +255,13 @@ class GenCodecMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) with
}

def anyParamHasTransientDefault: Boolean =
params.exists(isTransientDefault)
params.exists(isTransientDefault(_))

def isOptionLike(p: ApplyParam): Boolean =
p.optionLike.nonEmpty

def mayBeTransient(p: ApplyParam): Boolean =
isOptionLike(p) || isTransientDefault(p)
isOptionLike(p) || isTransientDefault(p, warnIfDefaultNotProvided = true)

def transientValue(p: ApplyParam): Tree = p.optionLike match {
case Some(optionLike) => q"${optionLike.reference(Nil)}.none"
Expand Down Expand Up @@ -614,7 +618,7 @@ class GenCodecMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) with
val deps = new mutable.ListBuffer[Tree]

ttpe.members.iterator.foreach { getter =>
if(getter.isMethod && isJavaGetter(getter.asMethod)) {
if (getter.isMethod && isJavaGetter(getter.asMethod)) {
val propType = getter.typeSignatureIn(ttpe).finalResultType
val getterName = getter.name.decodedName.toString
val setterName = getterName.replaceFirst("^(get|is)", "set")
Expand Down