Skip to content

Commit

Permalink
#20: Add warning about implicitly using legacy YOJ type mapping for YDB
Browse files Browse the repository at this point in the history
Issue: #20
  • Loading branch information
nvamelichev committed Feb 29, 2024
1 parent 8d1d38d commit 41a231b
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.NonNull;
import lombok.Value;
import lombok.With;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.ydb.proto.ValueProtos;
import tech.ydb.proto.ValueProtos.Type.PrimitiveTypeId;
import tech.ydb.proto.ValueProtos.Value.ValueCase;
Expand Down Expand Up @@ -50,6 +52,10 @@
@Value
@AllArgsConstructor(access = PRIVATE)
public class YqlPrimitiveType implements YqlType {
private static final Logger log = LoggerFactory.getLogger(YqlPrimitiveType.class);

private static volatile boolean typeMappingExplicitlySet = false;

// Only table column data types. See https://ydb.tech/en/docs/yql/reference/types/
private static final Map<PrimitiveTypeId, String> YQL_TYPE_NAMES = Map.ofEntries(
Map.entry(PrimitiveTypeId.BOOL, "Bool"),
Expand Down Expand Up @@ -354,14 +360,14 @@ public static void resetStringDefaultTypeToDefaults() {
* If you need to support legacy applications, call {@code useLegacyMappingFor(STRING, ENUM, TIMESTAMP)} before using
* any YOJ features.
*
* @param fieldValueTypes field value types to use legacy mapping for
* @deprecated We <strong>STRONGLY</strong> advise against using the legacy mapping in new projects.
* Please call {@link #useRecommendedMappingFor(FieldValueType...) useNewMappingFor(STRING, ENUM, TIMESTAMP)} instead,
* and annotate custom-mapped columns with {@link Column &#64;Column} where a different mapping is desired.
*
* @param fieldValueTypes field value types to use legacy mapping for
*/
@Deprecated
public static void useLegacyMappingFor(FieldValueType... fieldValueTypes) {
typeMappingExplicitlySet = true;
for (var fvt : fieldValueTypes) {
switch (fvt) {
case STRING, ENUM -> VALUE_DEFAULT_YQL_TYPES.put(fvt, new ValueYqlTypeSelector(fvt, PrimitiveTypeId.STRING, null));
Expand All @@ -386,6 +392,7 @@ public static void useLegacyMappingFor(FieldValueType... fieldValueTypes) {
*/
@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/20")
public static void useRecommendedMappingFor(FieldValueType... fieldValueTypes) {
typeMappingExplicitlySet = true;
for (var fvt : fieldValueTypes) {
switch (fvt) {
case STRING, ENUM -> VALUE_DEFAULT_YQL_TYPES.put(fvt, new ValueYqlTypeSelector(fvt, PrimitiveTypeId.UTF8, null));
Expand Down Expand Up @@ -457,6 +464,11 @@ public static YqlPrimitiveType of(JavaField column) {
@NonNull
private static YqlPrimitiveType resolveYqlType(Type javaType, FieldValueType valueType,
PrimitiveTypeId yqlType, String qualifier) {
if (!typeMappingExplicitlySet) {
typeMappingExplicitlySet = true;
log.error("YOJ's Java<->YDB type mapping IS NOT specified explicitly! See https://github.com/ydb-platform/yoj-project/issues/20#issuecomment-1971661677");
}

YqlTypeSelector typeSelector;

switch (valueType) {
Expand Down

0 comments on commit 41a231b

Please sign in to comment.