-
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.
- Add `@Deprecated(forRemoval=true)` and "will be removed in YOJ 3.0.0" warnings to `FieldValueType` pitfalls (`BINARY`, `isSortable()` etc.) - Remove `@CustomValueType.columnValueType` because it can always be deduced from `@CustomValueType.columnClass` instead. - Disallow *recursive* custom value types (that is, types whose converter in turn produce custom value types). - Allow preconverted values to be of a subclass of the column class. This is valuable if columnClass is a enum class, because Java makes anonymous inner classes of enums. Remove fast-path for postconverted values because it is never used by any real code (preconvert fast-path is used by `FieldValue`). - Add "map enum by ordinal" custom value type converter as a demonstration. Custom value types might become a viable alternatives for `@Column(dbQualifier="...")`.
- Loading branch information
1 parent
64c5315
commit 37e7399
Showing
16 changed files
with
201 additions
and
100 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
databind/src/main/java/tech/ydb/yoj/databind/converter/EnumOrdinalConverter.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,52 @@ | ||
package tech.ydb.yoj.databind.converter; | ||
|
||
import com.google.common.base.Preconditions; | ||
import lombok.NonNull; | ||
import tech.ydb.yoj.databind.schema.Schema.JavaField; | ||
|
||
/** | ||
* A generic converter that can be applied to represent your enum values as their {@link Enum#ordinal() ordinal}s | ||
* instead of their {@link Enum#name() constant name}s or {@link Enum#toString() string representation}s. | ||
* You can use it in a {@link tech.ydb.yoj.databind.schema.Column @Column} annotation, like this: | ||
* <blockquote><pre> | ||
* @Column( | ||
* customValueType=@CustomValueType( | ||
* columnClass=Integer.class, | ||
* converter=EnumOrdinalConverter.class | ||
* ) | ||
* ) | ||
* </pre></blockquote> | ||
* or as a global default for some of your enum type, like this: | ||
* <blockquote><pre> | ||
* @CustomValueType( | ||
* columnClass=Integer.class, | ||
* converter=EnumOrdinalConverter.class | ||
* ) | ||
* public enum MyEnum { | ||
* FOO, | ||
* BAR, | ||
* } | ||
* </pre></blockquote> | ||
* | ||
* @param <E> Java type | ||
*/ | ||
public final class EnumOrdinalConverter<E extends Enum<E>> implements ValueConverter<E, Integer> { | ||
private EnumOrdinalConverter() { | ||
} | ||
|
||
@Override | ||
public @NonNull Integer toColumn(@NonNull JavaField field, @NonNull E value) { | ||
return value.ordinal(); | ||
} | ||
|
||
@Override | ||
public @NonNull E toJava(@NonNull JavaField field, @NonNull Integer ordinal) { | ||
@SuppressWarnings("unchecked") | ||
E[] constants = (E[]) field.getRawType().getEnumConstants(); | ||
Preconditions.checkState(constants != null, "Not an enum field: %s", field); | ||
Preconditions.checkArgument(ordinal >= 0, "Negative ordinal %s for field %s", ordinal, field); | ||
Preconditions.checkArgument(ordinal < constants.length, "Unknown enum ordinal %s for field %s", ordinal, field); | ||
|
||
return constants[ordinal]; | ||
} | ||
} |
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.