Skip to content

Commit

Permalink
Added support to DefType Poly and Enum to include className
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Sep 6, 2024
1 parent 9c88e04 commit 8e0b5b9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
20 changes: 9 additions & 11 deletions core/shared/src/main/scala/fabric/define/DefType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ object DefType {
case Int => obj("type" -> "numeric", "precision" -> "integer")
case Dec => obj("type" -> "numeric", "precision" -> "decimal")
case Bool => obj("type" -> "boolean")
case Enum(values) => obj("type" -> "enum", "values" -> values)
case Poly(values) => obj(
case Enum(values, cn) => obj("type" -> "enum", "values" -> values, "className" -> cn.json)
case Poly(values, cn) => obj(
"type" -> "poly",
"values" -> values.map { case (key, dt) => key -> dt2V(dt) }
"values" -> values.map { case (key, dt) => key -> dt2V(dt) },
"className" -> cn.json
)
case Json => obj("type" -> "json")
case Null => obj("type" -> "null")
Expand Down Expand Up @@ -107,8 +108,9 @@ object DefType {
case "decimal" => Dec
}
case "boolean" => Bool
case "enum" => Enum(o.value("values").asVector.toList)
case "poly" => Poly(o.value("values").asMap.map { case (key, json) => key -> v2dt(json) })
case "enum" => Enum(o.value("values").asVector.toList, o.get("className").map(_.asString))
case "poly" =>
Poly(o.value("values").asMap.map { case (key, json) => key -> v2dt(json) }, o.get("className").map(_.asString))
case "json" => Json
case "null" => Null
}
Expand Down Expand Up @@ -205,14 +207,10 @@ object DefType {

override protected def template(path: JsonPath, config: TemplateConfig): Json = config.json(path)
}
case class Enum(values: List[Json]) extends DefType {
override def className: Option[String] = None

case class Enum(values: List[Json], className: Option[String]) extends DefType {
override protected def template(path: JsonPath, config: TemplateConfig): Json = config.`enum`(path, values)
}
case class Poly(values: Map[String, DefType]) extends DefType {
override def className: Option[String] = None

case class Poly(values: Map[String, DefType], className: Option[String]) extends DefType {
override protected def template(path: JsonPath, config: TemplateConfig): Json = values.head._2.template(path, config)
}
case object Null extends DefType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ object FabricGenerator {
case DefType.Int => "Long"
case DefType.Dec => "BigDecimal"
case DefType.Bool => "Boolean"
case DefType.Enum(_) => throw new RuntimeException("Unsupported")
case DefType.Poly(_) => throw new RuntimeException("Unsupported")
case DefType.Enum(_, _) => throw new RuntimeException("Unsupported")
case DefType.Poly(_, _) => throw new RuntimeException("Unsupported")
case DefType.Json => "Json"
case DefType.Null => throw new RuntimeException(
"Null type found in definition! Not supported for code generation!"
Expand Down
24 changes: 16 additions & 8 deletions core/shared/src/main/scala/fabric/rw/RW.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ object RW extends CompileRW {
def enumeration[T](
list: List[T],
asString: T => String = (t: T) => defaultClassNameMapping(t.getClass.getName),
caseSensitive: Boolean = false
caseSensitive: Boolean = false,
className: Option[String] = None
): RW[T] = new RW[T] {
private def fixString(s: String): String = if (caseSensitive) s else s.toLowerCase

Expand All @@ -57,7 +58,7 @@ object RW extends CompileRW {

override def read(t: T): Json = str(asString(t))

override val definition: DefType = DefType.Enum(list.map(t => Str(asString(t))))
override val definition: DefType = DefType.Enum(list.map(t => Str(asString(t))), className)
}

def string[T](asString: T => String, fromString: String => T): RW[T] = new RW[T] {
Expand Down Expand Up @@ -96,7 +97,11 @@ object RW extends CompileRW {
* @param types
* a list of tuples with the type names associated with their RW
*/
def poly[P](fieldName: String = "type", classNameMapping: String => String = defaultClassNameMapping)(
def poly[P](
fieldName: String = "type",
classNameMapping: String => String = defaultClassNameMapping,
className: Option[String] = None
)(
types: RW[_ <: P]*
): RW[P] = {
def typeName(rw: RW[_ <: P]): String = {
Expand All @@ -123,11 +128,14 @@ object RW extends CompileRW {
)
}
},
d = DefType.Poly(types.map { rw =>
val obj = rw.definition.asInstanceOf[DefType.Obj]
val key = typeName(rw)
key -> obj
}.toMap)
d = DefType.Poly(
types.map { rw =>
val obj = rw.definition.asInstanceOf[DefType.Obj]
val key = typeName(rw)
key -> obj
}.toMap,
className
)
)
}

Expand Down
3 changes: 2 additions & 1 deletion core/shared/src/test/scala/spec/FabricSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ class FabricSpec extends AnyWordSpec with Matchers {
),
"className" -> "spec.Polymorphic.PolyValue"
)
)
),
"className" -> Null
)
)
}
Expand Down

0 comments on commit 8e0b5b9

Please sign in to comment.