This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git


The following commit(s) were added to refs/heads/master by this push:
     new a4052ee  SCB-2096 The configuration center supports fuzzy query (#163)
a4052ee is described below

commit a4052ee58bdae0fb17fd00efb30e76f56e63c7bd
Author: develpoerX <[email protected]>
AuthorDate: Mon Nov 9 10:26:44 2020 +0800

    SCB-2096 The configuration center supports fuzzy query (#163)
    
    * 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
---
 pkg/validate/instance.go               |  3 ++-
 pkg/validate/instance_test.go          | 15 +++++++++++++++
 server/resource/v1/kv_resource_test.go | 15 +++++++++++++++
 server/service/mongo/kv/kv_dao.go      | 12 ++++++++++++
 4 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/pkg/validate/instance.go b/pkg/validate/instance.go
index a55499d..a85b6f3 100644
--- a/pkg/validate/instance.go
+++ b/pkg/validate/instance.go
@@ -5,6 +5,7 @@ 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]*$`
 )
 
@@ -12,7 +13,7 @@ const (
 // 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),
diff --git a/pkg/validate/instance_test.go b/pkg/validate/instance_test.go
index 8cc0952..0519656 100644
--- a/pkg/validate/instance_test.go
+++ b/pkg/validate/instance_test.go
@@ -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))
 }
diff --git a/server/resource/v1/kv_resource_test.go 
b/server/resource/v1/kv_resource_test.go
index 6a07d27..520ffef 100644
--- a/server/resource/v1/kv_resource_test.go
+++ b/server/resource/v1/kv_resource_test.go
@@ -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")
diff --git a/server/service/mongo/kv/kv_dao.go 
b/server/service/mongo/kv/kv_dao.go
index 10e750c..138776f 100644
--- a/server/service/mongo/kv/kv_dao.go
+++ b/server/service/mongo/kv/kv_dao.go
@@ -20,6 +20,8 @@ package kv
 import (
        "context"
        "fmt"
+       "regexp"
+       "strings"
        "time"
 
        "github.com/apache/servicecomb-kie/pkg/model"
@@ -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 {

Reply via email to