Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed instant handling in the nested object #239

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ class DefaultDynamoDBService<TItemClass> implements DynamoDBService<TItemClass>

}
)
.addFirstType(Instant,
new ArgumentMarshaller() {

@Override
AttributeValue marshall(Object obj) {
return obj == null ? null : new AttributeValue().withS(serializeDate((Instant) obj))
}

},
new ArgumentUnmarshaller() {

@Override
void typeCheck(AttributeValue value, Method setter) { }

@Override
Object unmarshall(AttributeValue value) throws ParseException {
return value.s == null ? null : Instant.from(Instant.parse(value.s))
}

}
)
.build()

// Specific ranges ending with 'Index' are String concatenated indexes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2024 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.aws.dynamodb.convert;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;

import java.time.Instant;

public class InstantConverter implements DynamoDBTypeConverter<String, Instant> {

public String convert(Instant instant) {
return instant.toString();
}

public Instant unconvert(String date) {
return Instant.parse(date);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import com.amazonaws.services.dynamodbv2.datamodeling.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@DynamoDBTable(tableName = "entity")
Expand All @@ -46,6 +48,8 @@ public class DynamoDBEntity {
@DynamoDBTypeConvertedEnum
State state = State.UNKNOWN;

private List<LogEntry> logs = new ArrayList<>();

public String getParentId() {
return parentId;
}
Expand Down Expand Up @@ -103,6 +107,14 @@ public void setState(State state) {
this.state = state;
}

public List<LogEntry> getLogs() {
return logs;
}

public void setLogs(List<LogEntry> logs) {
this.logs = logs;
}

//CHECKSTYLE:OFF
@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -161,6 +162,7 @@ private DynamoDBEntity createEntity(String parentId, String id, String rangeInde
entity.setId(id);
entity.setRangeIndex(rangeIndex);
entity.setDate(date);
entity.setLogs(List.of(LogEntry.create("test")));
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2024 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.aws.dynamodb;

import com.agorapulse.micronaut.aws.dynamodb.convert.InstantConverter;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;

import java.time.Instant;

@DynamoDBDocument
public class LogEntry {

public static LogEntry create(String message) {
LogEntry entry = new LogEntry();
entry.timestamp = Instant.now();
entry.message = message;
return entry;
}

@DynamoDBTypeConverted(converter = InstantConverter.class)
private Instant timestamp;
private String message;

public Instant getTimestamp() {
return timestamp;
}

public void setTimestamp(Instant timestamp) {
this.timestamp = timestamp;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}
Loading