Scalaxb v1.8.0 Release Notes

Release Date: 2020-09-09 // over 3 years ago
  • scalaxb 1.8.0 adds Monocle Lens generation feature.

    setup

    Using the sbt-scalaxb this can be enabled as:

    val monocleCore = "com.github.julien-truffaut" %% "monocle-core"% "2.0.3"// val monocleMacro = "com.github.julien-truffaut" %% "monocle-macro" % "2.0.3"val scalaXml = "org.scala-lang.modules" %% "scala-xml" % "1.3.0"val scalaParser = "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2"val dispatchV = "0.12.0"val dispatch = "net.databinder.dispatch" %% "dispatch-core" % dispatchVThisBuild / organization := "com.example"ThisBuild / scalaVersion := "2.12.8"lazy val root = (project in file(".")) .enablePlugins(ScalaxbPlugin) .settings( name := "foo", libraryDependencies ++= Seq(dispatch, scalaXml, scalaParser, monocleCore), Compile / scalaxb / scalaxbDispatchVersion := dispatchV, Compile / scalaxb / scalaxbPackageName := "com.example.ipo", Compile / scalaxb / scalaxbGenerateLens := true, Compile / scalaxb / scalaxbUseLists := true, )
    

    generated code

    This will generate Lenses in the companion objects as follows:

    case class PurchaseOrderType(shipTo: ipo.Addressable, billTo: ipo.Addressable, comment: Option[String] = None, items: ipo.Items, attributes: Map[String, scalaxb.DataRecord[Any]] = Map.empty) { lazy val orderDate = attributes.get("@orderDate") map { \_.as[javax.xml.datatype.XMLGregorianCalendar]} }object PurchaseOrderType { def shipTo: monocle.Lens[PurchaseOrderType, ipo.Addressable] = monocle.Lens[PurchaseOrderType, ipo.Addressable](\_.shipTo)((\_shipTo: ipo.Addressable) =\> (purchaseordertype: PurchaseOrderType) =\> purchaseordertype.copy(shipTo = \_shipTo)) def billTo: monocle.Lens[PurchaseOrderType, ipo.Addressable] = monocle.Lens[PurchaseOrderType, ipo.Addressable](\_.billTo)((\_billTo: ipo.Addressable) =\> (purchaseordertype: PurchaseOrderType) =\> purchaseordertype.copy(billTo = \_billTo)) def comment: monocle.Lens[PurchaseOrderType, Option[String]] = monocle.Lens[PurchaseOrderType, Option[String]](\_.comment)((\_comment: Option[String]) =\> (purchaseordertype: PurchaseOrderType) =\> purchaseordertype.copy(comment = \_comment)) def items: monocle.Lens[PurchaseOrderType, ipo.Items] = monocle.Lens[PurchaseOrderType, ipo.Items](\_.items)((\_items: ipo.Items) =\> (purchaseordertype: PurchaseOrderType) =\> purchaseordertype.copy(items = \_items)) def attributes: monocle.Lens[PurchaseOrderType, Map[String, scalaxb.DataRecord[Any]]] = monocle.Lens[PurchaseOrderType, Map[String, scalaxb.DataRecord[Any]]](\_.attributes)((\_attributes: Map[String, scalaxb.DataRecord[Any]]) =\> (purchaseordertype: PurchaseOrderType) =\> purchaseordertype.copy(attributes = \_attributes)) implicit class PurchaseOrderTypeW[A](l: monocle.Lens[A, PurchaseOrderType]) { def shipTo: monocle.Lens[A, ipo.Addressable] = l composeLens PurchaseOrderType.shipTo def billTo: monocle.Lens[A, ipo.Addressable] = l composeLens PurchaseOrderType.billTo def comment: monocle.Lens[A, Option[String]] = l composeLens PurchaseOrderType.comment def items: monocle.Lens[A, ipo.Items] = l composeLens PurchaseOrderType.items def attributes: monocle.Lens[A, Map[String, scalaxb.DataRecord[Any]]] = l composeLens PurchaseOrderType.attributes } }case class Items(item: List[ipo.Item] = Nil) object Items { def item: monocle.Lens[Items, List[ipo.Item]] = monocle.Lens[Items, List[ipo.Item]](\_.item)((\_item: List[ipo.Item]) =\> (items: Items) =\> items.copy(item = \_item)) implicit class ItemsW[A](l: monocle.Lens[A, Items]) { def item: monocle.Lens[A, List[ipo.Item]] = l composeLens Items.item } }
    

    usage

    Here's an example of how the lenses can be used:

    scala\> import com.example.ipo.\_import com.example.ipo.\_scala\> val items = Items(Item("a", BigInt(0), BigDecimal(0)) :: Nil) items: com.example.ipo.Items = Items(List(Item(a,0,0,None,None,Map()))) scala\> val po = PurchaseOrderType(Address("", "", ""), Address("", "", ""), None, items) po: com.example.ipo.PurchaseOrderType = PurchaseOrderType(Address(,,),Address(,,),None,Items(List(Item(a,0,0,None,None,Map()))),Map()) scala\> PurchaseOrderType.items.item.set(Item("b", BigInt(0), BigDecimal(0)) :: Nil)(po) res0: com.example.ipo.PurchaseOrderType = PurchaseOrderType(Address(,,),Address(,,),None,Items(List(Item(b,0,0,None,None,Map()))),Map())
    

    In the above, PurchaseOrderType.items.item zooms into the item field of the purchase order's items field and replaces the value.

    This was implemented by @eed3si9n as #535, based on #292 contributed by @lbruand.


Previous changes from v1.7.5

    • Populates xsi:type attribute on toXML(...) #518 by @abestel
    • ➕ Adds scalaxbUseLists setting to generate List[A] instead of Seq[A] #526 by @pgrandjean
    • 🛠 Fixes code generation to avoid multi-arg infix syntax #531 by @margussipria
    • Splits generated fromString method for big enums that would otherwise not compile #517 by @LolHens