Skip to content

Commit 7220c5a

Browse files
committed
update
1 parent d2cb713 commit 7220c5a

File tree

6 files changed

+57
-22
lines changed

6 files changed

+57
-22
lines changed

aws-dynamodb/src/main/java/com/formkiq/aws/dynamodb/AttributeValueToMap.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,39 @@ public Map<String, Object> apply(final Map<String, AttributeValue> map) {
4545
for (Map.Entry<String, AttributeValue> e : map.entrySet()) {
4646

4747
String key = getKey(e.getKey());
48-
String s = e.getValue().s();
49-
String n = e.getValue().n();
5048

51-
if (isEmpty(s) && !isEmpty(n)) {
52-
result.put(key, Double.valueOf(n));
53-
} else {
54-
result.put(key, s);
55-
}
49+
Object obj = convert(e.getValue());
50+
result.put(key, obj);
5651
}
5752

5853
return result;
5954
}
6055

56+
private Object convert(final AttributeValue val) {
57+
Object obj = null;
58+
59+
switch (val.type()) {
60+
case S -> {
61+
obj = val.s();
62+
}
63+
case N -> {
64+
obj = val.n();
65+
}
66+
case BOOL -> {
67+
obj = val.b();
68+
}
69+
case L -> {
70+
obj = val.l().stream().map(this::convert).toList();
71+
}
72+
default -> {
73+
throw new IllegalArgumentException(
74+
"Unsupported attribute value map conversion " + val.type());
75+
}
76+
}
77+
78+
return obj;
79+
}
80+
6181
private String getKey(final String key) {
6282

6383
String k = key;

aws-dynamodb/src/main/java/com/formkiq/aws/dynamodb/MapToAttributeValue.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
2727

28+
import java.util.Collection;
2829
import java.util.HashMap;
2930
import java.util.Map;
3031
import java.util.function.Function;
@@ -42,17 +43,25 @@ public Map<String, AttributeValue> apply(final Map<String, Object> map) {
4243
Map<String, AttributeValue> result = new HashMap<>();
4344

4445
for (Map.Entry<String, Object> e : map.entrySet()) {
45-
46-
if (e.getValue() instanceof Double d) {
47-
result.put(e.getKey(), AttributeValue.fromN(String.valueOf(d)));
48-
} else if (e.getValue() instanceof String s) {
49-
result.put(e.getKey(), AttributeValue.fromS(s));
50-
} else {
51-
throw new IllegalArgumentException(
52-
"Unsupported data type: " + e.getValue().getClass().getName());
53-
}
46+
AttributeValue a = convert(e.getValue());
47+
result.put(e.getKey(), a);
5448
}
5549

5650
return result;
5751
}
52+
53+
private AttributeValue convert(final Object obj) {
54+
AttributeValue o = null;
55+
if (obj instanceof Double d) {
56+
o = AttributeValue.fromN(String.valueOf(d));
57+
} else if (obj instanceof String s) {
58+
o = AttributeValue.fromS(s);
59+
} else if (obj instanceof Collection<?> c) {
60+
o = AttributeValue.fromL(c.stream().map(this::convert).toList());
61+
} else {
62+
throw new IllegalArgumentException("Unsupported data type: " + obj.getClass().getName());
63+
}
64+
65+
return o;
66+
}
5867
}

aws-dynamodb/src/test/java/com/formkiq/aws/dynamodb/AttributeValueToMapTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.Test;
2727
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
2828

29+
import java.util.List;
2930
import java.util.Map;
3031

3132
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -36,20 +37,23 @@
3637
public class AttributeValueToMapTest {
3738

3839
/** {@link AttributeValueToMap}. */
39-
private AttributeValueToMap av = new AttributeValueToMap();
40+
private final AttributeValueToMap av = new AttributeValueToMap();
4041

4142
@Test
4243
void testApply01() {
4344
// given
4445
Map<String, AttributeValue> map = Map.of("contentType", AttributeValue.fromS("text/plain"),
45-
"contentLength", AttributeValue.fromN("38"));
46+
"contentLength", AttributeValue.fromN("38"), "ids",
47+
AttributeValue.fromL(List.of(AttributeValue.fromS("123"), AttributeValue.fromS("444"))));
4648

4749
// when
4850
Map<String, Object> result = av.apply(map);
4951

5052
// then
51-
assertEquals(2, result.size());
53+
final int expected = 3;
54+
assertEquals(expected, result.size());
5255
assertEquals("text/plain", result.get("contentType"));
53-
assertEquals(Double.valueOf("38"), result.get("contentLength"));
56+
assertEquals("38", result.get("contentLength").toString());
57+
assertEquals("[123, 444]", result.get("ids").toString());
5458
}
5559
}

lambda-api/src/integration/java/com/formkiq/stacks/api/awstest/CognitoRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public void testAddUser01() throws Exception {
400400
addUser(userApi, email, null);
401401
} catch (ApiException e) {
402402
// then
403-
assertTrue(e.getResponseBody().contains("Invalid email address format"));
403+
assertTrue(e.getResponseBody().contains("Username should be an email"));
404404
}
405405
}
406406

lambda-api/src/integration/java/com/formkiq/stacks/api/awstest/DocumentsDocumentIdUrlRequestTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static com.formkiq.testutils.aws.FkqDocumentService.addDocument;
2727
import static com.formkiq.testutils.aws.FkqDocumentService.waitForDocumentContent;
28+
import static com.formkiq.testutils.aws.FkqDocumentService.waitForDocumentContentType;
2829
import static org.junit.jupiter.api.Assertions.assertEquals;
2930
import java.nio.charset.StandardCharsets;
3031
import java.util.concurrent.TimeUnit;
@@ -62,7 +63,7 @@ public void testGet01() throws Exception {
6263

6364
// then
6465
GetDocumentContentResponse response =
65-
waitForDocumentContent(client, siteId, documentId, text);
66+
waitForDocumentContentType(client, siteId, documentId, "text/plain");
6667

6768
assertEquals("text/plain", response.getContentType());
6869
assertEquals(text, response.getContent());

lambda-api/src/integration/java/com/formkiq/stacks/api/awstest/DocumentsUploadRequestTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ public void testPost03() throws Exception {
404404
// then
405405
assertEquals(STATUS_OK, httpResponse.statusCode());
406406
waitForDocumentContent(client, siteId, documentId, content);
407+
waitForDocumentContentType(client, siteId, documentId, "text/html");
407408
assertEquals("text/html", api.getDocument(documentId, siteId, null).getContentType());
408409

409410
// given

0 commit comments

Comments
 (0)