Skip to content

Commit

Permalink
0.9.32
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Dec 14, 2024
1 parent fb42954 commit aa6ad77
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 22 deletions.
2 changes: 1 addition & 1 deletion project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.babyfish.jimmer
version=0.9.31
version=0.9.32
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,22 @@ public ImmutableProp(

if (executableElement.getAnnotation(Default.class) != null &&
executableElement.getAnnotation(LogicalDeleted.class) != null) {
throw new MetaException(
executableElement,
"property cannot be decorated by both \"@Default\" and \"@LogicalDeleted\""
);
boolean isValid = executableElement.getReturnType().getKind() == TypeKind.INT;
if (!isValid) {
if (executableElement.getReturnType().getKind() == TypeKind.DECLARED) {
DeclaredType declaredType = (DeclaredType) executableElement.getReturnType();
TypeElement typeElement = (TypeElement) declaredType.asElement();
TypeElement superTypeElement = (TypeElement) ((DeclaredType)typeElement.getSuperclass()).asElement();
isValid = superTypeElement.getQualifiedName().toString().equals("java.lang.Enum");
}
}
if (!isValid) {
throw new MetaException(
executableElement,
"property cannot be decorated by both \"@Default\" and \"@LogicalDeleted\" " +
"unless its type is int or enum"
);
}
}

Formula formula = executableElement.getAnnotation(Formula.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public static LogicalDeletedInfo of(ImmutableProp prop) {
}
} else {
Default dft = prop.getAnnotation(Default.class);
if (dft != null && dft.value().isEmpty()) {
if (dft != null && !dft.value().isEmpty()) {
initializedText = dft.value();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1369,14 +1369,6 @@ public Ref<Object> getDefaultValueRef() {
if (ref == null) {
Version version = getAnnotation(Version.class);
Default dft = getAnnotation(Default.class);
if (dft != null && isLogicalDeleted()) {
throw new ModelException(
"Illegal property \"" +
this +
"\", it is logical deleted flag " +
"so it cannot be decorated \"@Default\""
);
}
if (version != null) {
Object value;
if (dft == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,42 @@ class ImmutableProp(
"the property whose type is kotlin value class is not supported now"
)
}
if (propDeclaration.annotation(Default::class) !== null &&
propDeclaration.annotation(LogicalDeleted::class) !== null) {
throw MetaException(
propDeclaration,
"the property cannot be decorated by both \"@Default\" and \"@LogicalDeleted\""
)
if (propDeclaration.annotation(LogicalDeleted::class) !== null) {
val declaration = realDeclaration
val typeName = if (declaration is KSClassDeclaration && declaration.modifiers.contains(Modifier.ENUM)) {
"<enum>"
} else {
declaration.fullName
}
when (typeName) {
"kotlin.Boolean", "kotlin.Int", "<enum>" ->
if (resolvedType.isMarkedNullable) {
throw MetaException(
propDeclaration,
"the property decorated by \"@LogicalDeleted\" cannot be nullable " +
"if its type is boolean, int, or enum"
)
}
"kotlin.Long", "java.util.UUID", "java.time.LocalDateTime" -> {}
else -> throw MetaException(
propDeclaration,
"the property decorated by \"@LogicalDeleted\" must be " +
"boolean, int, enum, UUID or LocalDateTime"
)
}
if (propDeclaration.annotation(Default::class) !== null) {
val isValid = when (typeName) {
"kotlin.Int", "<enum>" -> true
else -> false
}
if (!isValid) {
throw MetaException(
propDeclaration,
"the property cannot be decorated by both \"@Default\" and \"@LogicalDeleted\" " +
"unless its type is int or enum"
)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

import org.babyfish.jimmer.sql.DatabaseValidationIgnore
import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.LogicalDeleted

@Entity
@DatabaseValidationIgnore
interface A {

@Id
val id: Long

@LogicalDeleted("true")
val deleted: Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

import org.babyfish.jimmer.sql.DatabaseValidationIgnore
import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.LogicalDeleted

@Entity
@DatabaseValidationIgnore
interface B {

@Id
val id: Long

@LogicalDeleted("false")
val active: Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

import org.babyfish.jimmer.sql.DatabaseValidationIgnore
import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.LogicalDeleted

@Entity
@DatabaseValidationIgnore
interface C {

@Id
val id: Long

@LogicalDeleted("2")
val state: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

import org.babyfish.jimmer.sql.*

@Entity
@DatabaseValidationIgnore
interface D {

@Id
val id: Long

@Default("1")
@LogicalDeleted("2")
val state: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

import org.babyfish.jimmer.sql.*

@Entity
@DatabaseValidationIgnore
interface E {

@Id
val id: Long

@Default("NEW")
@LogicalDeleted("DELETED")
val state: State
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

enum class State {
NEW,
PROCESSING,
DELETED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.babyfish.jimmer.sql.kt.model.ld.validation

import org.babyfish.jimmer.meta.ImmutableType
import kotlin.test.Test
import kotlin.test.expect

class ValidationTest {

@Test
fun testA() {
val type = ImmutableType.get(A::class.java)
val prop = type.getProp("deleted")
val info = type.logicalDeletedInfo ?: error("Impossible")
expect(false) {
prop.defaultValueRef.value
}
expect(false) {
info.allocateInitializedValue()
}
expect(true) {
info.generateValue()
}
}

@Test
fun testB() {
val type = ImmutableType.get(B::class.java)
val prop = type.getProp("active")
val info = type.logicalDeletedInfo ?: error("Impossible")
expect(true) {
prop.defaultValueRef.value
}
expect(true) {
info.allocateInitializedValue()
}
expect(false) {
info.generateValue()
}
}

@Test
fun testC() {
val type = ImmutableType.get(C::class.java)
val prop = type.getProp("state")
val info = type.logicalDeletedInfo ?: error("Impossible")
expect(0) {
prop.defaultValueRef.value
}
expect(0) {
info.allocateInitializedValue()
}
expect(2) {
info.generateValue()
}
}

@Test
fun testD() {
val type = ImmutableType.get(D::class.java)
val prop = type.getProp("state")
val info = type.logicalDeletedInfo ?: error("Impossible")
expect(1) {
prop.defaultValueRef.value
}
expect(1) {
info.allocateInitializedValue()
}
expect(2) {
info.generateValue()
}
}

@Test
fun testE() {
val type = ImmutableType.get(E::class.java)
val prop = type.getProp("state")
val info = type.logicalDeletedInfo ?: error("Impossible")
expect(State.NEW) {
prop.defaultValueRef.value
}
expect(State.NEW) {
info.allocateInitializedValue()
}
expect(State.DELETED) {
info.generateValue()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class BatchSqlBuilder extends AbstractSqlBuilder<BatchSqlBuilder> {

private final List<TemplateVariable> templateVariables = new ArrayList<>();

final JSqlClientImplementor sqlClient;

private final JSqlClientImplementor sqlClient;

public BatchSqlBuilder(JSqlClientImplementor sqlClient) {
this.sqlClient = sqlClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.babyfish.jimmer.sql.model.ld.validate;

import org.babyfish.jimmer.sql.DatabaseValidationIgnore;
import org.babyfish.jimmer.sql.Entity;
import org.babyfish.jimmer.sql.Id;
import org.babyfish.jimmer.sql.LogicalDeleted;

@DatabaseValidationIgnore
@Entity
public interface A {

@Id
long id();

@LogicalDeleted("true")
boolean deleted();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.babyfish.jimmer.sql.model.ld.validate;

import org.babyfish.jimmer.sql.DatabaseValidationIgnore;
import org.babyfish.jimmer.sql.Entity;
import org.babyfish.jimmer.sql.Id;
import org.babyfish.jimmer.sql.LogicalDeleted;

@DatabaseValidationIgnore
@Entity
public interface B {

@Id
long id();

@LogicalDeleted("false")
boolean active();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.babyfish.jimmer.sql.model.ld.validate;

import org.babyfish.jimmer.sql.DatabaseValidationIgnore;
import org.babyfish.jimmer.sql.Entity;
import org.babyfish.jimmer.sql.Id;
import org.babyfish.jimmer.sql.LogicalDeleted;

@DatabaseValidationIgnore
@Entity
public interface C {

@Id
long id();

@LogicalDeleted("2")
int state();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.babyfish.jimmer.sql.model.ld.validate;

import org.babyfish.jimmer.sql.*;

@DatabaseValidationIgnore
@Entity
public interface D {

@Id
long id();

@Default("1")
@LogicalDeleted("2")
int state();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.babyfish.jimmer.sql.model.ld.validate;

import org.babyfish.jimmer.sql.*;

@DatabaseValidationIgnore
@Entity
public interface E {

@Id
long id();

@Default("NEW")
@LogicalDeleted("DELETED")
State state();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.babyfish.jimmer.sql.model.ld.validate;

public enum State {

NEW,
PROCESSING,
DELETED
}
Loading

0 comments on commit aa6ad77

Please sign in to comment.