Skip to content

Commit ad9c657

Browse files
committed
chore: address CR reviews
1 parent 4864a71 commit ad9c657

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

store-client/pkg/client/mongodb_client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,8 @@ func normalizeMongoTypes(doc map[string]interface{}) {
11001100
}
11011101

11021102
// normalizeValue converts a single value, handling MongoDB-specific types and nested structures
1103+
//
1104+
//nolint:cyclop // flat type switch: one independent branch per BSON type
11031105
func normalizeValue(value interface{}) interface{} {
11041106
switch v := value.(type) {
11051107
case bson.DateTime:

store-client/pkg/datastore/providers/mongodb/health_store.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,12 @@ func normalizeValue(v interface{}) interface{} {
474474
return normalizeD(val)
475475
case bson.A:
476476
return normalizeArray(val)
477+
case bson.M:
478+
// bson.M is a defined type, not an alias for map[string]interface{}, so it
479+
// does not match the case below. The collection client sets
480+
// DefaultDocumentM, which makes this the shape documents actually decode
481+
// into -- without this case the whole value is returned un-normalized.
482+
return normalizeMap(val)
477483
case map[string]interface{}:
478484
return normalizeMap(val)
479485
case []interface{}:

store-client/pkg/datastore/providers/mongodb/health_store_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,34 @@ func TestNormalizeValue(t *testing.T) {
475475
map[string]interface{}{"already": "normalized"},
476476
},
477477
},
478+
{
479+
// The collection client sets DefaultDocumentM, so documents decode
480+
// into bson.M rather than bson.D. bson.M is a defined type, so it
481+
// does not match the map[string]interface{} case.
482+
name: "top-level bson.M with nested values",
483+
input: bson.M{
484+
"nodename": "test-node",
485+
"metadata": bson.M{
486+
"region": "us-west",
487+
},
488+
"nested_d": bson.D{{Key: "zone", Value: "a"}},
489+
"items": bson.A{
490+
bson.M{"name": "item1"},
491+
bson.D{{Key: "name", Value: "item2"}},
492+
},
493+
},
494+
expected: map[string]interface{}{
495+
"nodename": "test-node",
496+
"metadata": map[string]interface{}{
497+
"region": "us-west",
498+
},
499+
"nested_d": map[string]interface{}{"zone": "a"},
500+
"items": []interface{}{
501+
map[string]interface{}{"name": "item1"},
502+
map[string]interface{}{"name": "item2"},
503+
},
504+
},
505+
},
478506
{
479507
name: "primitive string",
480508
input: "test-string",

store-client/pkg/datastore/providers/mongodb/watcher/watch_store_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
3838
)
3939

40-
func TestNewChangeStreamWatcher(t *testing.T) {
40+
func TestNewChangeStreamWatcher_InvalidTLSPaths_ReturnsClientOptsError(t *testing.T) {
4141
t.Run("error in constructing client options", func(t *testing.T) {
4242
mongoConfig := MongoDBConfig{
4343
URI: "mongodb://localhost:27017",
@@ -952,7 +952,7 @@ func TestCopyStructFields(t *testing.T) {
952952
})
953953
}
954954

955-
func TestGetCollectionClient(t *testing.T) {
955+
func TestGetCollectionClient_InvalidConfig_ReturnsError(t *testing.T) {
956956
t.Run("error in constructing client options", func(t *testing.T) {
957957
mongoConfig := MongoDBConfig{
958958
URI: "mongodb://localhost:27017",

0 commit comments

Comments
 (0)