From 6e11bd15cb6836fac265c04eb15239e5640ed44b Mon Sep 17 00:00:00 2001 From: Grigory Pomadchin Date: Sun, 10 Apr 2022 07:06:47 -0400 Subject: [PATCH] Improve HShow derivation --- .../hiveless/serializers/UnaryDeserializer.scala | 2 +- .../scala/com/azavea/hiveless/utils/HShow.scala | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/com/azavea/hiveless/serializers/UnaryDeserializer.scala b/core/src/main/scala/com/azavea/hiveless/serializers/UnaryDeserializer.scala index eee3665..a82c5f4 100644 --- a/core/src/main/scala/com/azavea/hiveless/serializers/UnaryDeserializer.scala +++ b/core/src/main/scala/com/azavea/hiveless/serializers/UnaryDeserializer.scala @@ -42,7 +42,7 @@ object UnaryDeserializer extends Serializable { extends Errors( s""" |${clz.getName}: could not deserialize the $name input argument: - |should match one of the following types: ${HShow[T].show().trim.init}""".stripMargin + |should match one of the following types: ${HShow[T].show()}""".stripMargin ) } diff --git a/core/src/main/scala/com/azavea/hiveless/utils/HShow.scala b/core/src/main/scala/com/azavea/hiveless/utils/HShow.scala index 084c1c7..555b98e 100644 --- a/core/src/main/scala/com/azavea/hiveless/utils/HShow.scala +++ b/core/src/main/scala/com/azavea/hiveless/utils/HShow.scala @@ -33,12 +33,18 @@ object HShow extends LowPriorityHShow { () => sl.show() /** Derive HShow for HList. */ - implicit val hshowHNil: HShow[HNil] = () => "" - implicit def hshowHCons[H: ClassTag, T <: HList](implicit sh: HShow[H], st: HShow[T]): HShow[H :: T] = () => s"${sh.show()}, ${st.show()}" + implicit val hshowHNil: HShow[HNil] = () => "" + implicit def hshowHCons[H: ClassTag, T <: HList](implicit sh: HShow[H], st: HShow[T]): HShow[H :: T] = () => { + val (h, t) = (sh.show(), st.show()) + if (t.isEmpty) h else s"$h, $t" + } /** Derive HShow for Coproduct. */ - implicit val hshowCNil: HShow[CNil] = () => "" - implicit def hshowCCons[H: ClassTag, T <: Coproduct](implicit sh: HShow[H], st: HShow[T]): HShow[H :+: T] = () => s"${sh.show()}, ${st.show()}" + implicit val hshowCNil: HShow[CNil] = () => "" + implicit def hshowCCons[H: ClassTag, T <: Coproduct](implicit sh: HShow[H], st: HShow[T]): HShow[H :+: T] = () => { + val (h, t) = (sh.show(), st.show()) + if (t.isEmpty) h else s"$h, $t" + } } trait LowPriorityHShow extends Serializable {