Skip to content

Commit 379040f

Browse files
update:
- 1、fixed:add key parameter for Each Position, Slice, Sort methods - 2、builder add tryMergeValue method
1 parent 58ad055 commit 379040f

File tree

4 files changed

+266
-114
lines changed

4 files changed

+266
-114
lines changed

builder/update/array_update_builder.go

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -116,87 +116,138 @@ func (b *arrayUpdateBuilder) PullAllString(key string, values ...string) *Builde
116116
return b.parent
117117
}
118118

119-
func (b *arrayUpdateBuilder) Each(values ...any) *Builder {
120-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
119+
func (b *arrayUpdateBuilder) Each(key string, values ...any) *Builder {
120+
e := bson.E{Key: types.Each, Value: values}
121+
if !b.parent.tryMergeValue(key, e) {
122+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
123+
}
121124
return b.parent
122125
}
123126

124-
func (b *arrayUpdateBuilder) EachInt(values ...int) *Builder {
125-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
127+
func (b *arrayUpdateBuilder) EachInt(key string, values ...int) *Builder {
128+
e := bson.E{Key: types.Each, Value: values}
129+
if !b.parent.tryMergeValue(key, e) {
130+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
131+
}
126132
return b.parent
127133
}
128134

129-
func (b *arrayUpdateBuilder) EachInt8(values ...int8) *Builder {
130-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
135+
func (b *arrayUpdateBuilder) EachInt8(key string, values ...int8) *Builder {
136+
e := bson.E{Key: types.Each, Value: values}
137+
if !b.parent.tryMergeValue(key, e) {
138+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
139+
}
131140
return b.parent
132141
}
133142

134-
func (b *arrayUpdateBuilder) EachInt16(values ...int16) *Builder {
135-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
143+
func (b *arrayUpdateBuilder) EachInt16(key string, values ...int16) *Builder {
144+
e := bson.E{Key: types.Each, Value: values}
145+
if !b.parent.tryMergeValue(key, e) {
146+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
147+
}
136148
return b.parent
137149
}
138150

139-
func (b *arrayUpdateBuilder) EachInt32(values ...int32) *Builder {
140-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
151+
func (b *arrayUpdateBuilder) EachInt32(key string, values ...int32) *Builder {
152+
e := bson.E{Key: types.Each, Value: values}
153+
if !b.parent.tryMergeValue(key, e) {
154+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
155+
}
141156
return b.parent
142157
}
143158

144-
func (b *arrayUpdateBuilder) EachInt64(values ...int64) *Builder {
145-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
159+
func (b *arrayUpdateBuilder) EachInt64(key string, values ...int64) *Builder {
160+
e := bson.E{Key: types.Each, Value: values}
161+
if !b.parent.tryMergeValue(key, e) {
162+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
163+
}
146164
return b.parent
147165
}
148166

149-
func (b *arrayUpdateBuilder) EachUint(values ...uint) *Builder {
150-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
167+
func (b *arrayUpdateBuilder) EachUint(key string, values ...uint) *Builder {
168+
e := bson.E{Key: types.Each, Value: values}
169+
if !b.parent.tryMergeValue(key, e) {
170+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
171+
}
151172
return b.parent
152173
}
153174

154-
func (b *arrayUpdateBuilder) EachUint8(values ...uint8) *Builder {
155-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
175+
func (b *arrayUpdateBuilder) EachUint8(key string, values ...uint8) *Builder {
176+
e := bson.E{Key: types.Each, Value: values}
177+
if !b.parent.tryMergeValue(key, e) {
178+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
179+
}
156180
return b.parent
157181
}
158182

159-
func (b *arrayUpdateBuilder) EachUint16(values ...uint16) *Builder {
160-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
183+
func (b *arrayUpdateBuilder) EachUint16(key string, values ...uint16) *Builder {
184+
e := bson.E{Key: types.Each, Value: values}
185+
if !b.parent.tryMergeValue(key, e) {
186+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
187+
}
161188
return b.parent
162189
}
163190

164-
func (b *arrayUpdateBuilder) EachUint32(values ...uint32) *Builder {
165-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
191+
func (b *arrayUpdateBuilder) EachUint32(key string, values ...uint32) *Builder {
192+
e := bson.E{Key: types.Each, Value: values}
193+
if !b.parent.tryMergeValue(key, e) {
194+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
195+
}
166196
return b.parent
167197
}
168198

169-
func (b *arrayUpdateBuilder) EachUint64(values ...uint64) *Builder {
170-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
199+
func (b *arrayUpdateBuilder) EachUint64(key string, values ...uint64) *Builder {
200+
e := bson.E{Key: types.Each, Value: values}
201+
if !b.parent.tryMergeValue(key, e) {
202+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
203+
}
171204
return b.parent
172205
}
173206

174-
func (b *arrayUpdateBuilder) EachFloat32(values ...float32) *Builder {
175-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
207+
func (b *arrayUpdateBuilder) EachFloat32(key string, values ...float32) *Builder {
208+
e := bson.E{Key: types.Each, Value: values}
209+
if !b.parent.tryMergeValue(key, e) {
210+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
211+
}
176212
return b.parent
177213
}
178214

179-
func (b *arrayUpdateBuilder) EachFloat64(values ...float64) *Builder {
180-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
215+
func (b *arrayUpdateBuilder) EachFloat64(key string, values ...float64) *Builder {
216+
e := bson.E{Key: types.Each, Value: values}
217+
if !b.parent.tryMergeValue(key, e) {
218+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
219+
}
181220
return b.parent
182221
}
183222

184-
func (b *arrayUpdateBuilder) EachString(values ...string) *Builder {
185-
b.parent.data = append(b.parent.data, bson.E{Key: types.Each, Value: values})
223+
func (b *arrayUpdateBuilder) EachString(key string, values ...string) *Builder {
224+
e := bson.E{Key: types.Each, Value: values}
225+
if !b.parent.tryMergeValue(key, e) {
226+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
227+
}
186228
return b.parent
187229
}
188230

189-
func (b *arrayUpdateBuilder) Position(value int) *Builder {
190-
b.parent.data = append(b.parent.data, bson.E{Key: types.Position, Value: value})
231+
func (b *arrayUpdateBuilder) Position(key string, value int) *Builder {
232+
e := bson.E{Key: types.Position, Value: value}
233+
if !b.parent.tryMergeValue(key, e) {
234+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
235+
}
191236
return b.parent
192237
}
193238

194-
func (b *arrayUpdateBuilder) Slice(num int) *Builder {
195-
b.parent.data = append(b.parent.data, bson.E{Key: types.SliceForUpdate, Value: num})
239+
func (b *arrayUpdateBuilder) Slice(key string, num int) *Builder {
240+
e := bson.E{Key: types.SliceForUpdate, Value: num}
241+
if !b.parent.tryMergeValue(key, e) {
242+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
243+
}
196244
return b.parent
197245
}
198246

199-
func (b *arrayUpdateBuilder) Sort(value any) *Builder {
200-
b.parent.data = append(b.parent.data, bson.E{Key: types.Sort, Value: value})
247+
func (b *arrayUpdateBuilder) Sort(key string, value any) *Builder {
248+
e := bson.E{Key: types.Sort, Value: value}
249+
if !b.parent.tryMergeValue(key, e) {
250+
b.parent.data = append(b.parent.data, bson.E{Key: key, Value: bson.D{e}})
251+
}
201252
return b.parent
202253
}

0 commit comments

Comments
 (0)