Skip to content

Commit

Permalink
SCB-2096 The configuration center supports fuzzy query (#163)
Browse files Browse the repository at this point in the history
* SCB-2096 The configuration center supports fuzzy query

* SCB-2096 The configuration center supports fuzzy query

* SCB-2096 The configuration center supports fuzzy query

* SCB-2096 The configuration center supports fuzzy query
  • Loading branch information
develpoerX committed Nov 9, 2020
1 parent ca26bad commit a4052ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/validate/instance.go
Expand Up @@ -5,14 +5,15 @@ var defaultValidator = NewValidator()
const (
key = "key"
commonNameRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`
getKeyRegexString = `^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$|^beginWith\([a-zA-Z0-9][a-zA-Z0-9_\-.]*\)$`
asciiRegexString = `^[\x00-\x7F]*$`
)

// custom validate rules
// please use different tag names from third party tags
var customRules = []*RegexValidateRule{
NewRule(key, commonNameRegexString, &Option{Min: 1, Max: 128}),
NewRule("getKey", commonNameRegexString, &Option{Max: 128}),
NewRule("getKey", getKeyRegexString, &Option{Max: 128}),
NewRule("commonName", commonNameRegexString, &Option{Min: 1, Max: 256}),
NewRule("valueType", `^$|^(ini|json|text|yaml|properties)$`, nil),
NewRule("kvStatus", `^$|^(enabled|disabled)$`, nil),
Expand Down
15 changes: 15 additions & 0 deletions pkg/validate/instance_test.go
Expand Up @@ -107,4 +107,19 @@ func TestValidate(t *testing.T) {
Labels: map[string]string{string32 + "a": "a"},
}
assert.Error(t, validate.Validate(kvDoc))

ListKVRe := &model.ListKVRequest{Project: "a", Domain: "a",
Key: "beginWith(a)",
}
assert.NoError(t, validate.Validate(ListKVRe))

ListKVRe = &model.ListKVRequest{Project: "a", Domain: "a",
Key: "beginW(a)",
}
assert.Error(t, validate.Validate(ListKVRe))

ListKVRe = &model.ListKVRequest{Project: "a", Domain: "a",
Key: "beginW()",
}
assert.Error(t, validate.Validate(ListKVRe))
}
15 changes: 15 additions & 0 deletions server/resource/v1/kv_resource_test.go
Expand Up @@ -315,6 +315,21 @@ func TestKVResource_List(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, len(result.Data))
})
t.Run("get one key, fuzzy match,should return 2 kv", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/v1/kv_test/kie/kv?key=beginWith(time)", nil)
r.Header.Set("Content-Type", "application/json")
kvr := &v1.KVResource{}
c, err := restfultest.New(kvr, nil)
assert.NoError(t, err)
resp := httptest.NewRecorder()
c.ServeHTTP(resp, r)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
result := &model.KVResponse{}
err = json.Unmarshal(body, result)
assert.NoError(t, err)
assert.Equal(t, 2, len(result.Data))
})
t.Run("get one key by service label should return 2 kv,delete one", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/v1/kv_test/kie/kv?key=timeout&label=service:utService", nil)
r.Header.Set("Content-Type", "application/json")
Expand Down
12 changes: 12 additions & 0 deletions server/service/mongo/kv/kv_dao.go
Expand Up @@ -20,6 +20,8 @@ package kv
import (
"context"
"fmt"
"regexp"
"strings"
"time"

"github.com/apache/servicecomb-kie/pkg/model"
Expand Down Expand Up @@ -108,12 +110,22 @@ func updateKeyValue(ctx context.Context, kv *model.KVDoc) error {

}

//Extract key values
func getValue(str string) string {
rex := regexp.MustCompile(`\(([^)]+)\)`)
res := rex.FindStringSubmatch(str)
return res[len(res)-1]
}

func findKV(ctx context.Context, domain string, project string, opts service.FindOptions) (*mongo.Cursor, int, error) {
collection := session.GetDB().Collection(session.CollectionKV)
ctx, _ = context.WithTimeout(ctx, opts.Timeout)
filter := bson.M{"domain": domain, "project": project}
if opts.Key != "" {
filter["key"] = opts.Key
if strings.HasPrefix(opts.Key, "beginWith") {
filter["key"] = bson.M{"$regex": getValue(opts.Key), "$options": "$i"}
}
}
if len(opts.Labels) != 0 {
for k, v := range opts.Labels {
Expand Down

0 comments on commit a4052ee

Please sign in to comment.