From 94013b05e5914fe9defc815f4076a0ff57815292 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 18 Nov 2023 03:51:21 +0000 Subject: [PATCH] fix the code style isseus --- .remarkrc | 1 + extensions/store-mongodb/pkg/client.go | 40 ++++++++++++-------------- 2 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 .remarkrc diff --git a/.remarkrc b/.remarkrc new file mode 100644 index 00000000..81c7c819 --- /dev/null +++ b/.remarkrc @@ -0,0 +1 @@ +listItemIndent=one diff --git a/extensions/store-mongodb/pkg/client.go b/extensions/store-mongodb/pkg/client.go index 98fba5fe..5065083b 100644 --- a/extensions/store-mongodb/pkg/client.go +++ b/extensions/store-mongodb/pkg/client.go @@ -30,6 +30,7 @@ import ( "fmt" "github.com/linuxsuren/api-testing/pkg/testing/remote" + "github.com/linuxsuren/api-testing/pkg/util" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -38,30 +39,27 @@ func (s *dbserver) getClient(ctx context.Context) (collection *mongo.Collection, store := remote.GetStoreFromContext(ctx) if store == nil { err = errors.New("no connect to mongodb") - } else { - databaseName := "testing" - collectionName := "atest" + return + } - if store.Properties != nil { - if v, ok := store.Properties["database"]; ok && v != "" { - databaseName = v - } - if v, ok := store.Properties["collection"]; ok && v != "" { - collectionName = v - } - } + if store.Properties == nil { + store.Properties = map[string]string{} + } + + databaseName := util.EmptyThenDefault(store.Properties["database"], "testing") + collectionName := util.EmptyThenDefault(store.Properties["collection"], "atest") + + address := fmt.Sprintf("mongodb://%s:%s@%s", store.Username, store.Password, store.URL) + var client *mongo.Client + client, err = mongo.Connect(ctx, options.Client().ApplyURI(address)) + if err != nil { + return + } - address := fmt.Sprintf("mongodb://%s:%s@%s", store.Username, store.Password, store.URL) - var client *mongo.Client - client, err = mongo.Connect(ctx, options.Client().ApplyURI(address)) - // TODO need a way to maintain the connection of it - if err == nil { + collection = client.Database(databaseName).Collection(collectionName) + if collection == nil { + if err = client.Database(databaseName).CreateCollection(ctx, collectionName); err == nil { collection = client.Database(databaseName).Collection(collectionName) - if collection == nil { - if err = client.Database(databaseName).CreateCollection(ctx, collectionName); err == nil { - collection = client.Database(databaseName).Collection(collectionName) - } - } } } return