Skip to content

Commit

Permalink
Fix plain text input encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpdaniels committed Jan 22, 2021
1 parent 52c9659 commit 9b0ce0e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ inThisBuild(
)

scalaVersion in ThisBuild := mainScala
gitVersioningSnapshotLowerBound in ThisBuild := "0.4.0"
gitVersioningSnapshotLowerBound in ThisBuild := "0.8.0"

val circeV = "0.13.0"
val zioV = "1.0.4"
Expand Down Expand Up @@ -70,6 +70,7 @@ lazy val client = project.in(file("client"))
"io.circe" %% "circe-generic" % circeV,
"dev.zio" %% "zio-test" % zioV % "it,test",
"dev.zio" %% "zio-test-sbt" % zioV % "it,test",
"dev.zio" %% "zio-test-magnolia" % zioV % "it,test",
"com.softwaremill.sttp.client" %% "core" % sttpV,
"com.softwaremill.sttp.client" %% "circe" % sttpV,
"com.softwaremill.sttp.client" %% "async-http-client-backend-zio" % sttpV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ case class InputBlock(label: PlainTextObject,
override val `type` = "input"
}

sealed trait InputBlockElement
sealed trait InputBlockElement {
self: BlockElement =>

val `type`: String
}

case class PlainTextInput(
action_id: String,
Expand All @@ -67,7 +71,8 @@ case class PlainTextInput(
min_length: Option[Int] = None,
max_length: Option[Int] = None,
dispatch_action_config: Option[DispatchActionConfig] = None
) extends BlockElement with InputBlockElement {
) extends BlockElement
with InputBlockElement {
override val `type` = "plain_text_input"
}

Expand All @@ -90,7 +95,7 @@ object TriggerAction {

implicit val decoder: Decoder[TriggerAction] = Decoder.decodeString.emap[TriggerAction] {
case "on_enter_pressed" => Right(OnEnterPressed)
case "on_character_pressed" => Right(OnCharacterEntered)
case "on_character_entered" => Right(OnCharacterEntered)
case s => Left(s"Could not decode TriggerAction from $s")
}
}
Expand Down Expand Up @@ -273,11 +278,15 @@ object BlockElement {
object InputBlockElement {
implicit val plainTextInputCodec: Codec.AsObject[PlainTextInput] = deriveCodec[PlainTextInput]

implicit val encoder: Encoder[InputBlockElement] = Encoder.AsObject.instance[InputBlockElement] {
case i: PlainTextInput => i.asJsonObject
case i: StaticSelectElement => i.asJsonObject
case i: DatePickerElement => i.asJsonObject
case i: ConversationSelectElement => i.asJsonObject
implicit val encoder: Encoder[InputBlockElement] = Encoder.AsObject.instance[InputBlockElement] { ibe =>
val json = ibe match {
case i: PlainTextInput => i.asJsonObject
case i: StaticSelectElement => i.asJsonObject
case i: DatePickerElement => i.asJsonObject
case i: ConversationSelectElement => i.asJsonObject
}

json.add("type", ibe.`type`.asJson)
}

val typeDecoder: Decoder[String] = Decoder.decodeString.at("type")
Expand Down
28 changes: 28 additions & 0 deletions client/src/test/scala/slack/api/models/SerializationSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package slack.api.models

import com.github.dapperware.slack.models.{ InputBlockElement, PlainTextInput }
import io.circe.syntax.EncoderOps
import zio.random.Random
import zio.test.Assertion._
import zio.test.magnolia.DeriveGen
import zio.test._

object SerializationSpec extends DefaultRunnableSpec {

implicit val blockInputGen: Gen[Sized with Random, PlainTextInput] = {
implicit val alphaCharGen: DeriveGen[String] = DeriveGen.instance(Gen.string(Gen.alphaNumericChar))
DeriveGen[PlainTextInput]
}

override def spec = suite("Serialization")(
suite("PlainTextInput")(
testM("reads what it writes")(check(blockInputGen) { blockInput =>
val encoded = (blockInput: InputBlockElement).asJson
val decoded = encoded.as[InputBlockElement]
assert(decoded)(isRight(equalTo(blockInput))) && assert(encoded.hcursor.downField("type").as[String])(
isRight(equalTo("plain_text_input"))
)
})
)
)
}

0 comments on commit 9b0ce0e

Please sign in to comment.