fix(store): retention policy regex (#2532)

This commit is contained in:
richΛrd 2024-03-15 09:46:35 -04:00 committed by GitHub
parent 0894f0cfea
commit 23a291b372
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)
@ -55,33 +56,32 @@ proc new*(T: type RetentionPolicy,
elif policy == "size": elif policy == "size":
var retentionSize: string var retentionSize: string
retentionSize = policyArgs retentionSize = policyArgs
# captures the size unit such as GB or MB # captures the size unit such as GB or MB
let sizeUnit = retentionSize.substr(retentionSize.len-2) let sizeUnit = retentionSize.substr(retentionSize.len-2)
# captures the string type number data of the size provided # captures the string type number data of the size provided
let sizeQuantityStr = retentionSize.substr(0,retentionSize.len-3) let sizeQuantityStr = retentionSize.substr(0,retentionSize.len-3)
# 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 case sizeUnit:
sizeQuantity = int64(inptSizeQuantity * 1024.0 * 1024.0 * 1024.0) of "gb":
elif sizeUnit in ["mb", "Mb", "MB", "mB"]: sizeMultiplier = 1024.0 * 1024.0 * 1024.0
try: of "mb":
inptSizeQuantity = parseFloat(sizeQuantityStr) sizeMultiplier = 1024.0 * 1024.0
# MB data is converted into bytes for uniform processing else:
sizeQuantity = int64(inptSizeQuantity * 1024.0 * 1024.0) return err ("""invalid size retention value unit: expected "Mb" or "Gb" but got """ & sizeUnit )
except ValueError:
return err("invalid size retention policy argument") # quantity is converted into bytes for uniform processing
else: sizeQuantity = int64(inptSizeQuantity * sizeMultiplier)
return err ("""invalid size retention value unit: expected "Mb" or "Gb" but got """ & sizeUnit )
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")