Added encoder and decoder for Scala Enumeration #325

Merged
merged 2 commits into from Jul 26, 2016

Conversation

Projects
None yet
4 participants
@mariussoutier
Contributor

mariussoutier commented Jul 8, 2016

Added an encoder and a decoder for Scala Enumerations.

Parsing an enumeration value that doesn't exist will result in a DecodingFailure that contains a NoSuchElementException.

To summarize why:

  • In some cases they are actually useful (convenience methods for example)
  • Sometimes you don't have control over how the domain model looks
  • They are a part of the Scala standard lib

Fixes #297

@travisbrown

This comment has been minimized.

Show comment
Hide comment
@travisbrown

travisbrown Jul 9, 2016

Member

Thanks, @mariussoutier. The one change I think we should make is using the Xor to represent parsing failures instead of throwing an exception (which should just take flatMap-ing on the string decoder and using either try or Xor.catchOnly).

Member

travisbrown commented Jul 9, 2016

Thanks, @mariussoutier. The one change I think we should make is using the Xor to represent parsing failures instead of throwing an exception (which should just take flatMap-ing on the string decoder and using either try or Xor.catchOnly).

@jpablo

This comment has been minimized.

Show comment
Hide comment
@jpablo

jpablo Jul 9, 2016

This is what I'm doing:

  def enumDecoder[E <: Enumeration](enum: E) = Decoder.instance { hc =>
    Try(hc.as[String] map (enum.withName(_))) match {
      case Failure(ex) => Xor.left(DecodingFailure(ex.getMessage, hc.history))
      case Success(eVal) => eVal
    }
  }

jpablo commented Jul 9, 2016

This is what I'm doing:

  def enumDecoder[E <: Enumeration](enum: E) = Decoder.instance { hc =>
    Try(hc.as[String] map (enum.withName(_))) match {
      case Failure(ex) => Xor.left(DecodingFailure(ex.getMessage, hc.history))
      case Success(eVal) => eVal
    }
  }
@mariussoutier

This comment has been minimized.

Show comment
Hide comment
@mariussoutier

mariussoutier Jul 11, 2016

Contributor

Alright, updated the PR.

Contributor

mariussoutier commented Jul 11, 2016

Alright, updated the PR.

@codecov-io

This comment has been minimized.

Show comment
Hide comment
@codecov-io

codecov-io Jul 25, 2016

Current coverage is 80.54% (diff: 100%)

Merging #325 into master will increase coverage by 0.41%

Sunburst

Powered by Codecov. Last update b8214a6...fd48575

codecov-io commented Jul 25, 2016

Current coverage is 80.54% (diff: 100%)

Merging #325 into master will increase coverage by 0.41%

Sunburst

Powered by Codecov. Last update b8214a6...fd48575

@travisbrown travisbrown merged commit 79998b2 into circe:master Jul 26, 2016

3 checks passed

codecov/patch 100% of diff hit (target 80.12%)
Details
codecov/project 80.54% (+0.41%) compared to b8214a6
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment