Skip to content

Commit

Permalink
dynamic params support regexp
Browse files Browse the repository at this point in the history
dynamic params support regexp
  • Loading branch information
wuhua3 committed Jan 2, 2025
1 parent f33fd57 commit 56f8c7b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/globalContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/weibocom/motan-go/log"
"os"
"reflect"
"regexp"
"strings"
"sync/atomic"
"unsafe"
Expand Down Expand Up @@ -360,6 +361,9 @@ func (c *Context) getDynamicParameters(dp map[interface{}]interface{}) map[strin
case map[interface{}]interface{}:
mv := v.(map[interface{}]interface{})
ph[sk] = mv[c.pool]
if ph[sk] == nil {
ph[sk] = c.getDynamicParametersByRegexp(mv)
}
if ph[sk] == nil {
ph[sk] = mv["default"]
}
Expand All @@ -371,6 +375,23 @@ func (c *Context) getDynamicParameters(dp map[interface{}]interface{}) map[strin
return ph
}

func (c *Context) getDynamicParametersByRegexp(mv map[interface{}]interface{}) interface{} {
for k, v := range mv {
ks, ok := k.(string)
if !ok {
continue
}
re, err := regexp.Compile(ks)
if err != nil {
continue
}
if re.MatchString(c.pool) {
return v
}
}
return nil
}

// parse host url including agenturl, clienturl, serverurl
func (c *Context) parseHostURL() {
agentInfo, _ := c.Config.GetSection(agentSection)
Expand Down
19 changes: 19 additions & 0 deletions core/globalContext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,22 @@ func TestContext_mergeFilterSet(t *testing.T) {
assert.Contains(t, "a,b,c", v)
}
}

func TestContext_getDynamicParametersByRegexp(t *testing.T) {
configFile := filepath.Join("testdata", "app.yaml")
cases := []struct {
pool string
expected string
}{
{"regexp", "regexp_default-param"},
{"reg-idc1", "reg-idx1-param"},
{"reg-idx2", "reg-idx2-param"},
{"xeg-idx3", "xeg-idx3-param"},
}
for _, c := range cases {
poolContext := NewContext(configFile, "app", c.pool)
testPlaceholder, err := poolContext.Config.GetSection("test_placeholder")
assert.Nil(t, err)
assert.Equal(t, c.expected, testPlaceholder["regexp"])
}
}
7 changes: 7 additions & 0 deletions core/testdata/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ dynamic-param:
app-idc2: ccc2
app-idc3: ccc3
default: ccc_default
regexp:
reg-idc1: reg-idx1-param
reg.*2$: reg-idx2-param
^x.*\d+$: xeg-idx3-param
default: regexp_default-param


test_placeholder:
aaa: "${aaa}"
Expand All @@ -24,3 +30,4 @@ test_placeholder:
- "xxx"
- "ddd"
- "xx"
regexp: "${regexp}"

0 comments on commit 56f8c7b

Please sign in to comment.