fixes envvar Option[T] decoder
This commit is contained in:
parent
f4087c1789
commit
b961fbe7b4
|
@ -43,7 +43,7 @@ template loadFile*(_: type Envvar,
|
||||||
var reader = unpackArgs(init, [EnvvarReader, prefix, params])
|
var reader = unpackArgs(init, [EnvvarReader, prefix, params])
|
||||||
reader.readValue(RecordType)
|
reader.readValue(RecordType)
|
||||||
|
|
||||||
template saveFile*(Format: type, prefix: string, value: auto, params: varargs[untyped]) =
|
template saveFile*(_: type Envvar, prefix: string, value: auto, params: varargs[untyped]) =
|
||||||
mixin init, WriterType, writeValue
|
mixin init, WriterType, writeValue
|
||||||
|
|
||||||
var writer = unpackArgs(init, [EnvvarWriter, prefix, params])
|
var writer = unpackArgs(init, [EnvvarWriter, prefix, params])
|
||||||
|
|
|
@ -29,8 +29,6 @@ proc handleReadException*(r: EnvvarReader,
|
||||||
proc init*(T: type EnvvarReader, prefix: string): T =
|
proc init*(T: type EnvvarReader, prefix: string): T =
|
||||||
result.prefix = prefix
|
result.prefix = prefix
|
||||||
|
|
||||||
template getUnderlyingType*[T](_: Option[T]): untyped = T
|
|
||||||
|
|
||||||
proc readValue*[T](r: var EnvvarReader, value: var T)
|
proc readValue*[T](r: var EnvvarReader, value: var T)
|
||||||
{.raises: [SerializationError, ValueError, Defect].} =
|
{.raises: [SerializationError, ValueError, Defect].} =
|
||||||
mixin readValue
|
mixin readValue
|
||||||
|
@ -45,15 +43,14 @@ proc readValue*[T](r: var EnvvarReader, value: var T)
|
||||||
getValue(key, value)
|
getValue(key, value)
|
||||||
|
|
||||||
elif T is Option:
|
elif T is Option:
|
||||||
|
template getUnderlyingType[T](_: Option[T]): untyped = T
|
||||||
let key = constructKey(r.prefix, r.key)
|
let key = constructKey(r.prefix, r.key)
|
||||||
if existsEnv(key):
|
if existsEnv(key):
|
||||||
type uType = getUnderlyingType(value)
|
type uType = getUnderlyingType(value)
|
||||||
var outVal: uType
|
|
||||||
when uType is string:
|
when uType is string:
|
||||||
value = some(os.getEnv(key))
|
value = some(os.getEnv(key))
|
||||||
else:
|
else:
|
||||||
r.readValue(outVal)
|
value = some(r.readValue(uType))
|
||||||
value = some(outVal)
|
|
||||||
|
|
||||||
elif T is (seq or array):
|
elif T is (seq or array):
|
||||||
when uTypeIsPrimitives(T):
|
when uTypeIsPrimitives(T):
|
||||||
|
|
|
@ -88,5 +88,29 @@ proc testEncoder() =
|
||||||
check x.antennae.isNone
|
check x.antennae.isNone
|
||||||
check x.bumper.get() == "Chromium"
|
check x.bumper.get() == "Chromium"
|
||||||
|
|
||||||
|
type
|
||||||
|
ValidIpAddress {.requiresInit.} = object
|
||||||
|
value: string
|
||||||
|
|
||||||
|
TestObject = object
|
||||||
|
address: Option[ValidIpAddress]
|
||||||
|
|
||||||
|
proc readValue(r: var EnvvarReader, value: var ValidIpAddress) =
|
||||||
|
r.readValue(value.value)
|
||||||
|
|
||||||
|
proc writeValue(w: var EnvvarWriter, value: ValidIpAddress) =
|
||||||
|
w.writeValue(value.value)
|
||||||
|
|
||||||
|
proc testOptionalFields() =
|
||||||
|
suite "optional fields test suite":
|
||||||
|
test "optional field with requiresInit pragma":
|
||||||
|
|
||||||
|
var z = TestObject(address: some(ValidIpAddress(value: "1.2.3.4")))
|
||||||
|
Envvar.saveFile(commonPrefix, z)
|
||||||
|
var x = Envvar.loadFile(commonPrefix, TestObject)
|
||||||
|
check x.address.isSome
|
||||||
|
check x.address.get().value == "1.2.3.4"
|
||||||
|
|
||||||
testUtils()
|
testUtils()
|
||||||
testEncoder()
|
testEncoder()
|
||||||
|
testOptionalFields()
|
||||||
|
|
Loading…
Reference in New Issue