Skip to content

Commit f66cff9

Browse files
committed
Adds assertion on min/max of fields in case class
1 parent 4634e1f commit f66cff9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

core/src/main/scala/magnolia1/magnolia.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ object Magnolia {
127127
val SubtypeTpe = typeOf[Subtype[Any, Any]].typeConstructor
128128
val TypeNameObj = reify(magnolia1.TypeName).tree
129129

130+
def assertFieldsLimits(caseClassParameters: List[TermSymbol]): Unit = {
131+
val minLimit = config.map(_.minFields).getOrElse(-1)
132+
val maxLimit = config.map(_.maxFields).getOrElse(-1)
133+
val fieldsNumber = caseClassParameters.size
134+
135+
if (minLimit > -1 && fieldsNumber < minLimit) {
136+
error(s"Case class ${genericSymbol.name} has $fieldsNumber fields which is less than required minimum: $minLimit")
137+
}
138+
139+
if (maxLimit > -1 && fieldsNumber > maxLimit) {
140+
error(s"Case class ${genericSymbol.name} has $fieldsNumber fields which is above the required maximum: $maxLimit")
141+
}
142+
}
143+
130144
val debug = c.macroApplication.symbol.annotations
131145
.find(_.tree.tpe <:< DebugTpe)
132146
.flatMap(_.tree.children.tail.collectFirst {
@@ -426,6 +440,8 @@ object Magnolia {
426440
}
427441
)
428442

443+
assertFieldsLimits(caseClassParameters)
444+
429445
val (factoryObject, factoryMethod) = {
430446
if (isReadOnly && isValueClass) ReadOnlyParamObj -> TermName("valueParam")
431447
else if (isReadOnly) ReadOnlyParamObj -> TermName("apply")

examples/src/main/scala/magnolia1/examples/csv.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package magnolia1.examples
22

3-
import magnolia1.{CaseClass, Config, Magnolia, SealedTrait}
3+
import magnolia1._
44

55
import scala.language.experimental.macros
66

@@ -12,9 +12,9 @@ object CsvConfig extends Config {
1212
type Proxy = Csv.type
1313
type Ignore = transient
1414
final val readOnly = true
15-
final val minFields = 0
15+
final val minFields = -1
1616
final val maxFields = -1
17-
final val minCases = 0
17+
final val minCases = -1
1818
final val maxCases = -1
1919
}
2020

0 commit comments

Comments
 (0)