-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable EndpointAPI codecs (#2911)
- Loading branch information
Showing
20 changed files
with
295 additions
and
191 deletions.
There are no files selected for viewing
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
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
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
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
49 changes: 47 additions & 2 deletions
49
zio-http/shared/src/main/scala/zio/http/codec/BinaryCodecWithSchema.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 |
---|---|---|
@@ -1,11 +1,56 @@ | ||
package zio.http.codec | ||
|
||
import zio._ | ||
|
||
import zio.schema.Schema | ||
import zio.schema.codec.BinaryCodec | ||
|
||
final case class BinaryCodecWithSchema[A](codec: BinaryCodec[A], schema: Schema[A]) | ||
import zio.http.Middleware | ||
|
||
final case class BinaryCodecWithSchema[A](codecFn: (Schema[A], CodecConfig) => BinaryCodec[A], schema: Schema[A]) { | ||
private var cached = Map.empty[CodecConfig, BinaryCodec[A]] | ||
def codec(config: CodecConfig): BinaryCodec[A] = | ||
cached.getOrElse( | ||
config, { | ||
val codec = codecFn(schema, config) | ||
cached += (config -> codec) | ||
codec | ||
}, | ||
) | ||
} | ||
|
||
object BinaryCodecWithSchema { | ||
def fromBinaryCodec[A](codec: BinaryCodec[A])(implicit schema: Schema[A]): BinaryCodecWithSchema[A] = | ||
def apply[A](codec: BinaryCodec[A], schema: Schema[A]): BinaryCodecWithSchema[A] = | ||
BinaryCodecWithSchema((_, _) => codec, schema) | ||
|
||
def fromBinaryCodec[A](codec: (Schema[A], CodecConfig) => BinaryCodec[A])(implicit | ||
schema: Schema[A], | ||
): BinaryCodecWithSchema[A] = | ||
BinaryCodecWithSchema(codec, schema) | ||
|
||
def fromBinaryCodec[A](codec: BinaryCodec[A])(implicit schema: Schema[A]): BinaryCodecWithSchema[A] = | ||
BinaryCodecWithSchema((_, _) => codec, schema) | ||
} | ||
|
||
/** | ||
* Configuration that is handed over when creating a binary codec | ||
* @param ignoreEmptyCollections | ||
* if true, empty collections will be ignored when encoding. This is currently | ||
* only used for the JSON codec | ||
*/ | ||
|
||
final case class CodecConfig( | ||
ignoreEmptyCollections: Boolean = true, | ||
) | ||
|
||
object CodecConfig { | ||
val defaultConfig: CodecConfig = CodecConfig() | ||
|
||
def configLayer(config: CodecConfig): ULayer[Unit] = | ||
ZLayer(codecRef.set(config)) | ||
|
||
def withConfig(config: CodecConfig): Middleware[Any] = | ||
Middleware.runBefore(codecRef.set(config)) | ||
|
||
private[http] val codecRef: FiberRef[CodecConfig] = FiberRef.unsafe.make(defaultConfig)(Unsafe) | ||
} |
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
Oops, something went wrong.