-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Krzysztof Palcowski
committed
Mar 11, 2024
1 parent
95fd5da
commit 7f6fb86
Showing
4 changed files
with
52 additions
and
17 deletions.
There are no files selected for viewing
34 changes: 29 additions & 5 deletions
34
core/src/main/scala/pl/iterators/baklava/core/model/RouteParameterRepresentation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
package pl.iterators.baklava.core.model | ||
|
||
import scala.reflect.runtime.universe.TypeTag | ||
import scala.util.Random | ||
|
||
case class RouteParameterRepresentation[T]( | ||
name: String, | ||
required: Boolean, | ||
sampleValue: T, | ||
marshaller: T => String, | ||
valueGenerator: () => T, | ||
marshaller: T => String = RouteParameterRepresentation.marshaller[T] _, | ||
required: Boolean = false, | ||
seq: Boolean = false, | ||
seqMin: Int = 2, | ||
seqMax: Int = 5, | ||
enumValues: Option[Seq[T]] = None | ||
)(implicit typeTag: TypeTag[T]) { | ||
|
||
lazy val marshall: String = marshaller(sampleValue) | ||
lazy val sampleValue: T = | ||
if (!seq) valueGenerator() else sys.error("sampleValue should not be called on seq parameter") | ||
|
||
lazy val seqSampleValue: Seq[T] = | ||
if (seq) (0 until Random.nextInt(1 + seqMax - seqMin) + seqMin).map(_ => valueGenerator()) | ||
else sys.error("seqSampleValue should not be called on seq parameter") | ||
|
||
lazy val enums: Option[Seq[String]] = enumValues.map(values => values.map(marshaller)) | ||
|
||
lazy val scalaType: String = typeTag.tpe.toString | ||
lazy val simpleType: String = typeTag.tpe.toString | ||
|
||
lazy val queryString: String = | ||
if (seq) { | ||
seqSampleValue.map(v => s"$name=${marshaller(v)}").mkString("&") | ||
} else | ||
s"$name=${marshaller(sampleValue)}" | ||
|
||
} | ||
|
||
object RouteParameterRepresentation { | ||
def marshaller[T](value: T): String = | ||
value match { | ||
case string: String => string | ||
case other => other.toString | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters