mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 18:25:56 +01:00
fix: fix the time duration type duplicated parse for config (#17981)
Fix the time duration type duplicated parse for config, maintaining the idempotency of set and get operations. Signed-off-by: chlins <chenyuzh@vmware.com> Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
parent
dec0b5a3f2
commit
b0a30b9a2f
@ -245,7 +245,8 @@ func (t *DurationType) validate(str string) error {
|
||||
}
|
||||
|
||||
func (t *DurationType) get(str string) (interface{}, error) {
|
||||
return time.ParseDuration(str)
|
||||
// should not parse the duration to avoid duplicate parse.
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// parseInt64 returns int64 from string which support scientific notation
|
||||
|
@ -110,6 +110,17 @@ func TestStringToStringMapType_get(t *testing.T) {
|
||||
assert.Equal(t, map[string]string{"sample": "abc", "another": "welcome"}, result)
|
||||
}
|
||||
|
||||
func TestDurationType(t *testing.T) {
|
||||
test := &DurationType{}
|
||||
// test get
|
||||
result, err := test.get("5m")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "5m", result)
|
||||
// test validate
|
||||
assert.Nil(t, test.validate("5m"))
|
||||
assert.NotNil(t, test.validate("100"))
|
||||
}
|
||||
|
||||
func Test_parseInt64(t *testing.T) {
|
||||
type args struct {
|
||||
str string
|
||||
|
@ -153,10 +153,18 @@ func (c *ConfigureValue) GetDuration() time.Duration {
|
||||
log.Errorf("GetDuration failed, error: %+v", err)
|
||||
return 0
|
||||
}
|
||||
if durationValue, suc := val.(time.Duration); suc {
|
||||
return durationValue
|
||||
|
||||
if durationStr, suc := val.(string); suc {
|
||||
durationVal, err := time.ParseDuration(durationStr)
|
||||
if err != nil {
|
||||
log.Errorf("Parse %s to time duration failed, error: %v", durationStr, err)
|
||||
return 0
|
||||
}
|
||||
|
||||
return durationVal
|
||||
}
|
||||
}
|
||||
|
||||
log.Errorf("GetDuration failed, the current value's metadata is not defined, %+v", c)
|
||||
return 0
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user