Skip to content

Commit

Permalink
Merge pull request #676 from matwojcik/example-functor
Browse files Browse the repository at this point in the history
Add map and functor instance for Example
  • Loading branch information
adamw authored Jul 23, 2020
2 parents 473d164 + 12374f7 commit aa3b578
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
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,25 @@
package sttp.tapir.integ.cats

import cats.Eq
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 {
implicit def exampleEq[A]: Eq[Example[A]] = Eq.fromUniversalEquals

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

0 comments on commit aa3b578

Please sign in to comment.