Skip to content

Commit 6bb8739

Browse files
bootjpCopilot
andauthored
Update adapter/dynamodb.go
Co-authored-by: Copilot <[email protected]>
1 parent 0db8f3a commit 6bb8739

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

adapter/dynamodb.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,51 @@ func (d *DynamoDBServer) updateItem(w http.ResponseWriter, r *http.Request) {
192192
Key: key,
193193
Value: []byte(valAttr.S),
194194
},
195-
},
195+
196+
// Parse UpdateExpression for SET clause(s)
197+
setPrefix := "SET "
198+
setIdx := strings.Index(strings.ToUpper(updExpr), setPrefix)
199+
if setIdx == -1 {
200+
http.Error(w, "only SET clause is supported in update expression", http.StatusBadRequest)
201+
return
202+
}
203+
setExpr := updExpr[setIdx+len(setPrefix):]
204+
// Remove any trailing clauses (REMOVE, ADD, DELETE)
205+
for _, clause := range []string{" REMOVE ", " ADD ", " DELETE "} {
206+
if idx := strings.Index(strings.ToUpper(setExpr), clause); idx != -1 {
207+
setExpr = setExpr[:idx]
208+
}
209+
}
210+
assignments := strings.Split(setExpr, ",")
211+
elems := make([]*kv.Elem[kv.OP], 0, len(assignments))
212+
for _, assign := range assignments {
213+
parts := strings.SplitN(assign, "=", 2)
214+
if len(parts) != 2 {
215+
http.Error(w, "invalid assignment in update expression", http.StatusBadRequest)
216+
return
217+
}
218+
field := strings.TrimSpace(parts[0])
219+
valPlaceholder := strings.TrimSpace(parts[1])
220+
valAttr, ok := in.ExpressionAttributeValues[valPlaceholder]
221+
if !ok {
222+
http.Error(w, "missing value attribute: "+valPlaceholder, http.StatusBadRequest)
223+
return
224+
}
225+
// For this example, we only support updating the "value" field
226+
if field != "value" {
227+
http.Error(w, "only 'value' field can be updated", http.StatusBadRequest)
228+
return
229+
}
230+
elems = append(elems, &kv.Elem[kv.OP]{
231+
Op: kv.Put,
232+
Key: key,
233+
Value: []byte(valAttr.S),
234+
})
235+
}
236+
237+
req := &kv.OperationGroup[kv.OP]{
238+
IsTxn: false,
239+
Elems: elems,
196240
}
197241
if _, err = d.coordinator.Dispatch(req); err != nil {
198242
http.Error(w, err.Error(), http.StatusInternalServerError)

0 commit comments

Comments
 (0)