Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Metamodel Fields Generation for Non-Indexed Fields (resolves gh-327) #330

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.redis.om.spring.annotations;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
public @interface Metamodel {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -317,7 +318,7 @@ else if (Number.class.isAssignableFrom(targetCls)) {
// Any Date/Time Types
//
else if ((targetCls == LocalDateTime.class) || (targetCls == LocalDate.class) //
|| (targetCls == Date.class) || (targetCls == Instant.class)) {
|| (targetCls == Date.class) || (targetCls == Instant.class) || (targetCls == OffsetDateTime.class)) {
targetInterceptor = DateField.class;
}
//
Expand Down Expand Up @@ -362,8 +363,10 @@ else if (targetCls == Boolean.class) {
searchSchemaAlias = indexed.alias();
}
} else {
var metamodel = field.getAnnotation(Metamodel.class);
try {
targetCls = ClassUtils.forName(cls, MetamodelGenerator.class.getClassLoader());

//
// Any Character class
//
Expand All @@ -381,7 +384,7 @@ else if (targetCls == Boolean.class) {
//
else if (Number.class.isAssignableFrom(targetCls) || (targetCls == LocalDateTime.class)
|| (targetCls == LocalDate.class) || (targetCls == Date.class)
|| (targetCls == Instant.class)) {
|| (targetCls == Instant.class) || (targetCls == OffsetDateTime.class)) {
targetInterceptor = NonIndexedNumericField.class;
}
//
Expand All @@ -397,8 +400,12 @@ else if (targetCls == Point.class) {
targetInterceptor = NonIndexedGeoField.class;
}
} catch (ClassNotFoundException cnfe) {
messager.printMessage(Diagnostic.Kind.WARNING,
"Processing class " + entityName + " could not resolve " + cls);
if (metamodel != null) {
messager.printMessage(Kind.NOTE,
"Processing class " + entityName + ", generating nested class " + cls
+ " metamodel (@Metamodel)");
fieldMetamodelSpec.addAll(processNestedIndexableFields(entity, chain));
}
}
}

Expand Down Expand Up @@ -539,8 +546,9 @@ private List<Triple<ObjectGraphFieldSpec, FieldSpec, CodeBlock>> processNestedIn
enclosedFields.forEach((field, getter) -> {
boolean fieldIsIndexed = (field.getAnnotation(Indexed.class) != null)
|| (field.getAnnotation(Searchable.class) != null);
boolean generateMetamodel = field.getAnnotation(Metamodel.class) != null;

if (fieldIsIndexed) {
if (fieldIsIndexed || generateMetamodel) {
List<Element> newChain = new ArrayList<>(chain);
newChain.add(field);
fieldMetamodels.addAll(processFieldMetamodel(entity, entityFieldName, newChain));
Expand Down Expand Up @@ -705,7 +713,7 @@ private Triple<ObjectGraphFieldSpec, FieldSpec, CodeBlock> generateFieldMetamode
.builder(interceptor, fieldAccessor).addModifiers(Modifier.PUBLIC, Modifier.STATIC) //
.build();

String alias = "";
String alias;
if (!isEmpty(searchSchemaAlias)) {
alias = searchSchemaAlias;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,7 @@ private List<T> toResultTuple(List<E> entities, String[] returnFields) {
entities.forEach(entity -> {
List<Object> mappedResults = new ArrayList<>();
returning.forEach(foi -> {
String getterName = "get" + ObjectUtils.ucfirst(foi.getSearchAlias());
Method getter = ReflectionUtils.findMethod(entitySearchStream.getEntityClass(), getterName);
// No "getXXX", maybe there is a "isXXX"
if (getter == null) {
getterName = "is" + ObjectUtils.ucfirst(foi.getSearchAlias());
getter = ReflectionUtils.findMethod(entitySearchStream.getEntityClass(), getterName);
}
//TODO: if getter is still null then field access?
mappedResults.add(ReflectionUtils.invokeMethod(getter, entity));
mappedResults.add(ObjectUtils.getValueByPath(entity, foi.getJSONPath()));
});

if (returning.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static String ucfirst(String input) {
public static String withFirst(String input, Function<Character, String> callback) {
if (input == null) {
return null;
} else if (input.length() == 0) {
} else if (input.isEmpty()) {
return "";
} else {
return String.join("", callback.apply(input.charAt(0)), input.subSequence(1, input.length()));
Expand Down Expand Up @@ -226,7 +226,7 @@ public static String toUnderscoreSeparated(final String javaName) {
final String input = unQuote(javaName.trim());
for (int i = 0; i < input.length(); i++) {
final char c = input.charAt(i);
if (result.length() == 0) {
if (result.isEmpty()) {
result.append(Character.toLowerCase(c));
} else if (Character.isUpperCase(c)) {
result.append("_").append(Character.toLowerCase(c));
Expand Down Expand Up @@ -540,7 +540,6 @@ public static Object getValueByPath(Object target, String path) {

if (!hasNestedObject) {
safeSpelPath = safeSpelPath //
.replace(".", "?.") //
.replace("[*]", "") //
.replace(".", "?.");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Metamodel;
import lombok.*;
import org.springframework.data.annotation.Id;

@Data
@RequiredArgsConstructor(staticName = "of")
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Document
public class DeepNestNonIndexed {
@Id
private String id;

@NonNull
@Indexed
private String name;

@NonNull
@Metamodel
private NestLevelNonIndexed1 nestLevel1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.repository.RedisDocumentRepository;

import java.util.Optional;

@SuppressWarnings("unused") public interface DeepNestNonIndexedRepository extends RedisDocumentRepository<DeepNestNonIndexed, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Metamodel;
import com.redis.om.spring.annotations.Searchable;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(staticName = "of")
public class NestLevelNonIndexed1 {
@NonNull
@Metamodel
private String name;

@NonNull
@Metamodel
private String block;

@NonNull
@Metamodel
private NestLevelNonIndexed2 nestLevel2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Metamodel;
import com.redis.om.spring.annotations.Searchable;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(staticName = "of")
public class NestLevelNonIndexed2 {
@NonNull
@Metamodel
private String name;

@NonNull
@Metamodel
private String block;
}
Loading
Loading