Skip to content

Commit

Permalink
Improvements to class name mapping in DefType when using values with …
Browse files Browse the repository at this point in the history
…a space.
  • Loading branch information
darkfrog26 committed Feb 19, 2024
1 parent d3a439c commit f649f91
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
20 changes: 8 additions & 12 deletions core/shared/src/main/scala/fabric/Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -544,20 +544,16 @@ case class Str(value: String, reference: Option[Any] = None) extends Json {
override def isEmpty: Boolean = value.isEmpty

override def asType[V <: Json](`type`: JsonType[V]): V = `type` match {
case JsonType.Bool => Try(Bool(value.toBoolean)).toOption
.map(_.asInstanceOf[V])
.getOrElse(
throw ConversionException(
s"$value is a Str and can't be converted to Bool"
)
case JsonType.Bool => Try(Bool(value.toBoolean)).toOption.getOrElse(
throw ConversionException(
s"$value is a Str and can't be converted to Bool"
)
case JsonType.NumInt => Try(NumInt(value.toLong)).toOption
.map(_.asInstanceOf[V])
.getOrElse(
throw ConversionException(
s"$value is a Str and can't be converted to NumInt"
)
)
case JsonType.NumInt => Try(NumInt(value.toLong)).toOption.getOrElse(
throw ConversionException(
s"$value is a Str and can't be converted to NumInt"
)
)
case JsonType.NumDec | JsonType.Num => Try(NumDec(BigDecimal(value))).toOption
.map(_.asInstanceOf[V])
.getOrElse(
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/fabric/rw/RW.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object RW extends CompileRW {

def enumeration[T](
list: List[T],
asString: T => String = (t: T) => t.getClass.getSimpleName.replace("$", ""),
asString: T => String = (t: T) => defaultClassNameMapping(t.getClass.getName),
caseSensitive: Boolean = false
): RW[T] = new RW[T] {
private def fixString(s: String): String = if (caseSensitive) s else s.toLowerCase
Expand Down Expand Up @@ -131,7 +131,7 @@ object RW extends CompileRW {
)
}

def cleanClassName(className: String): String = className.replace("$", ".") match {
def cleanClassName(className: String): String = className.replace("$u0020", " ").replace("$", ".") match {
case s if s.endsWith(".") => s.substring(0, s.length - 1)
case s => s
}
Expand Down
20 changes: 13 additions & 7 deletions core/shared/src/test/scala/spec/RWSpecAuto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,19 @@ class RWSpecAuto extends AnyWordSpec with Matchers {
)
)
}
// TODO: Enable once Scala 3 support for sealed traits is working
// "supporting sealed traits" in {
// val car: VehicleType = VehicleType.Car
// car.json should be(Str("Car"))
// "SUV".json.as[VehicleType] should be(VehicleType.SUV)
// }

"supporting sealed traits" in {
val car: VehicleType = VehicleType.Car
car.json should be(Str("Car"))
"SUV".json.as[VehicleType] should be(VehicleType.SUV)
VehicleType.rw.definition.asInstanceOf[DefType.Enum].values should be(
List[Json](
"Car",
"SUV",
"Truck",
"Mini Van"
)
)
}
"work properly with nulls and defaults" in {
val json = obj(
"offset" -> Null,
Expand Down
28 changes: 15 additions & 13 deletions core/shared/src/test/scala/spec/VehicleType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//package spec
//
//import fabric.rw.RW
//
//trait VehicleType
//
//object VehicleType {
// implicit val rw: RW[VehicleType] = RW.gen
//
// case object Car extends VehicleType
// case object SUV extends VehicleType
// case object Truck extends VehicleType
//}
package spec

import fabric.rw.RW

trait VehicleType

object VehicleType {
// TODO: Switch to RW.gen once Scala 3 has support for sealed traits
implicit val rw: RW[VehicleType] = RW.enumeration(List(Car, SUV, Truck, `Mini Van`))

case object Car extends VehicleType
case object SUV extends VehicleType
case object Truck extends VehicleType
case object `Mini Van` extends VehicleType
}

0 comments on commit f649f91

Please sign in to comment.