From 7886a35d6375b6ae4b407c3ce1efc6fb1d0f7c14 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Wed, 6 Dec 2023 02:42:20 -0500 Subject: [PATCH] Rename composeURI func to composeMongoURI To reflect that it's used exclusively for MongoDB (for now at least). Also added a descriptive comment to the function. --- engine/storage_mongo_datadb.go | 2 +- engine/storage_test.go | 2 +- engine/storage_utils.go | 12 +++++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 5b18e2ab3..038b8fc6e 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -158,7 +158,7 @@ func NewMongoStorage(host, port, db, user, pass, mrshlerStr string, storageType storageType: storageType, counter: utils.NewCounter(time.Now().UnixNano(), 0), } - uri := composeURI("mongodb", host, port, db, user, pass) + uri := composeMongoURI("mongodb", host, port, db, user, pass) reg := bson.NewRegistry() decimalType := reflect.TypeOf(utils.Decimal{}) reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(decimalEncoder)) diff --git a/engine/storage_test.go b/engine/storage_test.go index 4dcbef3be..8a29e7ca6 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -1906,7 +1906,7 @@ func TestComposeURI(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - url := composeURI(tt.scheme, tt.host, tt.port, tt.db, tt.user, tt.pass) + url := composeMongoURI(tt.scheme, tt.host, tt.port, tt.db, tt.user, tt.pass) if url != tt.expected { t.Errorf("expected %v,\nreceived %v", tt.expected, url) } diff --git a/engine/storage_utils.go b/engine/storage_utils.go index 95a6318cb..2c2830412 100644 --- a/engine/storage_utils.go +++ b/engine/storage_utils.go @@ -81,7 +81,17 @@ func NewStorDBConn(dbType, host, port, name, user, pass, marshaler string, return } -func composeURI(scheme, host, port, db, user, pass string) string { +// composeMongoURI constructs a MongoDB URI from the given parameters: +// - scheme: only "mongodb" for now. +// - host: MongoDB server host (e.g., "localhost"). +// - port: MongoDB server port, excluded if "0". +// - db: Database name, may include additional parameters (e.g., "db?retryWrites=true"). +// - user: Username for auth, omitted if empty. +// - pass: Password for auth, only if username is set. +// +// TODO: Decide whether to replace the 'scheme' string parameter with a boolean once support +// for "mongodb+srv" is added. +func composeMongoURI(scheme, host, port, db, user, pass string) string { uri := scheme + "://" if user != "" && pass != "" { uri += user + ":" + pass + "@"