Avoid overloading the separate classes of writeField ops

This commit is contained in:
Zahary Karadjov 2022-06-19 12:38:44 +03:00
parent 95399caff3
commit bedbe6595a
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
3 changed files with 23 additions and 17 deletions

View File

@ -1,14 +1,14 @@
import std/options, ../../json_serialization/[reader, writer, lexer] import std/options, ../../json_serialization/[reader, writer, lexer]
export options export options
template writeField*(w: var JsonWriter, template writeObjectField*(w: var JsonWriter,
record: auto, record: auto,
fieldName: static string, fieldName: static string,
field: Option) = field: Option) =
mixin writeField mixin writeObjectField
if field.isSome: if field.isSome:
writeField(w, record, fieldName, field.get) writeObjectField(w, record, fieldName, field.get)
proc writeValue*(writer: var JsonWriter, value: Option) = proc writeValue*(writer: var JsonWriter, value: Option) =
mixin writeValue mixin writeValue

View File

@ -4,20 +4,26 @@ import
export export
results results
template writeField*[T](w: var JsonWriter, template writeObjectField*[T](w: var JsonWriter,
record: auto, record: auto,
fieldName: static string, fieldName: static string,
field: Result[T, void]) = field: Result[T, void]) =
mixin writeObjectField
if field.isOk: if field.isOk:
writeField(w, record, fieldName, field.get) writeObjectField(w, record, fieldName, field.get)
proc writeValue*[T](writer: var JsonWriter, value: Result[T, void]) = proc writeValue*[T](writer: var JsonWriter, value: Result[T, void]) =
mixin writeValue
if value.isOk: if value.isOk:
writer.writeValue value.get writer.writeValue value.get
else: else:
writer.writeValue JsonString("null") writer.writeValue JsonString("null")
proc readValue*[T](reader: var JsonReader, value: var Result[T, void]) = proc readValue*[T](reader: var JsonReader, value: var Result[T, void]) =
mixin readValue
let tok = reader.lexer.lazyTok let tok = reader.lexer.lazyTok
if tok == tkNull: if tok == tkNull:
reset value reset value

View File

@ -145,10 +145,10 @@ template isStringLike(v: string|cstring|openArray[char]|seq[char]): bool = true
template isStringLike[N](v: array[N, char]): bool = true template isStringLike[N](v: array[N, char]): bool = true
template isStringLike(v: auto): bool = false template isStringLike(v: auto): bool = false
template writeField*[FieldType, RecordType](w: var JsonWriter, template writeObjectField*[FieldType, RecordType](w: var JsonWriter,
record: RecordType, record: RecordType,
fieldName: static string, fieldName: static string,
field: FieldType) = field: FieldType) =
mixin writeFieldIMPL, writeValue mixin writeFieldIMPL, writeValue
type type
@ -237,8 +237,8 @@ proc writeValue*(w: var JsonWriter, value: auto) =
type RecordType = type value type RecordType = type value
w.beginRecord RecordType w.beginRecord RecordType
value.enumInstanceSerializedFields(fieldName, field): value.enumInstanceSerializedFields(fieldName, field):
mixin writeField mixin writeObjectField
writeField(w, value, fieldName, field) writeObjectField(w, value, fieldName, field)
w.state = AfterField w.state = AfterField
w.endRecord() w.endRecord()