-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the flavors support optional when defining formats
Other changes: * The format declarations have been broken down in multiple parts. This makes it possible to structure the implementation libraries in a way such that the format name is accessible even when importing only reader or writer modules. * Flavors lead to more complicated Reader and Writer types. Instead of requiring the user to write down long type names such as `JsonReader[DefaultFlavor]`, it's now possible to refer to the reader and writer type of each format using expressions such as `Json.Reader` and `Json.Writer`. * Fixed a typo (PreferredOutput)
- Loading branch information
Showing
5 changed files
with
92 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import | ||
std/typetraits | ||
|
||
template serializationFormatImpl(Name: untyped, | ||
mimeTypeName: static string = "") {.dirty.} = | ||
# This indirection is required in order to be able to generate the | ||
# `mimeType` accessor template. Without the indirection, the template | ||
# mechanism of Nim will try to expand the `mimeType` param in the position | ||
# of the `mimeType` template name which will result in error. | ||
type Name* = object | ||
template mimeType*(T: type Name): string = mimeTypeName | ||
|
||
template serializationFormat*(Name: untyped, mimeType: static string = "") = | ||
serializationFormatImpl(Name, mimeType) | ||
|
||
template setReader*(Format, FormatReader: distinct type) = | ||
when arity(FormatReader) > 1: | ||
template ReaderType*(T: type Format, F: distinct type = DefaultFlavor): type = FormatReader[F] | ||
template Reader*(T: type Format, F: distinct type = DefaultFlavor): type = FormatReader[F] | ||
else: | ||
template ReaderType*(T: type Format): type = FormatReader | ||
template Reader*(T: type Format): type = FormatReader | ||
|
||
template setWriter*(Format, FormatWriter, PreferredOutput: distinct type) = | ||
when arity(FormatWriter) > 1: | ||
template WriterType*(T: type Format, F: distinct type = DefaultFlavor): type = FormatWriter[F] | ||
template Writer*(T: type Format, F: distinct type = DefaultFlavor): type = FormatWriter[F] | ||
else: | ||
template WriterType*(T: type Format): type = FormatWriter | ||
template Writer*(T: type Format): type = FormatWriter | ||
|
||
template PreferredOutputType*(T: type Format): type = PreferredOutput | ||
|
||
template createFlavor*(ModifiedFormat, FlavorName: untyped) = | ||
type FlavorName* = object | ||
template Reader*(T: type FlavorName): type = Reader(ModifiedFormat, FlavorName) | ||
template Writer*(T: type FlavorName): type = Writer(ModifiedFormat, FlavorName) | ||
template PreferredOutputType*(T: type FlavorName): type = PreferredOutputType(ModifiedFormat) | ||
template mimeType*(T: type FlavorName): string = mimeType(ModifiedFormat) | ||
|
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