[cherry-pick] fix: fix the time duration type duplicated parse for config (#17987)

fix: fix the time duration type duplicated parse for config

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:
Chlins Zhang 2022-12-14 10:16:03 +08:00 committed by GitHub
parent 2531cf40a0
commit b12379b0ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}