forked from softwaremill/tapir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add string-based const enum Codec
process feedback simplify macro
- Loading branch information
1 parent
2c29764
commit 816841f
Showing
11 changed files
with
95 additions
and
46 deletions.
There are no files selected for viewing
4 changes: 0 additions & 4 deletions
4
core/src/main/scala-2/sttp/tapir/macros/LowPrioSchemaMacrosVersionSpecific.scala
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
core/src/main/scala-2/sttp/tapir/macros/SchemaCompanionMacrosExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package sttp.tapir | ||
package macros | ||
|
||
trait SchemaCompanionMacrosExtensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
core/src/main/scala-3/sttp/tapir/macros/LowPrioSchemaMacrosVersionSpecific.scala
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
core/src/main/scala-3/sttp/tapir/macros/SchemaCompanionMacrosExtensions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package sttp.tapir | ||
package macros | ||
|
||
import sttp.tapir.Schema.SName | ||
|
||
object SchemaCompanionMacrosExtensions extends SchemaCompanionMacrosExtensions | ||
|
||
trait SchemaCompanionMacrosExtensions: | ||
inline given derivedStringBasedUnionEnumeration[S](using IsUnionOf[String, S]): Schema[S] = | ||
lazy val values = UnionDerivation.constValueUnionTuple[String, S] | ||
lazy val validator = Validator.enumeration(values.toList.asInstanceOf[List[S]]) | ||
Schema | ||
.string[S] | ||
.name(SName(values.toList.mkString("_or_"))) | ||
.validate(validator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sttp.tapir | ||
|
||
import org.scalatest.{Assertion, Inside} | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatestplus.scalacheck.Checkers | ||
import sttp.tapir.CodecFormat.TextPlain | ||
import sttp.tapir.DecodeResult.Value | ||
|
||
import sttp.tapir.DecodeResult.InvalidValue | ||
|
||
class CodecScala3Test extends AnyFlatSpec with Matchers with Checkers with Inside { | ||
|
||
it should "derive a codec for a string-based union type" in { | ||
// given | ||
val codec = summon[Codec[String, "Apple" | "Banana", TextPlain]] | ||
|
||
// then | ||
codec.encode("Apple") shouldBe "Apple" | ||
codec.encode("Banana") shouldBe "Banana" | ||
codec.decode("Apple") shouldBe Value("Apple") | ||
codec.decode("Banana") shouldBe Value("Banana") | ||
codec.decode("Orange") should matchPattern { case DecodeResult.InvalidValue(List(ValidationError(_, "Orange", _, _))) => } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters