Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to enum decoding behaviour #270

Merged
merged 3 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions modules/core/src/main/scala/vulcan/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,13 @@ object Codec extends CodecCompanionCompat {
else
Left(AvroError.encodeSymbolNotInSchema(symbol, symbols, typeName))
}, {
case (genericEnum: GenericEnumSymbol[_], schema) =>
val symbols = schema.getEnumSymbols().asScala.toList
case (genericEnum: GenericEnumSymbol[_], _) =>
val symbol = genericEnum.toString()

if (symbols.contains(symbol))
decode(symbol)
else
Left(AvroError.decodeSymbolNotInSchema(symbol, symbols, typeName))
default.toRight(AvroError.decodeSymbolNotInSchema(symbol, symbols, typeName))
}
)
}
Expand Down
18 changes: 15 additions & 3 deletions modules/core/src/test/scala/vulcan/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,26 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
)
}

it("should error if encoded value is not a schema symbol") {
assertDecodeError[SealedTraitEnum](
it("should return default value if encoded value is not a schema symbol") {
assertDecodeIs[SealedTraitEnum](
new GenericData.EnumSymbol(
SchemaFactory.enumeration("vulcan.examples.SealedTraitEnum", Array("symbol")),
"symbol"
),
Right(FirstInSealedTraitEnum),
Some(unsafeSchema[SealedTraitEnum])
)
}

it("should error if encoded value is not a schema symbol and there is no default value") {
assertDecodeError[SealedTraitEnumNoDefault](
new GenericData.EnumSymbol(
SchemaFactory
.enumeration("vulcan.examples.SealedTraitEnumNoDefault", Array("symbol")),
"symbol"
),
unsafeSchema[SealedTraitEnum],
"symbol is not part of schema symbols [first, second] for type vulcan.examples.SealedTraitEnum"
"symbol is not part of schema symbols [first, second] for type vulcan.examples.SealedTraitEnumNoDefault"
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package vulcan.examples

import cats.Eq
import org.scalacheck.{Arbitrary, Gen}
import vulcan.{AvroError, Codec, Props}

sealed trait SealedTraitEnumNoDefault

object SealedTraitEnumNoDefault {
implicit final val arbitrary: Arbitrary[SealedTraitEnumNoDefault] =
Arbitrary(Gen.oneOf(FirstInSealedTraitEnumNoDefault, SecondInSealedTraitEnumNoDefault))

implicit final val eq: Eq[SealedTraitEnumNoDefault] =
Eq.fromUniversalEquals

implicit final val codec: Codec[SealedTraitEnumNoDefault] =
Codec.enumeration(
name = "SealedTraitEnumNoDefault",
namespace = "vulcan.examples",
symbols = List("first", "second"),
aliases = List("first", "second"),
doc = Some("documentation"),
default = None,
encode = {
case FirstInSealedTraitEnumNoDefault => "first"
case SecondInSealedTraitEnumNoDefault => "second"
},
decode = {
case "first" => Right(FirstInSealedTraitEnumNoDefault)
case "second" => Right(SecondInSealedTraitEnumNoDefault)
case other => Left(AvroError(other))
},
props = Props.one("custom1", 10).add("custom2", "value2")
)
}

case object FirstInSealedTraitEnumNoDefault extends SealedTraitEnumNoDefault

case object SecondInSealedTraitEnumNoDefault extends SealedTraitEnumNoDefault