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

Add support for Instant to Long conversion #789

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -69,6 +69,8 @@ private DateConverters() {
converters.add(LongToJava8LocalDateConverter.INSTANCE);
converters.add(DurationToStringConverter.INSTANCE);
converters.add(StringToDurationConverter.INSTANCE);
converters.add(InstantToLongConverter.INSTANCE);
converters.add(LongToInstantConverter.INSTANCE);

if (JODA_TIME_IS_PRESENT) {
converters.add(LocalDateToLongConverter.INSTANCE);
Expand Down Expand Up @@ -295,4 +297,24 @@ public Duration convert(String source) {
return Duration.parse(source);
}
}

@WritingConverter
public enum InstantToLongConverter implements Converter<Instant, Long> {
INSTANCE;

@Override
public Long convert(Instant source) {
return source == null ? null : source.toEpochMilli();
}
}

@ReadingConverter
public enum LongToInstantConverter implements Converter<Long, Instant> {
INSTANCE;

@Override
public Instant convert(Long source) {
return source == null ? null : Instant.ofEpochMilli(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.within;
import static org.springframework.data.aerospike.sample.SampleClasses.SimpleClass.SIMPLESET;
import static org.springframework.data.aerospike.sample.SampleClasses.SimpleClassWithPersistenceConstructor.SIMPLESET2;
import static org.springframework.data.aerospike.sample.SampleClasses.User.SIMPLESET3;
Expand Down Expand Up @@ -672,6 +675,23 @@ void objectWithCalendarField(int converterOption) {
new Bin("calendar", DateConverters.CalendarToMapConverter.INSTANCE.convert(calendar)));
}

@ParameterizedTest
@ValueSource(ints = {0, 1})
void objectWithInstantField(int converterOption) {
Instant instant = Instant.ofEpochMilli(Instant.now().toEpochMilli()); //to truncate nanos
Copy link

@agrgr agrgr Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space between "//" and the comment text.
Why not using something like Instant instant = Instant.now().truncatedTo(ChronoUnit.MILLIS);?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't thought about that such method exists :) Thanks, updated

DocumentWithInstant object = new DocumentWithInstant(id, instant);

BiConsumer<DocumentWithInstant, DocumentWithInstant> objectAssertFunction = (expected, actual) -> {
assertThat(expected.getId()).isEqualTo(actual.getId());
assertThat(expected.getInstant()).isCloseTo(actual.getInstant(), within(1, ChronoUnit.MILLIS));
};

assertWriteAndRead(converterOption, object,
"DocumentWithInstant", id, objectAssertFunction,
new Bin("@_class", DocumentWithInstant.class.getName()),
new Bin("instant", DateConverters.InstantToLongConverter.INSTANCE.convert(instant)));
}

@ParameterizedTest()
@ValueSource(ints = {0, 1})
void objectWithDurationField(int converterOption) {
Expand Down Expand Up @@ -784,6 +804,17 @@ private <T> void assertWriteAndRead(int converterOption,
String expectedSet,
Object expectedUserKey,
Bin... expectedBins) {
BiConsumer<T,T> equalsAssertFunction = (a, b) -> assertThat(a).isEqualTo(b);
assertWriteAndRead(converterOption, object, expectedSet, expectedUserKey, equalsAssertFunction,
expectedBins);
}

private <T> void assertWriteAndRead(int converterOption,
T object,
String expectedSet,
Object expectedUserKey,
BiConsumer<T,T> objectAssertFunction,
Bin... expectedBins) {
MappingAerospikeConverter aerospikeConverter = getAerospikeMappingConverterByOption(converterOption);
AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);

Expand All @@ -808,7 +839,7 @@ private <T> void assertWriteAndRead(int converterOption,

@SuppressWarnings("unchecked") T actual = (T) aerospikeConverter.read(object.getClass(), forRead);

assertThat(actual).isEqualTo(object);
objectAssertFunction.accept(actual, object);
}

private boolean compareMaps(AerospikeDataSettings settings, Bin expected, Bin actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.math.BigInteger;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -988,6 +989,16 @@ public static class DocumentWithCalendar {
private Calendar calendar;
}

@Data
@AllArgsConstructor
@Document
public static class DocumentWithInstant {
@Id
private String id;
@Field
private Instant instant;
}

@Data
@AllArgsConstructor
@Document
Expand Down
Loading