Skip to content

Commit

Permalink
throw appropriate exception when the partition key is null
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Jun 27, 2024
1 parent 7457d8a commit ba2b3fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ private T postLoad(T i) {

private <R> R doWithKey(Object partitionKey, Object sortKey, Function<Key, R> function) {
String hashKeyName = table.tableSchema().tableMetadata().primaryPartitionKey();

if (partitionKey == null) {
throw new IllegalArgumentException("Partition key " + hashKeyName + " cannot be null");
}

AttributeValue partitionKeyValue = attributeConversionHelper.convert(table, hashKeyName, partitionKey);

if (sortKey == null) {
Expand All @@ -364,6 +369,11 @@ private <R> R doWithKey(Object partitionKey, Object sortKey, Function<Key, R> fu

private <R> Publisher<R> doWithKeys(Object partitionKey, Publisher<?> sortKeys, BiFunction<AttributeValue, Publisher<AttributeValue>, Publisher<R>> function) {
String hashKeyName = table.tableSchema().tableMetadata().primaryPartitionKey();

if (partitionKey == null) {
throw new IllegalArgumentException("Partition key " + hashKeyName + " cannot be null");
}

AttributeValue partitionKeyValue = attributeConversionHelper.convert(table, hashKeyName, partitionKey);

Optional<String> sortKeyName = table.tableSchema().tableMetadata().primarySortKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ private T postLoad(T i) {

private <R> R doWithKey(Object partitionKey, Object sortKey, Function<Key, R> function) {
String hashKeyName = table.tableSchema().tableMetadata().primaryPartitionKey();

if (partitionKey == null) {
throw new IllegalArgumentException("Partition key " + hashKeyName + " cannot be null");
}

AttributeValue partitionKeyValue = attributeConversionHelper.convert(table, hashKeyName, partitionKey);

if (sortKey == null) {
Expand All @@ -363,6 +368,11 @@ private <R> R doWithKey(Object partitionKey, Object sortKey, Function<Key, R> fu

private <R> Publisher<R> doWithKeys(Object partitionKey, Publisher<?> sortKeys, BiFunction<AttributeValue, Publisher<AttributeValue>, Publisher<R>> function) {
String hashKeyName = table.tableSchema().tableMetadata().primaryPartitionKey();

if (partitionKey == null) {
throw new IllegalArgumentException("Partition key " + hashKeyName + " cannot be null");
}

AttributeValue partitionKeyValue = attributeConversionHelper.convert(table, hashKeyName, partitionKey);

Optional<String> sortKeyName = table.tableSchema().tableMetadata().primarySortKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void testJavaService() {
assertNotNull(s.save(createEntity("1", "6", "foo", null, Date.from(REFERENCE_DATE.plus(3, ChronoUnit.DAYS)))));
assertNotNull(s.save(createEntity("2", "1", "bar", 3, Date.from(REFERENCE_DATE))));

assertThrowsExactly(IllegalArgumentException.class, () -> s.get(null, "1"));

assertEquals(2, s.countAllByNumber("1", 1));
assertEquals(1, s.countAllByNumber("1", null));
assertEquals(6, s.countAllByOptionalNumber("1", null));
Expand Down

0 comments on commit ba2b3fe

Please sign in to comment.