Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide CaseSensitive option to ignore case of keys or not #258

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions server/datasource/etcd/kv/kv_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func (s *Dao) Exist(ctx context.Context, key, project, domain string, options ..
datasource.WithExactLabels(),
datasource.WithLabels(opts.Labels),
datasource.WithLabelFormat(opts.LabelFormat),
datasource.WithKey(key))
datasource.WithKey(key),
datasource.WithCaseSensitive())
if err != nil {
openlog.Error("check kv exist: " + err.Error())
return false, err
Expand Down Expand Up @@ -485,7 +486,10 @@ func toRegex(opts datasource.FindOptions) (*regexp.Regexp, error) {
default:
value = strings.ReplaceAll(opts.Key, ".", "\\.")
}
value = "(?i)^" + value + "$"
value = "^" + value + "$"
if !opts.CaseSensitive {
value = "(?i)" + value
}
regex, err := regexp.Compile(value)
if err != nil {
openlog.Error("invalid wildcard expr: " + value + ", error: " + err.Error())
Expand Down
10 changes: 9 additions & 1 deletion server/datasource/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ type FindOptions struct {
// Offset the offset of the response, start at 0
Offset int64
// Limit the page size of the response, dot not paging if limit=0
Limit int64
Limit int64
CaseSensitive bool
}

// WriteOption is functional option to create, update and delete kv
Expand All @@ -86,6 +87,13 @@ func WithSync(enabled bool) WriteOption {
}
}

// WithCaseSensitive tell model service whether to match case of letters or not.
func WithCaseSensitive() FindOption {
return func(o *FindOptions) {
o.CaseSensitive = true
}
}

// WithExactLabels tell model service to return only one kv matches the labels
func WithExactLabels() FindOption {
return func(o *FindOptions) {
Expand Down
17 changes: 17 additions & 0 deletions server/service/kv/kv_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ func TestService_Create(t *testing.T) {
assert.EqualError(t, err,
config.NewError(config.ErrRecordAlreadyExists, datasource.ErrKVAlreadyExists.Error()).Error())
})
t.Run("create the kv with uppercase, expected: create successfully", func(t *testing.T) {
result, err := kvsvc.Create(context.TODO(), &model.KVDoc{
Key: "TIMEOUT",
Value: "2s",
Status: common.StatusEnabled,
Labels: map[string]string{
"app": "mall",
"service": "utCart",
},
Domain: domain,
Project: project,
})
assert.Nil(t, err)
assert.NotEmpty(t, result.ID)
assert.Equal(t, "TIMEOUT", result.Key)
assert.Equal(t, "2s", result.Value)
})
t.Run("list the kv", func(t *testing.T) {
res, err := kvsvc.List(context.TODO(), project, domain,
datasource.WithKey("wildcard(time*1)"))
Expand Down