bring dynamodb2 update expression handling closer to spec

This commit is contained in:
Peter Gorniak 2016-11-25 21:07:24 -08:00
parent c3c6eaf281
commit 5a7c711a74

View File

@ -3,6 +3,7 @@ from collections import defaultdict
import datetime import datetime
import decimal import decimal
import json import json
import re
from moto.compat import OrderedDict from moto.compat import OrderedDict
from moto.core import BaseBackend from moto.core import BaseBackend
@ -110,27 +111,24 @@ class Item(object):
} }
def update(self, update_expression, expression_attribute_names, expression_attribute_values): def update(self, update_expression, expression_attribute_names, expression_attribute_values):
ACTION_VALUES = ['SET', 'set', 'REMOVE', 'remove'] parts = [p for p in re.split(r'\b(SET|REMOVE|ADD|DELETE)\b', update_expression) if p]
for action, valstr in zip(parts[:-1:1], parts[1::1]):
action = None values = valstr.split(',')
for value in update_expression.split(): for value in values:
if value in ACTION_VALUES:
# An action
action = value
continue
else:
# A Real value # A Real value
value = value.lstrip(":").rstrip(",") value = value.lstrip(":").rstrip(",")
for k, v in expression_attribute_names.items(): for k, v in expression_attribute_names.items():
value = value.replace(k, v) value = value.replace(k, v)
if action == "REMOVE" or action == 'remove': if action == "REMOVE" or action == 'remove':
self.attrs.pop(value, None) self.attrs.pop(value, None)
elif action == 'SET' or action == 'set': elif action == 'SET' or action == 'set':
key, value = value.split("=") key, value = value.split("=")
if value in expression_attribute_values: key = key.strip()
self.attrs[key] = DynamoType(expression_attribute_values[value]) value = value.strip()
else: if value in expression_attribute_values:
self.attrs[key] = DynamoType({"S": value}) self.attrs[key] = DynamoType(expression_attribute_values[value])
else:
self.attrs[key] = DynamoType({"S": value})
def update_with_attribute_updates(self, attribute_updates): def update_with_attribute_updates(self, attribute_updates):
for attribute_name, update_action in attribute_updates.items(): for attribute_name, update_action in attribute_updates.items():