You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/11/24 12:17:30 UTC

[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #754: SCB-2094 implement basic service/rule/schema/tag interface

tianxiaoliang commented on a change in pull request #754:
URL: https://github.com/apache/servicecomb-service-center/pull/754#discussion_r529411197



##########
File path: datasource/mongo/client/mongo.go
##########
@@ -146,7 +148,20 @@ func (mc *MongoClient) Insert(ctx context.Context, Table string, document interf
 	if col == nil {
 		return nil, ErrCollectionsNil
 	}
-	return col.InsertOne(ctx, document, opts...)
+	res, err := col.InsertOne(ctx, document, opts...)
+	if err != nil {
+		switch tt := err.(type) {

Review comment:
       这里抽个公共函数,所有的insert就都能用了

##########
File path: datasource/mongo/account.go
##########
@@ -47,21 +46,12 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a *rbacframe.Account) e
 	}
 	a.Password = stringutil.Bytes2str(hash)
 	a.ID = util.GenerateUUID()
-	_, err = client.GetMongoClient().Insert(ctx, CollectionAccount, a)
+	res, err := client.GetMongoClient().Insert(ctx, CollectionAccount, a)
 	if err != nil {
-		switch tt := err.(type) {
-		case mongo.WriteException:
-			if tt.WriteErrors != nil {
-				for _, writeError := range tt.WriteErrors {
-					// The index is setup.The key is repeated.
-					if writeError.Code == DuplicateKey {
-						return datasource.ErrDuplicated
-					}
-				}
-			}
-		default:
-			return err
-		}
+		return err
+	}
+	if res == nil {
+		return datasource.ErrDuplicated

Review comment:
       所以这里也要用公共函数,我希望解决代码易读问题,而不是简化必要的逻辑




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org