Skip to content

Commit 1e937a2

Browse files
authored
feat(update): Implements SetOnInsertAny function and method.(#89)
1 parent 69f1af0 commit 1e937a2

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

builder/update/bson_build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func SetOnInsert(key string, value any) bson.D {
7474
return bson.D{{Key: SetOnInsertOp, Value: bson.D{{Key: key, Value: value}}}}
7575
}
7676

77+
func SetOnInsertAny(value any) bson.D {
78+
return bson.D{{Key: SetOnInsertOp, Value: value}}
79+
}
80+
7781
func CurrentDate(key string, value any) bson.D {
7882
return bson.D{{Key: CurrentDateOp, Value: bson.D{{Key: key, Value: value}}}}
7983
}

builder/update/bson_build_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,50 @@ func TestSetOnInsert(t *testing.T) {
179179
})
180180
}
181181

182+
func TestSetOnInsertAny(t *testing.T) {
183+
type FieldStruct struct {
184+
Name string `bson:"name,omitempty"`
185+
Age int `bson:"age,omitempty"`
186+
}
187+
188+
testCases := []struct {
189+
name string
190+
value any
191+
want bson.D
192+
}{
193+
{
194+
name: "nil value",
195+
value: nil,
196+
want: bson.D{bson.E{Key: "$setOnInsert", Value: nil}},
197+
},
198+
{
199+
name: "string value",
200+
value: "Alice",
201+
want: bson.D{bson.E{Key: "$setOnInsert", Value: "Alice"}},
202+
},
203+
{
204+
name: "map value",
205+
value: map[string]any{"name": "Alice"},
206+
want: bson.D{bson.E{Key: "$setOnInsert", Value: map[string]any{"name": "Alice"}}},
207+
},
208+
{
209+
name: "struct value",
210+
value: FieldStruct{Name: "Alice", Age: 18},
211+
want: bson.D{bson.E{Key: "$setOnInsert", Value: FieldStruct{Name: "Alice", Age: 18}}},
212+
},
213+
{
214+
name: "bson value",
215+
value: bson.D{bson.E{Key: "name", Value: "Alice"}},
216+
want: bson.D{bson.E{Key: "$setOnInsert", Value: bson.D{bson.E{Key: "name", Value: "Alice"}}}},
217+
},
218+
}
219+
for _, tc := range testCases {
220+
t.Run(tc.name, func(t *testing.T) {
221+
assert.Equal(t, tc.want, SetOnInsertAny(tc.value))
222+
})
223+
}
224+
}
225+
182226
func TestCurrentDate(t *testing.T) {
183227
t.Run("test CurrentDate", func(t *testing.T) {
184228
assert.Equal(t, bson.D{bson.E{Key: "$currentDate", Value: bson.D{bson.E{Key: "lastModified", Value: true}}}}, CurrentDate("lastModified", true))

builder/update/field_update_builder.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func (b *fieldUpdateBuilder) SetOnInsert(key string, value any) *Builder {
5656
return b.parent
5757
}
5858

59+
func (b *fieldUpdateBuilder) SetOnInsertAny(value any) *Builder {
60+
e := bson.E{Key: SetOnInsertOp, Value: value}
61+
if !b.parent.tryMergeValue(SetOnInsertOp, e) {
62+
b.parent.data = append(b.parent.data, e)
63+
}
64+
return b.parent
65+
}
66+
5967
func (b *fieldUpdateBuilder) CurrentDate(key string, value any) *Builder {
6068
e := bson.E{Key: key, Value: value}
6169
if !b.parent.tryMergeValue(CurrentDateOp, e) {

builder/update/field_update_builder_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,45 @@ func Test_fieldUpdateBuilder_SetOnInsert(t *testing.T) {
106106
})
107107
}
108108

109+
func Test_fieldUpdateBuilder_SetOnInsertAny(t *testing.T) {
110+
testCases := []struct {
111+
name string
112+
value any
113+
want bson.D
114+
}{
115+
{
116+
name: "nil value",
117+
value: nil,
118+
want: bson.D{bson.E{Key: "$setOnInsert", Value: nil}},
119+
},
120+
{
121+
name: "string value",
122+
value: "Alice",
123+
want: bson.D{bson.E{Key: "$setOnInsert", Value: "Alice"}},
124+
},
125+
{
126+
name: "map value",
127+
value: map[string]any{"name": "Alice"},
128+
want: bson.D{bson.E{Key: "$setOnInsert", Value: map[string]any{"name": "Alice"}}},
129+
},
130+
{
131+
name: "struct value",
132+
value: struct{ Name string }{Name: "Alice"},
133+
want: bson.D{bson.E{Key: "$setOnInsert", Value: struct{ Name string }{Name: "Alice"}}},
134+
},
135+
{
136+
name: "bson value",
137+
value: bson.D{bson.E{Key: "name", Value: "Alice"}},
138+
want: bson.D{bson.E{Key: "$setOnInsert", Value: bson.D{bson.E{Key: "name", Value: "Alice"}}}},
139+
},
140+
}
141+
for _, tc := range testCases {
142+
t.Run(tc.name, func(t *testing.T) {
143+
assert.Equal(t, tc.want, NewBuilder().SetOnInsertAny(tc.value).Build())
144+
})
145+
}
146+
}
147+
109148
func Test_fieldUpdateBuilder_Inc(t *testing.T) {
110149
t.Run("single operation", func(t *testing.T) {
111150
assert.Equal(t, bson.D{{Key: "$inc", Value: bson.D{bson.E{Key: "orders", Value: 1}}}}, NewBuilder().Inc("orders", 1).Build())

0 commit comments

Comments
 (0)