Skip to content

Commit

Permalink
fix: replace props when get (#3409)
Browse files Browse the repository at this point in the history
Signed-off-by: Song Gao <[email protected]>
  • Loading branch information
Yisaer authored and ngjaying committed Nov 29, 2024
1 parent 05338c1 commit 740a5d5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
7 changes: 7 additions & 0 deletions internal/meta/yamlConfigMeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ func GetYamlConf(configOperatorKey, language string) (b []byte, err error) {
}

cf := cfgOps.CopyConfContent()
for plug, props := range cf {
changed, newProps := replace.ReplacePropsWithPlug(plug, props)
if changed {
cf[plug] = newProps
}
}

if b, err = json.Marshal(cf); nil != err {
return nil, fmt.Errorf(`%s%v`, getMsg(language, source, "json_marshal_fail"), cf)
} else {
Expand Down
19 changes: 19 additions & 0 deletions pkg/replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ func ReplacePassword(props map[string]interface{}) (bool, map[string]interface{}
func ReplaceDuration(props map[string]interface{}) (bool, map[string]interface{}) {
changed := false
for _, replaceWord := range replaceDuration {
if replaceWord == "cacheTtl" {
vm, ok := props["lookup"]
if ok {
lookupm, ok := vm.(map[string]interface{})
if ok {
oldValue, ok := lookupm[replaceWord]
if ok {
intRaw, err := cast.ToInt(oldValue, cast.CONVERT_ALL)
if err == nil {
lookupm[replaceWord] = (time.Duration(intRaw) * time.Millisecond).String()
changed = true
props["lookup"] = lookupm
continue
}
}

}
}
}
v, ok := props[replaceWord]
if ok {
intRaw, err := cast.ToInt(v, cast.CONVERT_ALL)
Expand Down
47 changes: 33 additions & 14 deletions pkg/replace/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,50 +120,69 @@ func TestReplaceRuleJson(t *testing.T) {

func TestReplaceDuration(t *testing.T) {
props := map[string]interface{}{
"cacheTtl": 1000,
"timeout": 1000,
}
changed, newProps := ReplaceDuration(props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"timeout": "1s",
}, newProps)
props = map[string]interface{}{
"cacheTtl": int64(1000),
"timeout": int64(1000),
}
changed, newProps = ReplaceDuration(props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"timeout": "1s",
}, newProps)
props = map[string]interface{}{
"cacheTtl": float64(1000),
"timeout": float64(1000),
}
changed, newProps = ReplaceDuration(props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"timeout": "1s",
}, newProps)
}

func TestRelacePropsPlug(t *testing.T) {
props := map[string]interface{}{
"cacheTtl": 1000,
"url": "123",
"timeout": 1000,
"url": "123",
}
changed, newProps := ReplacePropsWithPlug("", props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"url": "123",
"timeout": "1s",
"url": "123",
}, newProps)
props = map[string]interface{}{
"cacheTtl": 1000,
"url": "123",
"timeout": 1000,
"url": "123",
}
changed, newProps = ReplacePropsWithPlug("sql", props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"cacheTtl": "1s",
"dburl": "123",
"timeout": "1s",
"dburl": "123",
}, newProps)
}

func TestReplaceCacheTtl(t *testing.T) {
props := map[string]interface{}{
"lookup": map[string]interface{}{
"a": 1,
"cacheTtl": 100,
},
"a": "b",
}
changed, newProps := ReplacePropsWithPlug("sql", props)
require.True(t, changed)
require.Equal(t, map[string]interface{}{
"lookup": map[string]interface{}{
"a": 1,
"cacheTtl": "100ms",
},
"a": "b",
}, newProps)
}

0 comments on commit 740a5d5

Please sign in to comment.