Skip to content

Commit 6f8e757

Browse files
committed
review date expiration functions
1 parent b173f81 commit 6f8e757

File tree

1 file changed

+53
-85
lines changed

1 file changed

+53
-85
lines changed

src/utils/utils.go

Lines changed: 53 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -154,65 +154,53 @@ func GetInt64Value(records map[string]interface{}, key string) int64 {
154154
}
155155

156156
func GetExpirationNum(val interface{}) int32 {
157-
now := int32(time.Now().Unix())
158-
num := int32(0)
159-
switch val.(type) {
160-
case nil:
161-
return 0
162-
case int:
163-
num = int32(val.(int))
164-
if num > now {
165-
return num - now
166-
}
167-
return num
168-
case int32:
169-
num = val.(int32)
170-
if num > now {
171-
return num - now
172-
}
173-
return num
174-
case int64:
175-
num = int32(val.(int64))
176-
if num > now {
177-
return num - now
178-
}
179-
return num
180-
case float64:
181-
num = int32(val.(float64))
182-
if num > now {
183-
return num - now
184-
}
185-
return num
186-
case string:
187-
expiration := val.(string)
188-
match := regexExpiration.FindStringSubmatch(expiration)
189-
// expiration format: 10d, 10h, 10m, 10s
190-
if len(match) == 2 {
191-
return Atoi(match[1])
192-
}
193-
if len(match) != 3 {
194-
log.Printf("failed to parse expiration value: %s", expiration)
195-
return 0
196-
}
197-
num2 := match[1]
198-
format := match[2]
199-
if len(format) == 0 {
200-
return Atoi(num2)
201-
}
202-
start := int32(0)
203-
switch format {
204-
case "d": // day
205-
start = (Atoi(num2) * 24 * 3600)
206-
case "h": // hour
207-
start = (Atoi(num2) * 3600)
208-
case "m": // month
209-
start = (Atoi(num2) * 24 * 31 * 3600)
210-
case "s":
211-
start = (Atoi(num2))
212-
}
213-
return start
214-
}
215-
return 0
157+
now := int32(time.Now().Unix())
158+
num := int32(0)
159+
switch val.(type) {
160+
case nil:
161+
return 0
162+
case int:
163+
num = int32(val.(int))
164+
case int32:
165+
num = val.(int32)
166+
case int64:
167+
num = int32(val.(int64))
168+
case float64:
169+
num = int32(val.(float64))
170+
case string:
171+
expiration := val.(string)
172+
match := regexExpiration.FindStringSubmatch(expiration)
173+
log.Printf("match: %v", match)
174+
// expiration format: 10d, 10h, 10m, 10s
175+
if len(match) == 2 {
176+
num = Atoi(match[1])
177+
} else {
178+
if len(match) != 3 {
179+
log.Printf("failed to parse expiration value: %s", expiration)
180+
return 0
181+
}
182+
numStr := match[1]
183+
format := match[2]
184+
if len(format) == 0 {
185+
num = Atoi(numStr)
186+
} else {
187+
switch format {
188+
case "d": // day
189+
num = (Atoi(numStr) * 24 * 3600)
190+
case "h": // hour
191+
num = (Atoi(numStr) * 3600)
192+
case "m": // month
193+
num = (Atoi(numStr) * 24 * 31 * 3600)
194+
case "s":
195+
num = (Atoi(numStr))
196+
}
197+
}
198+
}
199+
}
200+
if num > now {
201+
return num - now
202+
}
203+
return num
216204
}
217205

218206
func GetArgEnvFileVariable(vname string, masterKeyPtr *string) string {
@@ -368,33 +356,13 @@ func ParseExpiration0(expiration string) (int32, error) {
368356
return start, nil
369357
}
370358

371-
func ParseExpiration(expiration string) (int32, error) {
372-
match := regexExpiration.FindStringSubmatch(expiration)
373-
// expiration format: 10d, 10h, 10m, 10s
374-
if len(match) == 2 {
375-
return Atoi(match[1]), nil
359+
func ParseExpiration(expiration interface{}) (int32, error) {
360+
now := int32(time.Now().Unix()) + 10
361+
result := GetExpirationNum(expiration)
362+
if result == 0 {
363+
return 0, nil
376364
}
377-
if len(match) != 3 {
378-
e := fmt.Sprintf("failed to parse expiration value: %s", expiration)
379-
return 0, errors.New(e)
380-
}
381-
num := match[1]
382-
format := match[2]
383-
if len(format) == 0 {
384-
return Atoi(num), nil
385-
}
386-
start := int32(time.Now().Unix())
387-
switch format {
388-
case "d": // day
389-
start = start + (Atoi(num) * 24 * 3600)
390-
case "h": // hour
391-
start = start + (Atoi(num) * 3600)
392-
case "m": // month
393-
start = start + (Atoi(num) * 24 * 31 * 3600)
394-
case "s":
395-
start = start + (Atoi(num))
396-
}
397-
return start, nil
365+
return result + now, nil
398366
}
399367

400368
func LockMemory() error {

0 commit comments

Comments
 (0)