mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
fix: dynamodb support add function multiple values (#263)
* Fixes DynamoDB support for adding values to a list for the SET ... ADD query/command * Revert "Fixes DynamoDB support for adding values to a list for the SET ... ADD query/command" * Fixes DynamoDB support for adding values to a list for the SET ... ADD query/command
This commit is contained in:
1 parent
2dca523907
commit
1ddc8a32f7
3 files changed
+127
-5
No files matched your search
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>io.github.hectorvent</groupId>
|
||||
<artifactId>floci</artifactId>
|
||||
<version>1.0.11</version>
|
||||
<version>1.3.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Floci</name>
|
||||
|
||||
@@ -1010,7 +1010,7 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
private String applyAddClause(ObjectNode item, String clause,
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
while (!clause.isEmpty()) {
|
||||
String upper = clause.toUpperCase();
|
||||
if (upper.startsWith("SET ") || upper.startsWith("REMOVE ") || upper.startsWith("DELETE ")) {
|
||||
@@ -1025,9 +1025,11 @@ public class DynamoDbService {
|
||||
String valuePlaceholder = parts[1].replaceAll(",.*", "").trim();
|
||||
|
||||
if (valuePlaceholder.startsWith(":") && exprAttrValues != null) {
|
||||
JsonNode value = exprAttrValues.get(valuePlaceholder);
|
||||
if (value != null) {
|
||||
item.set(attrName, value);
|
||||
JsonNode addValue = exprAttrValues.get(valuePlaceholder);
|
||||
if (addValue != null) {
|
||||
JsonNode existingValue = item.get(attrName);
|
||||
JsonNode newValue = applyAddOperation(existingValue, addValue);
|
||||
item.set(attrName, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1043,6 +1045,77 @@ public class DynamoDbService {
|
||||
return clause;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements DynamoDB ADD operation semantics:
|
||||
* - For numbers (N): adds the value to the existing number, or sets it if attribute doesn't exist
|
||||
* - For sets (SS, NS, BS): adds elements to the existing set, or creates the set if it doesn't exist
|
||||
*/
|
||||
private JsonNode applyAddOperation(JsonNode existingValue, JsonNode addValue) {
|
||||
ObjectNode result = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
|
||||
|
||||
// Handle number addition
|
||||
if (addValue.has("N")) {
|
||||
String addNumStr = addValue.get("N").asText();
|
||||
if (existingValue == null || !existingValue.has("N")) {
|
||||
// Attribute doesn't exist — set to the add value
|
||||
return addValue;
|
||||
}
|
||||
// Add the numbers
|
||||
String existingNumStr = existingValue.get("N").asText();
|
||||
try {
|
||||
java.math.BigDecimal existingNum = new java.math.BigDecimal(existingNumStr);
|
||||
java.math.BigDecimal addNum = new java.math.BigDecimal(addNumStr);
|
||||
result.put("N", existingNum.add(addNum).toPlainString());
|
||||
return result;
|
||||
} catch (NumberFormatException e) {
|
||||
// Fall back to just setting the value
|
||||
return addValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle string set (SS) addition
|
||||
if (addValue.has("SS")) {
|
||||
if (existingValue == null || !existingValue.has("SS")) {
|
||||
return addValue;
|
||||
}
|
||||
java.util.Set<String> combined = new java.util.LinkedHashSet<>();
|
||||
existingValue.get("SS").forEach(n -> combined.add(n.asText()));
|
||||
addValue.get("SS").forEach(n -> combined.add(n.asText()));
|
||||
var arrayNode = result.putArray("SS");
|
||||
combined.forEach(arrayNode::add);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Handle number set (NS) addition
|
||||
if (addValue.has("NS")) {
|
||||
if (existingValue == null || !existingValue.has("NS")) {
|
||||
return addValue;
|
||||
}
|
||||
java.util.Set<String> combined = new java.util.LinkedHashSet<>();
|
||||
existingValue.get("NS").forEach(n -> combined.add(n.asText()));
|
||||
addValue.get("NS").forEach(n -> combined.add(n.asText()));
|
||||
var arrayNode = result.putArray("NS");
|
||||
combined.forEach(arrayNode::add);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Handle binary set (BS) addition
|
||||
if (addValue.has("BS")) {
|
||||
if (existingValue == null || !existingValue.has("BS")) {
|
||||
return addValue;
|
||||
}
|
||||
java.util.Set<String> combined = new java.util.LinkedHashSet<>();
|
||||
existingValue.get("BS").forEach(n -> combined.add(n.asText()));
|
||||
addValue.get("BS").forEach(n -> combined.add(n.asText()));
|
||||
var arrayNode = result.putArray("BS");
|
||||
combined.forEach(arrayNode::add);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Unsupported type for ADD — just set the value
|
||||
return addValue;
|
||||
}
|
||||
|
||||
String resolveAttributeName(String nameOrPlaceholder, JsonNode exprAttrNames) {
|
||||
nameOrPlaceholder = trimBalancedOuterParens(nameOrPlaceholder).trim();
|
||||
if (nameOrPlaceholder.startsWith("#") && exprAttrNames != null) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -764,4 +765,52 @@ class DynamoDbServiceTest {
|
||||
DynamoDbService.ScanResult result = service.scan("Users", "contains(values, :v)", null, exprValues, null, null, null);
|
||||
assertEquals(1, result.items().size(), "contains() on List with N elements should use type-aware numeric comparison");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateItemSetAddsToStringSet() {
|
||||
createOrdersTable();
|
||||
|
||||
ObjectNode key = item("customerId", "1", "orderId", "sort1");
|
||||
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
ObjectNode priceVal = mapper.createObjectNode();
|
||||
priceVal.put("N", "100");
|
||||
|
||||
// Use SS (String Set) type for ADD operation
|
||||
ObjectNode tagVal = mapper.createObjectNode();
|
||||
var tagArray = tagVal.putArray("SS");
|
||||
tagArray.add("a");
|
||||
exprValues.set(":val", priceVal);
|
||||
exprValues.set(":newTag", tagVal);
|
||||
|
||||
service.updateItem("Orders", key, null,
|
||||
"SET price = if_not_exists(price, :val) ADD tags :newTag",
|
||||
null, exprValues, null);
|
||||
|
||||
// And add another tag to the same item, to verify that the ADD works on existing items as well
|
||||
ObjectNode tagVal2 = mapper.createObjectNode();
|
||||
var tagArray2 = tagVal2.putArray("SS");
|
||||
tagArray2.add("b");
|
||||
exprValues.set(":newTag", tagVal2);
|
||||
DynamoDbService.UpdateResult updateResult = service.updateItem("Orders", key, null,
|
||||
"SET price = if_not_exists(price, :val) ADD tags :newTag",
|
||||
null, exprValues, null);
|
||||
|
||||
JsonNode stored = service.getItem("Orders", key);
|
||||
assertNotNull(stored, "item should have been created");
|
||||
assertTrue(stored.has("tags"), "tags attribute must be present on item after ADD");
|
||||
|
||||
// Verify tags is a String Set (SS) with both values
|
||||
JsonNode tagsNode = stored.get("tags");
|
||||
assertTrue(tagsNode.has("SS"), "tags should be of type SS (String Set)");
|
||||
JsonNode ssArray = tagsNode.get("SS");
|
||||
assertEquals(2, ssArray.size(), "tags should have 2 elements");
|
||||
|
||||
// Verify values from the SS array
|
||||
java.util.Set<String> tagValues = new java.util.HashSet<>();
|
||||
ssArray.forEach(node -> tagValues.add(node.asText()));
|
||||
assertEquals(2, tagValues.size());
|
||||
assertTrue(tagValues.containsAll(Arrays.asList("a", "b")));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user