Skip to content

Commit

Permalink
✨ Add Tapir module
Browse files Browse the repository at this point in the history
  • Loading branch information
jwojnowski committed Apr 13, 2024
1 parent a77d042 commit 8da12a9
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p uuid/target scuid/target circe/target core/target project/target
run: mkdir -p tapir/target uuid/target scuid/target circe/target core/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar uuid/target scuid/target circe/target core/target project/target
run: tar cf targets.tar tapir/target uuid/target scuid/target circe/target core/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ libraryDependencies += "me.wojnowski" %% "humanoid" % "<version>"
libraryDependencies += "me.wojnowski" %% "humanoid-scuid" % "<version>" // optional, for Cuid2 integration
libraryDependencies += "me.wojnowski" %% "humanoid-uuid" % "<version>" // optional, for UUID integration
libraryDependencies += "me.wojnowski" %% "humanoid-circe" % "<version>" // optional, for Circe codecs
libraryDependencies += "me.wojnowski" %% "humanoid-tapir" % "<version>" // optional, for Tapir schema/codecs
```

### Usage
Expand Down Expand Up @@ -89,3 +90,13 @@ libraryDependencies += "me.wojnowski" %% "humanoid-circe" % "<version>"
import me.wojnowski.humanoid.circe.strict._ // require prefix when decoding, encode with prefix
import me.wojnowski.humanoid.circe.relaxed._ // don't require prefix when decoding, encode with prefix
```

### Tapir integration
```scala
libraryDependencies += "me.wojnowski" %% "humanoid-tapir" % "<version>"
```

```scala
import me.wojnowski.humanoid.tapir.strict._ // require prefix when decoding, encode with prefix
import me.wojnowski.humanoid.tapir.relaxed._ // don't require prefix when decoding, encode with prefix
```
16 changes: 14 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ lazy val scuid =
lazy val circe =
project
.in(file("circe"))
.dependsOn(core)
.dependsOn(core % "compile->compile;test->test")
.settings(
name := "humanoid-circe",
libraryDependencies ++= Seq(
Expand All @@ -71,4 +71,16 @@ lazy val circe =
)
)

lazy val root = tlCrossRootProject.aggregate(core, scuid, uuid, circe)
lazy val tapir =
project
.in(file("tapir"))
.dependsOn(core % "compile->compile;test->test")
.settings(
name := "humanoid-tapir",
libraryDependencies ++= Seq(
"com.softwaremill.sttp.tapir" %% "tapir-core" % "1.10.4",
"org.scalameta" %% "munit" % "0.7.29" % Test
)
)

lazy val root = tlCrossRootProject.aggregate(core, scuid, uuid, circe, tapir)
35 changes: 8 additions & 27 deletions circe/src/test/scala/me/wojnowski/humanoid/circe/CodecTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ import io.circe.parser.decode
import io.circe.syntax.*
import me.wojnowski.humanoid.HumanId
import me.wojnowski.humanoid.HumanIdOps
import me.wojnowski.humanoid.IdConverter
import me.wojnowski.humanoid.LowercaseStringId
import munit.FunSuite

import CodecTest.LowercaseString

