cover missing case in MultiAddress.init (#198)

* cover missing case in MultiAddress.init

* raise assert on marker in protocol

* unify inits for markers / non-markers

* fix string
This commit is contained in:
Jacek Sieka 2020-06-01 14:41:45 +02:00 committed by GitHub
parent 88dbeebf17
commit 7e31210455
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 19 deletions

View File

@ -692,7 +692,9 @@ proc validate*(ma: MultiAddress): bool =
discard discard
result = true result = true
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openarray[byte]): MaResult[MultiAddress] = proc init*(
mtype: typedesc[MultiAddress], protocol: MultiCodec,
value: openarray[byte] = []): MaResult[MultiAddress] =
## Initialize MultiAddress object from protocol id ``protocol`` and array ## Initialize MultiAddress object from protocol id ``protocol`` and array
## of bytes ``value``. ## of bytes ``value``.
let proto = CodeAddresses.getOrDefault(protocol) let proto = CodeAddresses.getOrDefault(protocol)
@ -702,38 +704,31 @@ proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openarray
var res: MultiAddress var res: MultiAddress
res.data = initVBuffer() res.data = initVBuffer()
res.data.writeVarint(cast[uint64](proto.mcodec)) res.data.writeVarint(cast[uint64](proto.mcodec))
if proto.kind in {Fixed, Length, Path}: case proto.kind
of Fixed, Length, Path:
if len(value) == 0: if len(value) == 0:
err("multiaddress: Value must not be empty array") err("multiaddress: Value must not be empty array")
else: else:
if proto.kind == Fixed: if proto.kind == Fixed:
res.data.writeArray(value) res.data.writeArray(value)
else: else:
var data = newSeq[byte](len(value)) res.data.writeSeq(value)
copyMem(addr data[0], unsafeAddr value[0], len(value))
res.data.writeSeq(data)
res.data.finish() res.data.finish()
ok(res) ok(res)
of Marker:
if len(value) != 0:
err("multiaddress: Value must be empty for markers")
else:
res.data.finish()
ok(res)
of None:
raiseAssert "None checked above"
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: PeerID): MaResult[MultiAddress] {.inline.} = proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: PeerID): MaResult[MultiAddress] {.inline.} =
## Initialize MultiAddress object from protocol id ``protocol`` and peer id ## Initialize MultiAddress object from protocol id ``protocol`` and peer id
## ``value``. ## ``value``.
init(mtype, protocol, value.data) init(mtype, protocol, value.data)
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec): MaResult[MultiAddress] =
## Initialize MultiAddress object from protocol id ``protocol``.
let proto = CodeAddresses.getOrDefault(protocol)
if proto.kind == None:
err("multiaddress: Protocol not found")
else:
var res: MultiAddress
res.data = initVBuffer()
if proto.kind != Marker:
raise newException(MultiAddressError, "Protocol missing value")
res.data.writeVarint(cast[uint64](proto.mcodec))
res.data.finish()
ok(res)
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: int): MaResult[MultiAddress] = proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: int): MaResult[MultiAddress] =
## Initialize MultiAddress object from protocol id ``protocol`` and integer ## Initialize MultiAddress object from protocol id ``protocol`` and integer
## ``value``. This procedure can be used to instantiate ``tcp``, ``udp``, ## ``value``. This procedure can be used to instantiate ``tcp``, ``udp``,