Skip to content

Commit

Permalink
Merge pull request #332 from fd4s/chain
Browse files Browse the repository at this point in the history
Deduplicate chain code
  • Loading branch information
bplommer authored Apr 30, 2021
2 parents 8dad83a + 6f9e14d commit ae2197c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
12 changes: 1 addition & 11 deletions modules/core/src/main/scala/vulcan/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,7 @@ object Codec extends CodecCompanionCompat {
implicit final def chain[A](
implicit codec: Codec[A]
): Codec.Aux[java.util.List[codec.Repr], Chain[A]] =
Codec.instanceForTypes(
"Collection",
"Chain",
codec.schema.map(Schema.createArray),
_.toList.traverse(codec.encode(_)).map(_.asJava), {
case (collection: java.util.Collection[_], schema) =>
collection.asScala.toList
.traverse(codec.decode(_, schema.getElementType()))
.map(Chain.fromSeq)
}
)
Codec.list[A].imap(Chain.fromSeq)(_.toList).withTypeName("Chain")

/**
* @group General
Expand Down
13 changes: 7 additions & 6 deletions modules/core/src/test/scala/vulcan/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
assertDecodeError[Chain[Int]](
unsafeEncode(Chain(1, 2, 3)),
unsafeSchema[Int],
"Error decoding Chain: Got unexpected schema type INT, expected schema type ARRAY"
"Error decoding Chain: Error decoding List: Got unexpected schema type INT, expected schema type ARRAY"
)
}

it("should error if value is not collection") {
assertDecodeError[Chain[Int]](
unsafeEncode(10),
unsafeSchema[Chain[Int]],
"Error decoding Chain: Got unexpected type java.lang.Integer, expected type Collection"
"Error decoding Chain: Error decoding List: Got unexpected type java.lang.Integer, expected type Collection"
)
}

Expand Down Expand Up @@ -1138,7 +1138,9 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
val value = LocalTime.now()
assertEncodeIs[LocalTime](
value,
Right(java.lang.Integer.valueOf(TimeUnit.NANOSECONDS.toMillis(value.toNanoOfDay()).toInt))
Right(
java.lang.Integer.valueOf(TimeUnit.NANOSECONDS.toMillis(value.toNanoOfDay()).toInt)
)
)
}
}
Expand Down Expand Up @@ -1233,7 +1235,6 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
}
}


describe("long") {
describe("schema") {
it("should be encoded as long") {
Expand Down Expand Up @@ -1415,15 +1416,15 @@ final class CodecSpec extends BaseSpec with CodecSpecHelpers {
assertDecodeError[NonEmptyChain[Int]](
unsafeEncode(NonEmptyChain(1, 2, 3)),
unsafeSchema[Int],
"Error decoding NonEmptyChain: Error decoding Chain: Got unexpected schema type INT, expected schema type ARRAY"
"Error decoding NonEmptyChain: Error decoding Chain: Error decoding List: Got unexpected schema type INT, expected schema type ARRAY"
)
}

it("should error if value is not collection") {
assertDecodeError[NonEmptyChain[Int]](
unsafeEncode(10),
unsafeSchema[NonEmptyChain[Int]],
"Error decoding NonEmptyChain: Error decoding Chain: Got unexpected type java.lang.Integer, expected type Collection"
"Error decoding NonEmptyChain: Error decoding Chain: Error decoding List: Got unexpected type java.lang.Integer, expected type Collection"
)
}

Expand Down

0 comments on commit ae2197c

Please sign in to comment.