scodec/scodec — pull request #6 by pchiusano (2014-02-15 05:05:48+00:00)
Title: Discriminator syntax made much nicer

Here's the new syntax for `stdEither`:

```Scala
def stdEither[L, R](indicator: Codec[Boolean], left: Codec[L], right: Codec[R]): Codec[Either[L,R]] =
discriminated[Either[L,R]].by(indicator)
.| (false) { case Left(l) => l } (Left.apply) (left)
.| (true) { case Right(r) => r } (Right.apply) (right)
.build
```

Scala didn't cooperate as much as I would have liked - I would have preferred to be able to say `Left.unapply` instead of having to expand the pattern, but alas, that didn't work out [because of this issue](https://gist.github.com/pchiusano/9014455). But overall, very readable. Interesting, there is no requirement that the cases be extracted to a subtype of the base codec type. You could have a case that extracts a `String` from a `Either[A, List[String]]`.

Other changes - I got rid of `EitherCodec` as a separate class - it is just defined inline in `codecs`. Also got rid of the old `Discriminator` type - there is now just `DiscriminatorBuilder`, which you build up using various combinators, then call `build` to get a `Codec[A]`. `DiscriminatorCodec` still exists but is private, so I moved its documentation to the `codecs.discriminated` function, which is the entry point for this little DSL.

As part of this, made some miscellaneous related changes, like adding `pxmap` and `pcontramap`. Also added some property based tests to `EitherTest`. All the tests compile and run successfully.