Coder Social home page Coder Social logo

scala-xml-encoder's Introduction

Scala Xml Encoder: Making it more fun to encode XML with scala

A tiny micro-library for encoding scala-xml with zero dependencies (outside of scala-xml itself)

See blog post //TODO BEFORE PUBLISHING ARTIFACTS

How do I use this to produce XML

Import it

import codes.mostly.xml.XmlSugar._

Define your encoders:

case class Employee(name: String, hasProvidedTaxCode: Boolean)
case class TerribleAccountingSoftware(employees: Option[List[Set[Seq[Option[List[Employee]]]]]]) // Hopefully a contrived example

implicit val developerEncoder: XmlEncoder[Employee] = employee =>
  <emp>
    <name>{employee.name}</name>
    <isPayingTax>{employee.hasProvidedTaxCode.toString}</isPayingTax>
  </emp>

implicit val accountingSoftwareEncoder: XmlEncoder[TerribleAccountingSoftware] = software =>
  <accountingSofware3000>
    <employees>{software.employees.asXml}</employees>
  </accountingSofware3000>

And produce XML:

myAccountingSoftware.asTopLevelXml match {
    Left(error: String)         => ??? // in case you goofed and didn't produce EXACTLY one xml element
    Right(elem: scala.xml.Elem) => ??? // here's your valid XML element
}

(here's this example written as a unit test)

That's it!

Usage examples

What's the design idea here

To make writing XML more fun and less annoying by using implicit encoders which is inspired by circe, but a little simpler and a lot less grand in scope.

The general concepts are:

  1. Put implicit XML encoders for your models into scope
  2. Once you have that, define your XML using standard scala-xml syntax .asXml to encode the child types.
  3. Empty collections means no XML elements are produced

I use Cats, are there encoders for [name_of_thing_here]

I like cats too, but I didn't want to add more dependencies to this micro library.

In general, if you need to add encoders for any structural types, the easiest thing is to convert them to a list and call .asXml on them. If you for instance wanted NonEmptyList and NonEmptySet encoders, you could do:

import codes.mostly.xml.XmlSugar._

implicit def encodeNEL[A](implicit aEnc: XmlEncoder[A]): XmlEncoder[NonEmptyList[A]] = nel => nel.toList.toXml
implicit def encodeNES[A](implicit aEnc: XmlEncoder[A]): XmlEncoder[NonEmptySet[A]]  = nes => nes.toNonEmptyList.toXml

And now you can encode to your heart's content:

case class Person(name: String, hobbies: NonEmptySet[Hobby], jobs: NonEmptyList[Job])
implicit val personEncoder: XmlEncoder[Person] = p => 
    <person name={p.name}>
       {p.hobbies.asXml}  <!-- assuming an XmlEncoder[Hobby] exists in scope -->
       {p.jobs.asXml}     <!-- assuming an XmlEncoder[Job] exists in scope -->
    </person>

Limitations of this library

  • No de-coding of XML
  • Automatically deriving XML based on your case classes is out of scope for this project.
  • It's assumed your encoders are always deterministic - this won't catch any errors thrown or raised during encoding. So don't do that.

scala-xml-encoder's People

Contributors

scala-steward avatar tobiasroland avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.