class CodecTest extends FunSuite {

val rawId: LowercaseString = LowercaseString.fromString("idvalue").toOption.get
val humanId: HumanId["pfx", LowercaseString] = HumanIdOps["pfx", LowercaseString].fromId(rawId)
val humanIdJsonWithPrefix: String = "\"pfx_idvalue\""
val humanIdJsonWithoutPrefix: String = "\"idvalue\""
val rawId: LowercaseStringId = LowercaseStringId.fromString("idvalue").toOption.get
val humanId: HumanId["pfx", LowercaseStringId] = HumanIdOps["pfx", LowercaseStringId].fromId(rawId)
val humanIdJsonWithPrefix: String = "\"pfx_idvalue\""
val humanIdJsonWithoutPrefix: String = "\"idvalue\""

test("encoder encodes with prefix") {
import me.wojnowski.humanoid.circe.strict.*
Expand All @@ -48,7 +46,7 @@ class CodecTest extends FunSuite {
test("relaxed decoder accepts ID without prefix") {
import me.wojnowski.humanoid.circe.relaxed.*

val result = decode[HumanId["pfx", LowercaseString]](humanIdJsonWithoutPrefix)
val result = decode[HumanId["pfx", LowercaseStringId]](humanIdJsonWithoutPrefix)

assertEquals(result, Right(humanId))
}
Expand All @@ -65,7 +63,7 @@ class CodecTest extends FunSuite {
test("strict decoder accepts ID with correct prefix") {
import me.wojnowski.humanoid.circe.strict.*

val result = decode[HumanId["pfx", LowercaseString]](humanIdJsonWithPrefix)
val result = decode[HumanId["pfx", LowercaseStringId]](humanIdJsonWithPrefix)

assertEquals(result, Right(humanId))
}
Expand All @@ -80,25 +78,8 @@ class CodecTest extends FunSuite {
test("strict decoder rejects ID without prefix") {
import me.wojnowski.humanoid.circe.strict.*

val result = decode[HumanId["other", LowercaseString]](humanIdJsonWithoutPrefix)
val result = decode[HumanId["other", LowercaseStringId]](humanIdJsonWithoutPrefix)
assert(result.isLeft)
}

}

object CodecTest {
abstract case class LowercaseString private (value: String)

object LowercaseString {
def fromString(value: String): Either[String, LowercaseString] =
Either.cond(value.forall(_.isLower), new LowercaseString(value) {}, s"Value [$value] is not lowercase")

implicit val idConverter: IdConverter[LowercaseString] = new IdConverter[LowercaseString] {
override def fromString(raw: String): Either[String, LowercaseString] = LowercaseString.fromString(raw)

override def renderString(id: LowercaseString): String = id.value
}

}

}
36 changes: 36 additions & 0 deletions core/src/test/scala/me/wojnowski/humanoid/LowercaseStringId.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 Jakub Wojnowski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package me.wojnowski.humanoid

abstract case class LowercaseStringId private (value: String)

object LowercaseStringId {
def fromString(value: String): Either[String, LowercaseStringId] =
Either.cond(value.forall(_.isLower), new LowercaseStringId(value) {}, s"Value [$value] is not lowercase")

implicit val idConverter: IdConverter[LowercaseStringId] = new IdConverter[LowercaseStringId] {
override def fromString(raw: String): Either[String, LowercaseStringId] = LowercaseStringId.fromString(raw)

override def renderString(id: LowercaseStringId): String = id.value
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 Jakub Wojnowski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package me.wojnowski.humanoid.tapir

import me.wojnowski.humanoid.HumanId
import sttp.tapir.Schema

trait TapirSchemas {
implicit def schemaForHumanId[P <: String, Id](implicit valueOf: ValueOf[P]): Schema[HumanId[P, Id]] =
Schema.string.format(s"ID with prefix: ${valueOf.value}_")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Jakub Wojnowski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package me.wojnowski.humanoid.tapir.relaxed

import me.wojnowski.humanoid.*
import sttp.tapir.Codec
import sttp.tapir.Codec.PlainCodec

trait RelaxedCodecs {
implicit def codecForHumanId[P <: String, Id](implicit valueOf: ValueOf[P], IdConverter: IdConverter[Id]): PlainCodec[HumanId[P, Id]] =
Codec.string.mapEither(HumanIdOps[P, Id].parsePrefixOptional)(_.renderWithPrefix)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 Jakub Wojnowski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package me.wojnowski.humanoid.tapir

package object relaxed extends TapirSchemas with RelaxedCodecs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Jakub Wojnowski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package me.wojnowski.humanoid.tapir.strict

import me.wojnowski.humanoid.*
import sttp.tapir.Codec
import sttp.tapir.Codec.PlainCodec

trait StrictCodecs {
implicit def codecForHumanId[P <: String, Id](implicit valueOf: ValueOf[P], IdConverter: IdConverter[Id]): PlainCodec[HumanId[P, Id]] =
Codec.string.mapEither(HumanIdOps[P, Id].parseRequirePrefix)(_.renderWithPrefix)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 Jakub Wojnowski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package me.wojnowski.humanoid.tapir

package object strict extends TapirSchemas with StrictCodecs
Loading

0 comments on commit 8da12a9

Please sign in to comment.