mirror of
https://github.com/waku-org/nwaku.git
synced 2025-02-09 21:46:53 +00:00
fix(store): retention policy regex (#2532)
This commit is contained in:
parent
0894f0cfea
commit
23a291b372
@ -17,12 +17,13 @@ import
|
|||||||
proc new*(T: type RetentionPolicy,
|
proc new*(T: type RetentionPolicy,
|
||||||
retPolicy: string):
|
retPolicy: string):
|
||||||
RetentionPolicyResult[Option[RetentionPolicy]] =
|
RetentionPolicyResult[Option[RetentionPolicy]] =
|
||||||
|
let retPolicy = retPolicy.toLower
|
||||||
|
|
||||||
# Validate the retention policy format
|
# Validate the retention policy format
|
||||||
if retPolicy == "" or retPolicy == "none":
|
if retPolicy == "" or retPolicy == "none":
|
||||||
return ok(none(RetentionPolicy))
|
return ok(none(RetentionPolicy))
|
||||||
|
|
||||||
const StoreMessageRetentionPolicyRegex = re"^\w+:\w+$"
|
const StoreMessageRetentionPolicyRegex = re"^\w+:\d*\.?\d+((g|m)b)?$"
|
||||||
if not retPolicy.match(StoreMessageRetentionPolicyRegex):
|
if not retPolicy.match(StoreMessageRetentionPolicyRegex):
|
||||||
return err("invalid 'store message retention policy' format: " & retPolicy)
|
return err("invalid 'store message retention policy' format: " & retPolicy)
|
||||||
|
|
||||||
@ -63,25 +64,24 @@ proc new*(T: type RetentionPolicy,
|
|||||||
# to hold the numeric value data of size
|
# to hold the numeric value data of size
|
||||||
var inptSizeQuantity: float
|
var inptSizeQuantity: float
|
||||||
var sizeQuantity: int64
|
var sizeQuantity: int64
|
||||||
|
var sizeMultiplier: float
|
||||||
|
|
||||||
if sizeUnit in ["gb", "Gb", "GB", "gB"]:
|
|
||||||
# parse the actual value into integer type var
|
|
||||||
try:
|
try:
|
||||||
inptSizeQuantity = parseFloat(sizeQuantityStr)
|
inptSizeQuantity = parseFloat(sizeQuantityStr)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return err("invalid size retention policy argument: " & getCurrentExceptionMsg())
|
return err("invalid size retention policy argument: " & getCurrentExceptionMsg())
|
||||||
# GB data is converted into bytes for uniform processing
|
|
||||||
sizeQuantity = int64(inptSizeQuantity * 1024.0 * 1024.0 * 1024.0)
|
case sizeUnit:
|
||||||
elif sizeUnit in ["mb", "Mb", "MB", "mB"]:
|
of "gb":
|
||||||
try:
|
sizeMultiplier = 1024.0 * 1024.0 * 1024.0
|
||||||
inptSizeQuantity = parseFloat(sizeQuantityStr)
|
of "mb":
|
||||||
# MB data is converted into bytes for uniform processing
|
sizeMultiplier = 1024.0 * 1024.0
|
||||||
sizeQuantity = int64(inptSizeQuantity * 1024.0 * 1024.0)
|
|
||||||
except ValueError:
|
|
||||||
return err("invalid size retention policy argument")
|
|
||||||
else:
|
else:
|
||||||
return err ("""invalid size retention value unit: expected "Mb" or "Gb" but got """ & sizeUnit )
|
return err ("""invalid size retention value unit: expected "Mb" or "Gb" but got """ & sizeUnit )
|
||||||
|
|
||||||
|
# quantity is converted into bytes for uniform processing
|
||||||
|
sizeQuantity = int64(inptSizeQuantity * sizeMultiplier)
|
||||||
|
|
||||||
if sizeQuantity <= 0:
|
if sizeQuantity <= 0:
|
||||||
return err("invalid size retention policy argument: a non-zero value is required")
|
return err("invalid size retention policy argument: a non-zero value is required")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user