little-cui commented on a change in pull request #1204:
URL:
https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779972260
##########
File path: datasource/etcd/account.go
##########
@@ -83,13 +84,13 @@ func (ds *RbacDAO) CreateAccount(ctx context.Context, a
*rbacmodel.Account) erro
log.Error("", err)
return err
}
- if datasource.EnableSync {
- op, err := GenTaskOpts("", "", sync.CreateAction,
datasource.ResourceAccount, a)
- if err != nil {
- log.Error("", err)
- return err
- }
- opts = append(opts, op)
+ syncOpts, err := esync.GenSyncOpts(ctx, sync.CreateAction,
datasource.ResourceAccount, a)
+ if err != nil {
+ log.Error("fail to create sync opts", err)
+ return err
+ }
+ if len(syncOpts) != 0 {
+ opts = append(opts, syncOpts...)
Review comment:
不需要判断长度,go内部已经优化
##########
File path: datasource/etcd/account.go
##########
@@ -194,20 +195,15 @@ func (ds *RbacDAO) DeleteAccount(ctx context.Context,
names []string) (bool, err
continue //do not fail if some account is invalid
}
- if datasource.EnableSync {
- taskOpt, err := GenTaskOpts("", "", sync.DeleteAction,
datasource.ResourceAccount, a)
- if err != nil {
- log.Error("", err)
- return false, err
- }
- tombstoneOpt, err := GenTombstoneOpts("", "",
datasource.ResourceAccount, a.Name)
- if err != nil {
- log.Error("", err)
- return false, err
- }
- opts = append(opts, tombstoneOpt, taskOpt)
+ syncOpts, err := esync.GenSyncOpts(ctx, sync.DeleteAction,
datasource.ResourceAccount, a, esync.WithResourceID(a.Name))
Review comment:
resourceID是必填字段,不做optional,value是可选
##########
File path: datasource/etcd/sync/sync.go
##########
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package sync
+
+import (
+ "context"
+ "encoding/json"
+
+ "github.com/go-chassis/cari/sync"
+ "github.com/little-cui/etcdadpt"
+
+ "github.com/apache/servicecomb-service-center/datasource"
+
"github.com/apache/servicecomb-service-center/eventbase/datasource/etcd/key"
+ "github.com/apache/servicecomb-service-center/pkg/util"
+)
+
+type Options struct {
+ ResourceID string
+ Opts map[string]string
+}
+
+type Option func(options *Options)
+
+func NewSyncOptions() Options {
+ return Options{}
+}
+
+func WithResourceID(resourceID string) Option {
+ return func(options *Options) {
+ options.ResourceID = resourceID
+ }
+}
+
+func WithOpts(opts map[string]string) Option {
+ return func(options *Options) {
+ options.Opts = opts
+ }
+}
+
+func GenSyncOpts(ctx context.Context, action string, resourceType string,
resource interface{},
+ options ...Option) ([]etcdadpt.OpOptions, error) {
+ opts := make([]etcdadpt.OpOptions, 0)
+ if !datasource.EnableSync {
+ return opts, nil
+ }
+ syncOpts := NewSyncOptions()
+ for _, option := range options {
+ option(&syncOpts)
+ }
+ taskOpt, err := genTaskOpt(ctx, action, resourceType, resource,
&syncOpts)
+ if err != nil {
+ return opts, err
+ }
+ opts = append(opts, taskOpt)
+ if action == sync.DeleteAction {
Review comment:
取反,提前return,减少复杂度
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]