-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade zio-schema and adjust OpenAPI and CSV modules
- Loading branch information
Showing
10 changed files
with
2,406 additions
and
3,079 deletions.
There are no files selected for viewing
1,424 changes: 868 additions & 556 deletions
1,424
chopsticks-csv/src/main/scala/dev/chopsticks/csv/CsvDecoder.scala
Large diffs are not rendered by default.
Oops, something went wrong.
985 changes: 83 additions & 902 deletions
985
chopsticks-csv/src/main/scala/dev/chopsticks/csv/CsvEncoder.scala
Large diffs are not rendered by default.
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
2,699 changes: 1,285 additions & 1,414 deletions
2,699
...sticks-openapi/src/main/scala/dev/chopsticks/openapi/OpenApiZioSchemaCirceConverter.scala
Large diffs are not rendered by default.
Oops, something went wrong.
241 changes: 47 additions & 194 deletions
241
...icks-openapi/src/main/scala/dev/chopsticks/openapi/OpenApiZioSchemaToTapirConverter.scala
Large diffs are not rendered by default.
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
44 changes: 44 additions & 0 deletions
44
chopsticks-openapi/src/main/scala/dev/chopsticks/openapi/common/ConverterCacheKey.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,44 @@ | ||
package dev.chopsticks.openapi.common | ||
|
||
import dev.chopsticks.openapi.OpenApiParsedAnnotations | ||
import zio.schema.TypeId | ||
|
||
final private[chopsticks] case class ConverterCacheKey(entityName: String, annotationsHash: Int) | ||
|
||
final private[chopsticks] class ConverterCache[C[_]]( | ||
cache: scala.collection.mutable.Map[ConverterCacheKey, C[_] with ConverterCache.Lazy[C[_]]] = | ||
scala.collection.mutable.Map.empty[ConverterCacheKey, C[_] with ConverterCache.Lazy[C[_]]] | ||
) { | ||
private[chopsticks] def convertUsingCache[A]( | ||
typeId: TypeId, | ||
annotations: OpenApiParsedAnnotations[A] | ||
)(convert: => C[A])( | ||
initLazy: () => C[A] with ConverterCache.Lazy[C[A]] | ||
): C[A] = { | ||
val entityName = OpenApiConverterUtils.getEntityName(Some(typeId), annotations) | ||
entityName match { | ||
case Some(name) => | ||
val cacheKey = ConverterCacheKey(name, annotations.hashCode()) | ||
cache.get(cacheKey) match { | ||
case Some(value) => value.asInstanceOf[C[A]] | ||
case None => | ||
val lazyEnc = initLazy() | ||
val _ = cache.addOne(cacheKey -> lazyEnc.asInstanceOf[C[_] with ConverterCache.Lazy[C[_]]]) | ||
val result = convert | ||
lazyEnc.set(result) | ||
result | ||
} | ||
case None => | ||
convert | ||
} | ||
} | ||
} | ||
object ConverterCache { | ||
private[chopsticks] trait Lazy[A] { | ||
private var _value: A = _ | ||
final private[ConverterCache] def set(value: A): Unit = _value = value | ||
final private[chopsticks] def get: A = | ||
if (_value == null) throw new RuntimeException(s"${this.getClass.getSimpleName} has not yet been initialized") | ||
else _value | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
chopsticks-openapi/src/main/scala/dev/chopsticks/openapi/common/OpenApiConverterUtils.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,50 @@ | ||
package dev.chopsticks.openapi.common | ||
|
||
import dev.chopsticks.openapi.OpenApiParsedAnnotations | ||
import zio.schema.{Schema, TypeId} | ||
import zio.schema.TypeId.Nominal | ||
|
||
object OpenApiConverterUtils { | ||
def getEntityName(schema: Schema[_]): Option[String] = { | ||
schema match { | ||
case r: Schema.Record[_] => | ||
val parsed = OpenApiParsedAnnotations.extractAnnotations(r.annotations) | ||
getEntityName(Some(r.id), parsed) | ||
case r: Schema.Enum[_] => | ||
val parsed = OpenApiParsedAnnotations.extractAnnotations(r.annotations) | ||
getEntityName(Some(r.id), parsed) | ||
case r: Schema.Lazy[_] => | ||
getEntityName(r.schema) | ||
case other => | ||
OpenApiParsedAnnotations.extractAnnotations(other.annotations).entityName | ||
} | ||
} | ||
|
||
private[chopsticks] def getCaseEntityName( | ||
schemaCase: Schema.Case[_, _], | ||
parsed: OpenApiParsedAnnotations[_] | ||
): Option[String] = { | ||
getEntityName(schemaCase.schema, parsed) | ||
} | ||
|
||
private def getEntityName(schema: Schema[_], parsed: OpenApiParsedAnnotations[_]): Option[String] = { | ||
schema match { | ||
case r: Schema.Record[_] => | ||
getEntityName(Some(r.id), parsed) | ||
case r: Schema.Lazy[_] => | ||
getEntityName(r.schema, parsed) | ||
case _ => | ||
parsed.entityName | ||
} | ||
} | ||
|
||
private[chopsticks] def getEntityName(typeId: Option[TypeId], metadata: OpenApiParsedAnnotations[_]) = { | ||
metadata.entityName.orElse { | ||
typeId match { | ||
case Some(Nominal(packageName, objectNames, typeName)) => | ||
Some(OpenApiSettings.default.getTypeName(packageName, objectNames, typeName)) | ||
case _ => None | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
chopsticks-openapi/src/main/scala/dev/chopsticks/openapi/common/OpenApiSettings.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,16 @@ | ||
package dev.chopsticks.openapi.common | ||
|
||
import zio.Chunk | ||
|
||
trait OpenApiSettings { | ||
def getTypeName(packageName: Chunk[String], objectNames: Chunk[String], typeName: String): String | ||
} | ||
object OpenApiSettings { | ||
val default = new OpenApiSettings { | ||
override def getTypeName(packageName: Chunk[String], objectNames: Chunk[String], typeName: String): String = { | ||
val n1 = objectNames.mkString("_") | ||
if (n1.isEmpty) typeName | ||
else s"${n1}_$typeName" | ||
} | ||
} | ||
} |
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