Skip to content

Commit

Permalink
Add enum to specify name strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Alekseev authored and Andrey Alekseev committed Jan 16, 2025
1 parent a6c326d commit 5fb040c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
13 changes: 13 additions & 0 deletions databind/src/main/java/tech/ydb/yoj/databind/schema/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,17 @@
*/
@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/24")
CustomValueType customValueType() default @CustomValueType(columnClass = Comparable.class, converter = NoConverter.class);

enum NamingStrategy {
@Deprecated LEGACY,
ABSOLUTE,
RELATIVE
}

/**
* LEGACY - Use parent name as prefix only if current(child) doesn't specify name explicityly
* ABSOLUTE - Use only current name, don't add prent name as prefix
* RELATIVE - Use current name and add parent name as prefix
*/
NamingStrategy namingStrategy() default NamingStrategy.LEGACY;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.yoj.databind.schema.naming;

import lombok.NonNull;
import tech.ydb.yoj.databind.schema.Column;
import tech.ydb.yoj.databind.schema.Schema;
import tech.ydb.yoj.databind.schema.Table;

Expand Down Expand Up @@ -30,26 +31,25 @@ protected String getNameFromClass(Class<?> entityClass) {

private String getColumnName(Schema.JavaField field) {
var annotation = field.getField().getColumn();
if (annotation != null && !annotation.name().isEmpty()) {
var parentName = getParentAnnotationName(field);
return (parentName == null || parentName.isEmpty())
? annotation.name()
: parentName + NAME_DELIMITER + annotation.name();
var parentName = (field.getParent() == null) ? null : field.getParent().getName();

if (annotation == null) {
var fieldName = field.getField().getName();
return parentName == null ? fieldName : parentName + NAME_DELIMITER + fieldName;
}

return getColumnNameFromField(field);
}
var name = (annotation.name().isEmpty()) ? field.getField().getName() : annotation.name();

private String getParentAnnotationName(Schema.JavaField field) {
if (field.getParent() == null || field.getParent().getField() == null || field.getParent().getField().getColumn() == null)
return null;
return field.getParent().getField().getColumn().name();
}

protected String getColumnNameFromField(Schema.JavaField field) {
return (field.getParent() == null)
? field.getField().getName()
: field.getParent().getName() + NAME_DELIMITER + field.getField().getName();
if (annotation.namingStrategy() == Column.NamingStrategy.ABSOLUTE) {
return name;
} else if (annotation.namingStrategy() == Column.NamingStrategy.RELATIVE) {
return parentName + NAME_DELIMITER + name;
} else { // annotation.namingStrategy() == Column.NamingStrategy.LEGACY
if (!annotation.name().isEmpty()) {
return annotation.name();
}
return parentName == null ? name : parentName + NAME_DELIMITER + name;
}
}

protected void propagateFieldNameToFlatSubfield(Schema.JavaField field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testMixedEntityTableName() {
@Test
public void testMixedEntityFieldNameTest() {
verifyFieldNames(MixedEntity.class,
"column_name", "subEntity_boolValue", "sfe_timestamp", "id_stringValue", "int.val", "prefix_boolValue", "prefix_sfe_timestamp");
"column_name", "subEntity_boolValue", "absoluteBoolValue", "sfe_timestamp", "id_stringValue", "int.val", "prefix_sfe_relative_timestamp", "sfe_absolute_timestamp", "relativeWithoutColumnAnnotation_sfe_relative_timestamp");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public class MixedEntity {

TestEntity.SubEntity subEntity;

SubEntityWithRelative relativeWithoutColumnAnnotation;

@Column(name = "prefix")
TestEntity.SubEntity subEntityWithPrefix;
SubEntityWithRelative relativeWithPrefix;

@Value
private static class Id {
Expand All @@ -22,4 +24,10 @@ private static class Id {
@Column(name = "int.val")
Integer intValue;
}

@Value
static class SubEntityWithRelative {
@Column(name = "sfe_relative", namingStrategy = Column.NamingStrategy.RELATIVE)
SingleFieldEntity singleFieldEntityRelative;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ private static class Id {
static class SubEntity {
boolean boolValue;

@Column(namingStrategy = Column.NamingStrategy.ABSOLUTE)
boolean absoluteBoolValue;

@Column(name = "sfe")
SingleFieldEntity singleFieldEntity;

@Column(name = "sfe_absolute", namingStrategy = Column.NamingStrategy.ABSOLUTE)
SingleFieldEntity singleFieldEntityAbsolute;
}
}

0 comments on commit 5fb040c

Please sign in to comment.