-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: adds OffsetDateTime converters for Hashes (#195)
* OffsetDateTime converters for Hashes * Adding Test Cases - OffsetDateTime converters for Hashes --------- Co-authored-by: khushwinder <[email protected]>
- Loading branch information
1 parent
be2e10c
commit 196da88
Showing
6 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...s-om-spring/src/main/java/com/redis/om/spring/convert/BytesToOffsetDateTimeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.redis.om.spring.convert; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.ReadingConverter; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
|
||
@ReadingConverter | ||
public class BytesToOffsetDateTimeConverter implements Converter<byte[], OffsetDateTime> { | ||
|
||
@Override | ||
public OffsetDateTime convert(byte[] source) { | ||
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(toString(source))), ZoneId.systemDefault()); | ||
} | ||
String toString(byte[] source) { | ||
return new String(source, StandardCharsets.UTF_8); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...s-om-spring/src/main/java/com/redis/om/spring/convert/OffsetDateTimeToBytesConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.redis.om.spring.convert; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.WritingConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
|
||
@Component | ||
@WritingConverter | ||
public class OffsetDateTimeToBytesConverter implements Converter<OffsetDateTime, byte[]> { | ||
byte[] fromString(String source) { | ||
return source.getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public byte[] convert(OffsetDateTime source) { | ||
Instant instant = source.atZoneSameInstant(ZoneId.systemDefault()).toInstant(); | ||
long timeInMillis = instant.toEpochMilli(); | ||
|
||
return fromString(Long.toString(timeInMillis)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...-om-spring/src/main/java/com/redis/om/spring/convert/OffsetDateTimeToStringConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.redis.om.spring.convert; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.WritingConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
|
||
@Component | ||
@WritingConverter | ||
public class OffsetDateTimeToStringConverter implements Converter<OffsetDateTime, String> { | ||
@Override | ||
public String convert(OffsetDateTime source) { | ||
Instant instant = source.atZoneSameInstant(ZoneId.systemDefault()).toInstant(); | ||
long unixTime = instant.getEpochSecond(); | ||
return Long.toString(unixTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters