Skip to content

Commit 58a534a

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

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
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,22 @@ object Magnolia {
127127
val SubtypeTpe = typeOf[Subtype[Any, Any]].typeConstructor
128128
val TypeNameObj = reify(magnolia1.TypeName).tree
129129

130+
def assertFieldsLimits(caseClassSym: TypeSymbol): Unit = {
131+
val minLimit = config.map(_.minFields).getOrElse(-1)
132+
val maxLimit = config.map(_.maxFields).getOrElse(Int.MaxValue)
133+
val fieldsNumber = caseClassSym.info.members.size
134+
135+
if (minLimit > -1 && fieldsNumber < minLimit) {
136+
error(s"Case class ${caseClassSym.name} has $fieldsNumber fields which is less than required minimum: $minLimit")
137+
}
138+
139+
if (fieldsNumber > maxLimit) {
140+
error(s"Case class ${caseClassSym.name} has $fieldsNumber fields which is above the required maximum: $maxLimit")
141+
}
142+
}
143+
144+
assertFieldsLimits(CaseClassSym)
145+
130146
val debug = c.macroApplication.symbol.annotations
131147
.find(_.tree.tpe <:< DebugTpe)
132148
.flatMap(_.tree.children.tail.collectFirst {

0 commit comments

Comments
 (0)