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

Add map and functor instance for Example #676

Merged
merged 3 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ lazy val cats: Project = (project in file("integrations/cats"))
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % "2.1.1",
scalaTest % Test,
scalaCheck % Test
scalaCheck % Test,
"org.typelevel" %% "discipline-scalatest" % "1.0.1" % Test,
"org.typelevel" %% "cats-laws" % "2.1.1" % Test
)
)
.dependsOn(core)
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/scala/sttp/tapir/EndpointIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ object EndpointIO {

//

case class Example[+T](value: T, name: Option[String], summary: Option[String])
case class Example[+T](value: T, name: Option[String], summary: Option[String]) {
def map[B](f: T => B): Example[B] = copy(value = f(value))
}

object Example {
def of[T](t: T, name: Option[String] = None, summary: Option[String] = None): Example[T] = Example(t, name, summary)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sttp.tapir.integ.cats

import cats.Functor
import sttp.tapir.EndpointIO.Example

trait ExampleInstances {
implicit val exampleFunctor: Functor[Example] = new Functor[Example] {
override def map[A, B](example: Example[A])(f: A => B): Example[B] = example.map(f)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package sttp.tapir.integ.cats

object instances extends ExampleInstances
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sttp.tapir.integ.cats

import cats.Eq
import cats.implicits._
import cats.laws.discipline.FunctorTests
import org.scalacheck.Arbitrary
import org.scalatest.funsuite.AnyFunSuite
import org.scalatestplus.scalacheck.Checkers
import org.typelevel.discipline.scalatest.FunSuiteDiscipline
import sttp.tapir.EndpointIO.Example
import sttp.tapir.integ.cats.instances._

class ExampleFunctorLawSpec extends AnyFunSuite with FunSuiteDiscipline with Checkers {
matwojcik marked this conversation as resolved.
Show resolved Hide resolved
implicit def eqTree[A: Eq]: Eq[Example[A]] = Eq.fromUniversalEquals
matwojcik marked this conversation as resolved.
Show resolved Hide resolved

implicit def exampleArbitrary[T: Arbitrary]: Arbitrary[Example[T]] = Arbitrary {
for {
t <- Arbitrary.arbitrary[T]
name <- Arbitrary.arbitrary[Option[String]]
summary <- Arbitrary.arbitrary[Option[String]]
} yield Example(t, name, summary)
}

checkAll("Example.FunctorLaws", FunctorTests[Example].functor[Int, Int, String])
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package sttp.tapir.integ.cats

import cats.data.{NonEmptyChain, NonEmptyList, NonEmptySet}
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.Checkers
import org.scalacheck.Arbitrary.arbString
import sttp.tapir.SchemaType.{SArray, SString}
Expand All @@ -12,7 +13,7 @@ import codec._

import scala.collection.immutable.SortedSet

class TapirCodecCatsTest extends FlatSpec with Matchers with Checkers {
class TapirCodecCatsTest extends AnyFlatSpec with Matchers with Checkers {
case class Test(value: String)

it should "find schema for cats collections" in {
Expand Down