Skip to content

Commit 740a5d5

Browse files
Yisaerngjaying
authored andcommitted
fix: replace props when get (#3409)
Signed-off-by: Song Gao <[email protected]>
1 parent 05338c1 commit 740a5d5

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

internal/meta/yamlConfigMeta.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ func GetYamlConf(configOperatorKey, language string) (b []byte, err error) {
232232
}
233233

234234
cf := cfgOps.CopyConfContent()
235+
for plug, props := range cf {
236+
changed, newProps := replace.ReplacePropsWithPlug(plug, props)
237+
if changed {
238+
cf[plug] = newProps
239+
}
240+
}
241+
235242
if b, err = json.Marshal(cf); nil != err {
236243
return nil, fmt.Errorf(`%s%v`, getMsg(language, source, "json_marshal_fail"), cf)
237244
} else {

pkg/replace/replace.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ func ReplacePassword(props map[string]interface{}) (bool, map[string]interface{}
105105
func ReplaceDuration(props map[string]interface{}) (bool, map[string]interface{}) {
106106
changed := false
107107
for _, replaceWord := range replaceDuration {
108+
if replaceWord == "cacheTtl" {
109+
vm, ok := props["lookup"]
110+
if ok {
111+
lookupm, ok := vm.(map[string]interface{})
112+
if ok {
113+
oldValue, ok := lookupm[replaceWord]
114+
if ok {
115+
intRaw, err := cast.ToInt(oldValue, cast.CONVERT_ALL)
116+
if err == nil {
117+
lookupm[replaceWord] = (time.Duration(intRaw) * time.Millisecond).String()
118+
changed = true
119+
props["lookup"] = lookupm
120+
continue
121+
}
122+
}
123+
124+
}
125+
}
126+
}
108127
v, ok := props[replaceWord]
109128
if ok {
110129
intRaw, err := cast.ToInt(v, cast.CONVERT_ALL)

pkg/replace/replace_test.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,50 +120,69 @@ func TestReplaceRuleJson(t *testing.T) {
120120

121121
func TestReplaceDuration(t *testing.T) {
122122
props := map[string]interface{}{
123-
"cacheTtl": 1000,
123+
"timeout": 1000,
124124
}
125125
changed, newProps := ReplaceDuration(props)
126126
require.True(t, changed)
127127
require.Equal(t, map[string]interface{}{
128-
"cacheTtl": "1s",
128+
"timeout": "1s",
129129
}, newProps)
130130
props = map[string]interface{}{
131-
"cacheTtl": int64(1000),
131+
"timeout": int64(1000),
132132
}
133133
changed, newProps = ReplaceDuration(props)
134134
require.True(t, changed)
135135
require.Equal(t, map[string]interface{}{
136-
"cacheTtl": "1s",
136+
"timeout": "1s",
137137
}, newProps)
138138
props = map[string]interface{}{
139-
"cacheTtl": float64(1000),
139+
"timeout": float64(1000),
140140
}
141141
changed, newProps = ReplaceDuration(props)
142142
require.True(t, changed)
143143
require.Equal(t, map[string]interface{}{
144-
"cacheTtl": "1s",
144+
"timeout": "1s",
145145
}, newProps)
146146
}
147147

148148
func TestRelacePropsPlug(t *testing.T) {
149149
props := map[string]interface{}{
150-
"cacheTtl": 1000,
151-
"url": "123",
150+
"timeout": 1000,
151+
"url": "123",
152152
}
153153
changed, newProps := ReplacePropsWithPlug("", props)
154154
require.True(t, changed)
155155
require.Equal(t, map[string]interface{}{
156-
"cacheTtl": "1s",
157-
"url": "123",
156+
"timeout": "1s",
157+
"url": "123",
158158
}, newProps)
159159
props = map[string]interface{}{
160-
"cacheTtl": 1000,
161-
"url": "123",
160+
"timeout": 1000,
161+
"url": "123",
162162
}
163163
changed, newProps = ReplacePropsWithPlug("sql", props)
164164
require.True(t, changed)
165165
require.Equal(t, map[string]interface{}{
166-
"cacheTtl": "1s",
167-
"dburl": "123",
166+
"timeout": "1s",
167+
"dburl": "123",
168+
}, newProps)
169+
}
170+
171+
func TestReplaceCacheTtl(t *testing.T) {
172+
props := map[string]interface{}{
173+
"lookup": map[string]interface{}{
174+
"a": 1,
175+
"cacheTtl": 100,
176+
},
177+
"a": "b",
178+
}
179+
changed, newProps := ReplacePropsWithPlug("sql", props)
180+
require.True(t, changed)
181+
require.Equal(t, map[string]interface{}{
182+
"lookup": map[string]interface{}{
183+
"a": 1,
184+
"cacheTtl": "100ms",
185+
},
186+
"a": "b",
168187
}, newProps)
169188
}

0 commit comments

Comments
 (0)