Skip to content

Commit

Permalink
style[orm]: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Aug 3, 2024
1 parent d86c7e6 commit a455317
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
36 changes: 15 additions & 21 deletions orm/src/main/java/com/zfoo/orm/convention/AnnotationConvention.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.zfoo.orm.convention;

import com.zfoo.orm.anno.Id;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.util.ReflectionUtils;
import org.bson.codecs.pojo.ClassModelBuilder;
import org.bson.codecs.pojo.Convention;
import org.bson.codecs.pojo.PropertyModelBuilder;
Expand All @@ -16,37 +18,29 @@
* @since 2024/7/30
*/
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);
Field idField = ReflectionUtils.getFieldByNameInPOJOClass(classModelBuilder.getType(), idPropertyName);
Id annotation = idField.getAnnotation(Id.class);
if (annotation == null) {
throw new RunException("The class:[{}] has an id property:[{}] but no @Id annotation", classModelBuilder.getType().getName(), idPropertyName);
}
return;
}
for (PropertyModelBuilder<?> propertyModelBuilder : classModelBuilder.getPropertyModelBuilders()) {
processPropertyAnnotations(classModelBuilder, propertyModelBuilder);
}
}

private void processPropertyAnnotations(final ClassModelBuilder<?> classModelBuilder,
final PropertyModelBuilder<?> propertyModelBuilder) {
for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) {
if (annotation.annotationType().equals(Id.class)) {
String fieldName = propertyModelBuilder.getName();
classModelBuilder.idPropertyName(fieldName);
break;
for (Annotation annotation : propertyModelBuilder.getReadAnnotations()) {
if (annotation.annotationType().equals(Id.class)) {
String fieldName = propertyModelBuilder.getName();
classModelBuilder.idPropertyName(fieldName);
break;
}
}
}
}
}

}
6 changes: 4 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 @@ -105,9 +105,11 @@ public void initBefore() {
allEntityCachesUsableMap.put(entityClass, false);
}

var pojoCodecProvider = PojoCodecProvider.builder().automatic(true)
var pojoCodecProvider = PojoCodecProvider.builder()
.automatic(true)
.conventions(List.of(Conventions.ANNOTATION_CONVENTION, AnnotationConvention.INSTANCE))
.register(new MapCodecProvider()).build();
.register(new MapCodecProvider())
.build();
var codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
CodecRegistries.fromProviders(pojoCodecProvider));

Expand Down

0 comments on commit a455317

Please sign in to comment.