mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
Fix(dynamoDB): handle null for Java AWS SDK v2 DynamoDB EnhancedClient correctly (#309)
* 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" * fix(dynamoDB): support Java AWS SDK v2 enhanced client null values * fix(dynamoDB): added compatibility integration test for DynamoDB EnhancedClient. Including suggestion for default host+port for test fixture, so `mvn test -q` can work even without setting the env variable set.
This commit is contained in:
1 parent
e83f6d6cbd
commit
8bac81807e
6 files changed
+387
-24
No files matched your search
@@ -904,6 +904,8 @@ public class DynamoDbService {
|
||||
JsonNode exprAttrNames, JsonNode exprAttrValues) {
|
||||
// Parse comma-separated assignments: "attr = :val, #name = :val2"
|
||||
// Stop when we hit another clause keyword (REMOVE, ADD, DELETE) or end
|
||||
LOG.debugv("applySetClause: clause={0}, exprAttrNames={1}, exprAttrValues={2}",
|
||||
clause, exprAttrNames, exprAttrValues);
|
||||
while (!clause.isEmpty()) {
|
||||
String upper = clause.toUpperCase();
|
||||
if (upper.startsWith("REMOVE ") || upper.startsWith("ADD ") || upper.startsWith("DELETE ")) {
|
||||
@@ -920,23 +922,25 @@ public class DynamoDbService {
|
||||
String rest = clause.substring(eqIdx + 1).trim();
|
||||
|
||||
// Find the value placeholder or expression
|
||||
// IMPORTANT: Check for clause keywords FIRST, then commas
|
||||
// This ensures we don't include REMOVE/ADD/DELETE clauses in value parts
|
||||
String valuePart;
|
||||
int nextClause = findNextClauseKeyword(rest);
|
||||
int commaIdx = findNextComma(rest);
|
||||
if (commaIdx >= 0) {
|
||||
|
||||
// If there's a clause keyword, use the earlier of comma or keyword
|
||||
if (nextClause >= 0 && (commaIdx < 0 || nextClause < commaIdx)) {
|
||||
valuePart = rest.substring(0, nextClause).trim();
|
||||
rest = rest.substring(nextClause).trim();
|
||||
} else if (commaIdx >= 0) {
|
||||
valuePart = rest.substring(0, commaIdx).trim();
|
||||
rest = rest.substring(commaIdx + 1).trim();
|
||||
} else {
|
||||
// Check for next clause keyword
|
||||
int nextClause = findNextClauseKeyword(rest);
|
||||
if (nextClause >= 0) {
|
||||
valuePart = rest.substring(0, nextClause).trim();
|
||||
rest = rest.substring(nextClause).trim();
|
||||
} else {
|
||||
valuePart = rest.trim();
|
||||
rest = "";
|
||||
}
|
||||
valuePart = rest.trim();
|
||||
rest = "";
|
||||
}
|
||||
|
||||
|
||||
// Resolve the value
|
||||
if (valuePart.startsWith("if_not_exists(")) {
|
||||
// if_not_exists(attrRef, fallbackExpr) evaluates to:
|
||||
@@ -948,17 +952,17 @@ public class DynamoDbService {
|
||||
String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames);
|
||||
String fallbackExpr = args[1].trim();
|
||||
JsonNode resolved;
|
||||
if (item.has(checkAttr)) {
|
||||
if (hasValueAtPath(item, checkAttr, exprAttrNames)) {
|
||||
// attrRef exists — evaluate to its current value
|
||||
resolved = item.get(checkAttr);
|
||||
resolved = getValueAtPath(item, checkAttr, exprAttrNames);
|
||||
} else if (fallbackExpr.startsWith(":") && exprAttrValues != null) {
|
||||
resolved = exprAttrValues.get(fallbackExpr);
|
||||
} else {
|
||||
// fallback is itself an attribute reference
|
||||
resolved = item.get(resolveAttributeName(fallbackExpr, exprAttrNames));
|
||||
resolved = getValueAtPath(item, resolveAttributeName(fallbackExpr, exprAttrNames), exprAttrNames);
|
||||
}
|
||||
if (resolved != null) {
|
||||
item.set(attrName, resolved);
|
||||
setValueAtPath(item, attrPath, resolved, exprAttrNames);
|
||||
}
|
||||
}
|
||||
} else if (valuePart.startsWith("list_append(")) {
|
||||
@@ -985,15 +989,20 @@ public class DynamoDbService {
|
||||
}
|
||||
} else if (valuePart.startsWith(":") && exprAttrValues != null) {
|
||||
JsonNode value = exprAttrValues.get(valuePart);
|
||||
LOG.debugv("applySetClause: looked up valuePart={0} in exprAttrValues, got value={1}",
|
||||
valuePart, value);
|
||||
if (value != null) {
|
||||
item.set(attrName, value);
|
||||
setValueAtPath(item, attrPath, value, exprAttrNames);
|
||||
LOG.debugv("applySetClause: set attrPath={0} to value={1}", attrPath, value);
|
||||
} else {
|
||||
LOG.debugv("applySetClause: value was null for valuePart={0}, NOT setting attribute", valuePart);
|
||||
}
|
||||
} else if (!valuePart.isEmpty()) {
|
||||
// Plain attribute reference: SET a = b or SET a = #alias
|
||||
String refAttr = resolveAttributeName(valuePart, exprAttrNames);
|
||||
JsonNode refValue = item.get(refAttr);
|
||||
JsonNode refValue = getValueAtPath(item, refAttr, exprAttrNames);
|
||||
if (refValue != null) {
|
||||
item.set(attrName, refValue);
|
||||
setValueAtPath(item, attrPath, refValue, exprAttrNames);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1149,6 +1158,95 @@ public class DynamoDbService {
|
||||
return nameOrPlaceholder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value at a potentially nested attribute path.
|
||||
* Supports paths like "attr", "parent.child", "#alias.nested" etc.
|
||||
* For nested paths, navigates into the DynamoDB Map structure (M field).
|
||||
*/
|
||||
private void setValueAtPath(ObjectNode item, String path, JsonNode value, JsonNode exprAttrNames) {
|
||||
// Resolve any # placeholders in the path segments
|
||||
String[] segments = path.split("\\.");
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
segments[i] = resolveAttributeName(segments[i].trim(), exprAttrNames);
|
||||
}
|
||||
|
||||
if (segments.length == 1) {
|
||||
// Simple top-level attribute
|
||||
item.set(segments[0], value);
|
||||
return;
|
||||
}
|
||||
|
||||
// Navigate to the parent of the target attribute
|
||||
ObjectNode current = item;
|
||||
for (int i = 0; i < segments.length - 1; i++) {
|
||||
String segment = segments[i];
|
||||
JsonNode child = current.get(segment);
|
||||
|
||||
if (child == null || !child.has("M")) {
|
||||
// Create nested map structure if it doesn't exist
|
||||
ObjectNode newMap = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
|
||||
ObjectNode wrapper = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
|
||||
wrapper.set("M", newMap);
|
||||
current.set(segment, wrapper);
|
||||
current = newMap;
|
||||
} else {
|
||||
// Navigate into existing map
|
||||
JsonNode mapContent = child.get("M");
|
||||
if (mapContent instanceof ObjectNode) {
|
||||
current = (ObjectNode) mapContent;
|
||||
} else {
|
||||
// Cannot navigate further, structure mismatch
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the final attribute
|
||||
current.set(segments[segments.length - 1], value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value at a potentially nested attribute path.
|
||||
* Returns null if the path doesn't exist.
|
||||
*/
|
||||
private JsonNode getValueAtPath(JsonNode item, String path, JsonNode exprAttrNames) {
|
||||
// Resolve any # placeholders in the path segments
|
||||
String[] segments = path.split("\\.");
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
segments[i] = resolveAttributeName(segments[i].trim(), exprAttrNames);
|
||||
}
|
||||
|
||||
JsonNode current = item;
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
if (current == null) return null;
|
||||
|
||||
String segment = segments[i];
|
||||
JsonNode child = current.get(segment);
|
||||
|
||||
if (child == null) return null;
|
||||
|
||||
if (i < segments.length - 1) {
|
||||
// Need to navigate deeper - check for Map structure
|
||||
if (child.has("M")) {
|
||||
current = child.get("M");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
// This is the final segment
|
||||
return child;
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a value exists at a potentially nested attribute path.
|
||||
*/
|
||||
private boolean hasValueAtPath(JsonNode item, String path, JsonNode exprAttrNames) {
|
||||
return getValueAtPath(item, path, exprAttrNames) != null;
|
||||
}
|
||||
|
||||
private int findNextComma(String s) {
|
||||
// Find next comma that is not inside a function call
|
||||
int depth = 0;
|
||||
|
||||
Reference in new issue
Block a user