Skip to content

Commit 7ef90d9

Browse files
authoredOct 6, 2020
added better interoperability with Mongo's object id (#1344)
1 parent b29a6b9 commit 7ef90d9

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
 

‎gateway/modules/crud/mgo/read.go

+2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ func (m *Mongo) Read(ctx context.Context, col string, req *model.ReadRequest) (i
116116
results := []interface{}{}
117117

118118
if len(req.Aggregate) > 0 {
119+
helpers.Logger.LogDebug(helpers.GetRequestID(ctx), "Mongo aggregate", map[string]interface{}{"pipeline": pipeline})
119120
cur, err = collection.Aggregate(ctx, pipeline)
120121
} else {
122+
helpers.Logger.LogDebug(helpers.GetRequestID(ctx), "Mongo query", map[string]interface{}{"find": req.Find, "options": findOptions})
121123
cur, err = collection.Find(ctx, req.Find, findOptions)
122124
}
123125
if err != nil {

‎gateway/utils/store.go

+59
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/spaceuptech/helpers"
13+
"go.mongodb.org/mongo-driver/bson/primitive"
1314
)
1415

1516
// Adjust loads value from state if referenced
@@ -77,6 +78,64 @@ func LoadValue(key string, state map[string]interface{}) (interface{}, error) {
7778
pre := strings.IndexRune(function, '(')
7879
post := strings.IndexRune(function, ')')
7980
params := splitVariable(function[pre+1:len(function)-1], ',')
81+
82+
// Method check - stringToObjectId
83+
if strings.HasPrefix(function, "stringToObjectId") {
84+
value, err := LoadValue(function[pre+1:post], state)
85+
if err != nil {
86+
return nil, err
87+
}
88+
89+
if v, ok := value.(primitive.A); ok {
90+
value = []interface{}(v)
91+
}
92+
93+
// The value can be a string or an array of string
94+
switch v := value.(type) {
95+
case primitive.ObjectID:
96+
return v, nil
97+
case string:
98+
return primitive.ObjectIDFromHex(v)
99+
case []interface{}:
100+
array := make([]interface{}, len(v))
101+
for i, item := range v {
102+
s, ok := item.(string)
103+
if !ok {
104+
return nil, fmt.Errorf("value (%v) cannot be converted to ObjectID", item)
105+
}
106+
107+
objID, err := primitive.ObjectIDFromHex(s)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
array[i] = objID
113+
}
114+
return array, nil
115+
default:
116+
return nil, fmt.Errorf("invalid type provided (%s) for object id conversion", reflect.TypeOf(value))
117+
}
118+
}
119+
120+
// Method check - objectIdToString
121+
if strings.HasPrefix(function, "objectIdToString") {
122+
value, err := LoadValue(function[pre+1:post], state)
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
switch val := value.(type) {
128+
case string:
129+
return val, nil
130+
131+
case primitive.ObjectID:
132+
return val.Hex(), nil
133+
134+
default:
135+
return nil, fmt.Errorf("invalid type provided (%s) for object id conversion", reflect.TypeOf(value))
136+
}
137+
}
138+
80139
if strings.HasPrefix(function, "exists") {
81140
_, err := LoadValue(function[pre+1:post], state)
82141
return err == nil, nil

‎gateway/utils/store_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"reflect"
66
"testing"
77
"time"
8+
9+
"go.mongodb.org/mongo-driver/bson/primitive"
810
)
911

1012
func TestStoreValue(t *testing.T) {
@@ -599,6 +601,97 @@ func TestLoadNumber(t *testing.T) {
599601
}
600602
}
601603

604+
func TestLoadValueMongo(t *testing.T) {
605+
objectID, _ := primitive.ObjectIDFromHex("5f7c4770582dc480c95ec67e")
606+
type args struct {
607+
key string
608+
state map[string]interface{}
609+
}
610+
tests := []struct {
611+
name string
612+
args args
613+
want interface{}
614+
wantErr bool
615+
}{
616+
{
617+
name: "string to object id",
618+
args: args{
619+
key: "utils.stringToObjectId(args.string)",
620+
state: map[string]interface{}{"string": "5f7c4770582dc480c95ec67e"},
621+
},
622+
want: objectID,
623+
},
624+
{
625+
name: "string array to object id",
626+
args: args{
627+
key: "utils.stringToObjectId(args.string)",
628+
state: map[string]interface{}{"string": []interface{}{"5f7c4770582dc480c95ec67e", "5f7c4770582dc480c95ec67e"}},
629+
},
630+
want: []interface{}{objectID, objectID},
631+
},
632+
{
633+
name: "string array (primitive.A) to object id",
634+
args: args{
635+
key: "utils.stringToObjectId(args.string)",
636+
state: map[string]interface{}{"string": primitive.A{"5f7c4770582dc480c95ec67e", "5f7c4770582dc480c95ec67e"}},
637+
},
638+
want: []interface{}{objectID, objectID},
639+
},
640+
{
641+
name: "object id to object id",
642+
args: args{
643+
key: "utils.stringToObjectId(args.string)",
644+
state: map[string]interface{}{"string": objectID},
645+
},
646+
want: objectID,
647+
},
648+
{
649+
name: "invalid string to object id",
650+
args: args{
651+
key: "utils.stringToObjectId(args.string)",
652+
state: map[string]interface{}{"string": "5f7c4770582dc480c95ec67edd"},
653+
},
654+
wantErr: true,
655+
},
656+
{
657+
name: "string to string",
658+
args: args{
659+
key: "utils.objectIdToString(args.obj)",
660+
state: map[string]interface{}{"obj": "5f7c4770582dc480c95ec67e"},
661+
},
662+
want: "5f7c4770582dc480c95ec67e",
663+
},
664+
{
665+
name: "object id to string",
666+
args: args{
667+
key: "utils.objectIdToString(args.obj)",
668+
state: map[string]interface{}{"obj": objectID},
669+
},
670+
want: "5f7c4770582dc480c95ec67e",
671+
},
672+
{
673+
name: "random stuff to string",
674+
args: args{
675+
key: "utils.objectIdToString(args.obj)",
676+
state: map[string]interface{}{"obj": 12},
677+
},
678+
wantErr: true,
679+
},
680+
}
681+
for _, tt := range tests {
682+
t.Run(tt.name, func(t *testing.T) {
683+
got, err := LoadValue(tt.args.key, map[string]interface{}{"args": tt.args.state})
684+
if (err != nil) != tt.wantErr {
685+
t.Errorf("LoadValue() error = %v, wantErr %v", err, tt.wantErr)
686+
return
687+
}
688+
if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
689+
t.Errorf("LoadValue() = %v, want %v", got, tt.want)
690+
}
691+
})
692+
}
693+
}
694+
602695
func TestLoadValue(t *testing.T) {
603696
type args struct {
604697
key string

0 commit comments

Comments
 (0)