-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Георгий Ковалев
committed
May 2, 2024
1 parent
b90f917
commit f7d77eb
Showing
35 changed files
with
2,090 additions
and
358 deletions.
There are no files selected for viewing
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
modules/core/src/main/scala-2.12/tethys/derivation/JsonObjectWriterDerivation.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,6 @@ | ||
package tethys.derivation | ||
|
||
|
||
private [tethys] trait JsonObjectWriterDerivation { | ||
|
||
} |
6 changes: 0 additions & 6 deletions
6
modules/core/src/main/scala-2.12/tethys/derivation/JsonWriterDerivation.scala
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
modules/core/src/main/scala-2.13+/tethys/derivation/JsonObjectWriterDerivation.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,6 @@ | ||
package tethys.derivation | ||
|
||
|
||
private [tethys] trait JsonObjectWriterDerivation { | ||
|
||
} |
6 changes: 0 additions & 6 deletions
6
modules/core/src/main/scala-2.13+/tethys/derivation/JsonWriterDerivation.scala
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
package tethys | ||
|
||
sealed trait JsonConfig[A]: | ||
def discriminateBy[Field](select: A => Field): JsonConfig[A] | ||
|
||
|
||
object JsonConfig: | ||
@scala.annotation.compileTimeOnly("Config must be an inlined given or provided directly to 'derived'") | ||
def configure[A]: JsonConfig[A] = | ||
throw IllegalStateException("Config must be an inlined given or provided directly to 'derived'") |
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,46 @@ | ||
package tethys | ||
|
||
import java.util.regex.Pattern | ||
|
||
|
||
enum JsonFieldStyle { | ||
case Capitalize, Uncapitalize, LowerCase, UpperCase | ||
|
||
case KebabCase, LowerKebabCase, UpperKebabCase, CapitalizedKebabCase | ||
|
||
case SnakeCase, LowerSnakeCase, UpperSnakeCase, CapitalizedSnakeCase | ||
} | ||
|
||
private[tethys] | ||
object JsonFieldStyle: | ||
private val regexp1: Pattern = Pattern.compile("([A-Z]+)([A-Z][a-z])") | ||
private val regexp2: Pattern = Pattern.compile("([a-z\\d])([A-Z])") | ||
private val replacement: String = "$1_$2" | ||
private val snakeCase: String => String = splitName(_).mkString("_") | ||
private val kebabcase: String => String = splitName(_).mkString("-") | ||
private val capitalize: String => String = _.capitalize | ||
private val uncapitalize: String => String = (field: String) => field.updated(0, field.charAt(0).toLower) | ||
private val lowercase: String => String = _.toLowerCase() | ||
private val uppercase: String => String = _.toUpperCase() | ||
|
||
def applyStyle(string: String, style: JsonFieldStyle): String = | ||
style match | ||
case JsonFieldStyle.Capitalize => capitalize(string) | ||
case JsonFieldStyle.Uncapitalize => uncapitalize(string) | ||
case JsonFieldStyle.LowerCase => lowercase(string) | ||
case JsonFieldStyle.UpperCase => uppercase(string) | ||
|
||
case JsonFieldStyle.KebabCase => kebabcase(string) | ||
case JsonFieldStyle.LowerKebabCase => (kebabcase andThen lowercase)(string) | ||
case JsonFieldStyle.UpperKebabCase => (kebabcase andThen uppercase)(string) | ||
case JsonFieldStyle.CapitalizedKebabCase => (kebabcase andThen capitalize)(string) | ||
|
||
case JsonFieldStyle.SnakeCase => snakeCase(string) | ||
case JsonFieldStyle.LowerSnakeCase => (snakeCase andThen lowercase)(string) | ||
case JsonFieldStyle.UpperSnakeCase => (snakeCase andThen uppercase)(string) | ||
case JsonFieldStyle.CapitalizedSnakeCase => (snakeCase andThen capitalize)(string) | ||
|
||
|
||
private def splitName(name: String): List[String] = | ||
val first = JsonFieldStyle.regexp1.matcher(name).replaceAll(JsonFieldStyle.replacement) | ||
JsonFieldStyle.regexp2.matcher(first).replaceAll(JsonFieldStyle.replacement).split("_").toList |
21 changes: 21 additions & 0 deletions
21
modules/core/src/main/scala-3/tethys/OrdinalEnumJsonReader.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,21 @@ | ||
package tethys | ||
|
||
import tethys.readers.{FieldName, ReaderError} | ||
import tethys.readers.tokens.TokenIterator | ||
|
||
trait OrdinalEnumJsonReader[A] extends JsonReader[A] | ||
|
||
object OrdinalEnumJsonReader: | ||
inline def derived[A <: scala.reflect.Enum]: OrdinalEnumJsonReader[A] = | ||
new OrdinalEnumJsonReader[A]: | ||
def read(it: TokenIterator)(implicit fieldName: FieldName): A = | ||
if it.currentToken().isNumberValue then | ||
val res = it.int() | ||
it.next() | ||
try derivation.EnumCompanion.getByOrdinal[A](res) | ||
catch | ||
case ex: NoSuchElementException => | ||
ReaderError.wrongJson(s"Unknown enum ordinal: $res") | ||
else | ||
ReaderError.wrongJson(s"Expected int value but found: ${it.currentToken()}") | ||
|
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
18 changes: 0 additions & 18 deletions
18
modules/core/src/main/scala-3/tethys/OrdinalEnumReader.scala
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
modules/core/src/main/scala-3/tethys/StringEnumJsonReader.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,23 @@ | ||
package tethys | ||
|
||
import tethys.readers.{FieldName, ReaderError} | ||
import tethys.readers.tokens.TokenIterator | ||
|
||
trait StringEnumJsonReader[A] extends JsonReader[A] | ||
|
||
object StringEnumJsonReader: | ||
inline def derived[A <: scala.reflect.Enum]: StringEnumJsonReader[A] = | ||
new StringEnumJsonReader[A]: | ||
def read(it: TokenIterator)(implicit fieldName: FieldName): A = | ||
if it.currentToken().isStringValue then | ||
val res = it.string() | ||
it.next() | ||
try | ||
derivation.EnumCompanion.getByName[A](res) | ||
catch | ||
case ex: NoSuchElementException => | ||
ReaderError.wrongJson(s"Unknown enum name: $res") | ||
else | ||
ReaderError.wrongJson(s"Expected string value but found: ${it.currentToken()}") | ||
|
||
|
6 changes: 3 additions & 3 deletions
6
...ain/scala-3/tethys/StringEnumWriter.scala → ...scala-3/tethys/StringEnumJsonWriter.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
18 changes: 0 additions & 18 deletions
18
modules/core/src/main/scala-3/tethys/StringEnumReader.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.