Skip to content

Commit

Permalink
Merge pull request #129 from sandogeek/main
Browse files Browse the repository at this point in the history
ref[orm]: Optimize naming and logic
  • Loading branch information
jaysunxiao authored Jul 31, 2024
2 parents 02ec775 + bd36142 commit d86c7e6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package com.zfoo.orm.convention;

import com.zfoo.orm.anno.Id;
import org.bson.BsonType;
import org.bson.codecs.pojo.ClassModelBuilder;
import org.bson.codecs.pojo.Convention;
import org.bson.codecs.pojo.PropertyModelBuilder;
import org.bson.codecs.pojo.annotations.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
* zfoo注解约定
* 注解约定
*
* @author Sando
* @version 1.0
* @since 2024/7/30
*/
public class ZfooAnnotationConvention implements Convention {
public static final ZfooAnnotationConvention INSTANCE = new ZfooAnnotationConvention();
public class AnnotationConvention implements Convention {
public static final AnnotationConvention INSTANCE = new AnnotationConvention();

@Override
public void apply(ClassModelBuilder<?> classModelBuilder) {
String idPropertyName = classModelBuilder.getIdPropertyName();
if (idPropertyName != null) {
try {
Field idField = classModelBuilder.getType().getDeclaredField(idPropertyName);
Id annotation = idField.getAnnotation(Id.class);
if (annotation == null) {
throw new RuntimeException("The class " + classModelBuilder.getType().getName() +
" has an id property[name=" + idPropertyName + "] but no @Id annotation");
}
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
return;
}
for (PropertyModelBuilder<?> propertyModelBuilder : classModelBuilder.getPropertyModelBuilders()) {
processPropertyAnnotations(classModelBuilder, propertyModelBuilder);
}
Expand All @@ -28,17 +42,10 @@ public void apply(ClassModelBuilder<?> classModelBuilder) {
private void processPropertyAnnotations(final ClassModelBuilder<?> classModelBuilder,
final PropertyModelBuilder<?> propertyModelBuilder) {
for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) {
if (annotation instanceof Id) {
String idPropertyName = classModelBuilder.getIdPropertyName();
if (annotation.annotationType().equals(Id.class)) {
String fieldName = propertyModelBuilder.getName();
if (idPropertyName != null && !fieldName.equals(idPropertyName)) {
// allow using @Id and @BsonId on same field
String typeName = classModelBuilder.getType().getName();
throw new IllegalStateException("The class " +
typeName + " has more than one id property. The properties are " +
idPropertyName + " and " + fieldName);
}
classModelBuilder.idPropertyName(fieldName);
break;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions orm/src/main/java/com/zfoo/orm/manager/OrmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.zfoo.orm.config.CacheStrategy;
import com.zfoo.orm.config.OrmConfig;
import com.zfoo.orm.config.PersisterStrategy;
import com.zfoo.orm.convention.ZfooAnnotationConvention;
import com.zfoo.orm.convention.AnnotationConvention;
import com.zfoo.orm.model.EntityDef;
import com.zfoo.orm.model.IEntity;
import com.zfoo.orm.model.IndexDef;
Expand Down Expand Up @@ -106,7 +106,7 @@ public void initBefore() {
}

var pojoCodecProvider = PojoCodecProvider.builder().automatic(true)
.conventions(List.of(Conventions.ANNOTATION_CONVENTION, ZfooAnnotationConvention.INSTANCE))
.conventions(List.of(Conventions.ANNOTATION_CONVENTION, AnnotationConvention.INSTANCE))
.register(new MapCodecProvider()).build();
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
CodecRegistries.fromProviders(pojoCodecProvider));
Expand Down
15 changes: 15 additions & 0 deletions orm/src/test/java/com/zfoo/orm/accessor/IdAnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import com.zfoo.orm.OrmContext;
import com.zfoo.orm.entity.MailEntity;
import com.zfoo.orm.entity.WrongEntity;
import com.zfoo.protocol.util.StringUtils;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Expand All @@ -38,4 +40,17 @@ public void insertMail(String mailId) {
var mailEntity = MailEntity.valueOf(mailId, "userName-" + mailId, "content" + mailId, new Date());
OrmContext.getAccessor().insert(mailEntity);
}

@Test
public void wrongCase() {
new ClassPathXmlApplicationContext("application.xml");
var entity = new WrongEntity();
Exception exception = null;
try {
OrmContext.getAccessor().insert(entity);
} catch (Exception e) {
exception = e;
}
Assert.assertNotNull(exception);
}
}
43 changes: 43 additions & 0 deletions orm/src/test/java/com/zfoo/orm/entity/WrongEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.zfoo.orm.entity;

import com.zfoo.orm.anno.EntityCache;
import com.zfoo.orm.anno.Id;
import com.zfoo.orm.anno.Index;
import com.zfoo.orm.model.IEntity;
import org.bson.codecs.pojo.annotations.BsonId;

/**
* 用法有问题的entity
*
* @author Sando
* @version 1.0
* @since 2024/7/30
*/
@EntityCache
public class WrongEntity implements IEntity<String> {
@BsonId
private String userName;
@Id
private String mailId;

@Override
public String id() {
return mailId;
}

public String getMailId() {
return mailId;
}

public void setMailId(String mailId) {
this.mailId = mailId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}

0 comments on commit d86c7e6

Please sign in to comment.