Skip to content

Commit

Permalink
Refactor RSQL serach fields related classes (2) (#1835)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Sep 9, 2024
1 parent 072809b commit 368c18e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
@Slf4j
public abstract class AbstractRSQLVisitor<A extends Enum<A> & RsqlQueryField> {

private final Class<A> fieldNameProvider;
private final Class<A> rsqlQueryFieldType;

@Value
public class RsqlField {
protected class RsqlField {

A enumValue;
String[] subAttributes;
Expand All @@ -39,22 +39,22 @@ private RsqlField(final A enumValue, final String[] subAttributes) {
}
}

protected AbstractRSQLVisitor(final Class<A> fieldNameProvider) {
this.fieldNameProvider = fieldNameProvider;
protected AbstractRSQLVisitor(final Class<A> rsqlQueryFieldType) {
this.rsqlQueryFieldType = rsqlQueryFieldType;
}

protected RsqlField getRsqlField(final ComparisonNode node) {
final String[] graph = node.getSelector().split(RsqlQueryField.SUB_ATTRIBUTE_SPLIT_REGEX);
final String enumName = graph.length == 0 ? node.getSelector() : graph[0];
log.debug("get field identifier by name {} of enum type {}", enumName, fieldNameProvider);
log.debug("get field identifier by name {} of enum type {}", enumName, rsqlQueryFieldType);

try {
final A enumValue = Enum.valueOf(fieldNameProvider, enumName.toUpperCase());
final A enumValue = Enum.valueOf(rsqlQueryFieldType, enumName.toUpperCase());
final String[] subAttributes = enumValue.getSubAttributes(node.getSelector());

// validate
if (enumValue.isMap()) {
// enum.key
// <enum>.<key>
if (subAttributes.length != 2) {
throw new RSQLParameterUnsupportedFieldException(
"The syntax of the given map search parameter field {" + node.getSelector() + "} is wrong. Syntax is: <enum name>.<key name>");
Expand Down Expand Up @@ -92,8 +92,7 @@ protected RsqlField getRsqlField(final ComparisonNode node) {
* @return Exception with prepared message extracted from the comparison node.
*/
private RSQLParameterUnsupportedFieldException createRSQLParameterUnsupportedException(
@NotNull final ComparisonNode node,
final Exception rootException) {
@NotNull final ComparisonNode node, final Exception rootException) {
return new RSQLParameterUnsupportedFieldException(String.format(
"The given search parameter field {%s} does not exist, must be one of the following fields %s",
node.getSelector(), getExpectedFieldList()), rootException);
Expand All @@ -106,7 +105,7 @@ private String getFormattedSubEntityAttribute(final A propertyEnum, final String
}

private List<String> getExpectedFieldList() {
final List<String> expectedFieldList = Arrays.stream(fieldNameProvider.getEnumConstants())
final List<String> expectedFieldList = Arrays.stream(rsqlQueryFieldType.getEnumConstants())
.filter(enumField -> enumField.getSubEntityAttributes().isEmpty()).map(enumField -> {
final String enumFieldName = enumField.name().toLowerCase();
if (enumField.isMap()) {
Expand All @@ -116,7 +115,7 @@ private List<String> getExpectedFieldList() {
}
}).collect(Collectors.toList());

final List<String> expectedSubFieldList = Arrays.stream(fieldNameProvider.getEnumConstants())
final List<String> expectedSubFieldList = Arrays.stream(rsqlQueryFieldType.getEnumConstants())
.filter(enumField -> !enumField.getSubEntityAttributes().isEmpty()).flatMap(enumField -> {
final List<String> subEntity = enumField
.getSubEntityAttributes().stream().map(fieldName -> enumField.name().toLowerCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@
* {@link RSQLVisitor} implementation which validates the nodes (fields) based
* on a given {@link RsqlQueryField} for a given entity type.
*
* @param <A>
* The type the {@link RsqlQueryField} refers to.
* @param <A> The type the {@link RsqlQueryField} refers to.
*/
public class FieldValidationRsqlVisitor<A extends Enum<A> & RsqlQueryField> extends AbstractRSQLVisitor<A>
implements RSQLVisitor<Void, String> {

/**
* Constructs the visitor and initializes it.
*
* @param fieldNameProvider
* The {@link RsqlQueryField} to use for validation.
* @param fieldNameProvider The {@link RsqlQueryField} to use for validation.
*/
public FieldValidationRsqlVisitor(final Class<A> fieldNameProvider) {
super(fieldNameProvider);
Expand All @@ -58,5 +56,4 @@ private Void visitNode(final LogicalNode node, final String param) {
node.getChildren().forEach(child -> child.accept(this, param));
return null;
}

}
}

0 comments on commit 368c18e

Please sign in to comment.