-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See the
@CustomValueType
annotation and the ValueConverter
interf…
…ace for more information.
- Loading branch information
1 parent
14f61c1
commit a7e80e3
Showing
19 changed files
with
274 additions
and
10 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
databind/src/main/java/tech/ydb/yoj/databind/CustomValueType.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,30 @@ | ||
package tech.ydb.yoj.databind; | ||
|
||
import tech.ydb.yoj.ExperimentalApi; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target(TYPE) | ||
@Retention(RUNTIME) | ||
@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/24") | ||
public @interface CustomValueType { | ||
/** | ||
* Simple value type that the {@link #converter()} represents a custom value type as. | ||
* Cannot be {@link FieldValueType#COMPOSITE} or {@link FieldValueType#UNKNOWN}. | ||
*/ | ||
FieldValueType columnValueType(); | ||
|
||
/** | ||
* Type of value that {@link #converter() converter's} {@link ValueConverter#toColumn(Object) toColumn()} method returns | ||
*/ | ||
Class<?> columnClass(); | ||
|
||
/** | ||
* Converter class. Must have a no-args public constructor. | ||
*/ | ||
Class<? extends ValueConverter<?, ?>> converter(); | ||
} |
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
2 changes: 1 addition & 1 deletion
2
.../yoj/databind/schema/StringValueType.java → ...ech/ydb/yoj/databind/StringValueType.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package tech.ydb.yoj.databind.schema; | ||
package tech.ydb.yoj.databind; | ||
|
||
import tech.ydb.yoj.ExperimentalApi; | ||
|
||
|
19 changes: 19 additions & 0 deletions
19
databind/src/main/java/tech/ydb/yoj/databind/ValueConverter.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,19 @@ | ||
package tech.ydb.yoj.databind; | ||
|
||
import lombok.NonNull; | ||
import tech.ydb.yoj.ExperimentalApi; | ||
|
||
/** | ||
* Custom value conversion logic. Must have a no-args public constructor. | ||
* | ||
* @param <V> Java value type | ||
* @param <C> Database column value type | ||
*/ | ||
@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/24") | ||
public interface ValueConverter<V, C> { | ||
@NonNull | ||
C toColumn(@NonNull V v); | ||
|
||
@NonNull | ||
V toJava(@NonNull C c); | ||
} |
11 changes: 11 additions & 0 deletions
11
databind/src/main/java/tech/ydb/yoj/databind/schema/CustomConverterException.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,11 @@ | ||
package tech.ydb.yoj.databind.schema; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
import tech.ydb.yoj.ExperimentalApi; | ||
|
||
@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/24") | ||
public final class CustomConverterException extends BindingException { | ||
public CustomConverterException(@Nullable Throwable cause, String message) { | ||
super(cause, __ -> message); | ||
} | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
...sitory-test/src/main/java/tech/ydb/yoj/repository/test/sample/model/NetworkAppliance.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,50 @@ | ||
package tech.ydb.yoj.repository.test.sample.model; | ||
|
||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Value; | ||
import tech.ydb.yoj.databind.CustomValueType; | ||
import tech.ydb.yoj.databind.ValueConverter; | ||
import tech.ydb.yoj.repository.db.RecordEntity; | ||
|
||
import java.net.Inet6Address; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
import static tech.ydb.yoj.databind.FieldValueType.BINARY; | ||
|
||
public record NetworkAppliance( | ||
@NonNull Id id, | ||
@NonNull Ipv6Address address | ||
) implements RecordEntity<NetworkAppliance> { | ||
public record Id(@NonNull String value) implements RecordEntity.Id<NetworkAppliance> { | ||
} | ||
|
||
@CustomValueType(columnValueType = BINARY, columnClass = byte[].class, converter = Ipv6Address.Converter.class) | ||
@Value | ||
@RequiredArgsConstructor | ||
public static class Ipv6Address { | ||
Inet6Address addr; | ||
|
||
public Ipv6Address(String ip6) throws UnknownHostException { | ||
this((Inet6Address) InetAddress.getByName(ip6)); | ||
} | ||
|
||
public static final class Converter implements ValueConverter<Ipv6Address, byte[]> { | ||
@Override | ||
public byte @NonNull [] toColumn(@NonNull Ipv6Address ipv6Address) { | ||
return ipv6Address.addr.getAddress(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Ipv6Address toJava(byte @NonNull [] bytes) { | ||
try { | ||
return new Ipv6Address((Inet6Address) InetAddress.getByAddress(bytes)); | ||
} catch (UnknownHostException neverHappens) { | ||
throw new InternalError(neverHappens); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.