Skip to content

Commit

Permalink
fix: permit query asset json properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Oct 23, 2023
1 parent cba9558 commit 4c48e4f
Show file tree
Hide file tree
Showing 23 changed files with 493 additions and 658 deletions.
2 changes: 2 additions & 0 deletions core/common/util/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ plugins {
}

dependencies {
api(project(":spi:common:core-spi"))

testImplementation(libs.junit.pioneer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.eclipse.edc.util.reflection;

import org.eclipse.edc.spi.types.PathItem;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -47,29 +49,34 @@ public static <T> T getFieldValue(String propertyName, Object object) {
Objects.requireNonNull(propertyName, "propertyName");
Objects.requireNonNull(object, "object");

if (propertyName.contains(".")) {
var dotIx = propertyName.indexOf(".");
var field = propertyName.substring(0, dotIx);
var rest = propertyName.substring(dotIx + 1);
object = getFieldValue(field, object);
if (object == null) {
var path = PathItem.parse(propertyName);
return getFieldValue(path, object);
}

private static <T> T getFieldValue(List<PathItem> path, Object object) {
var first = path.get(0);

if (path.size() > 1) {
var nested = getFieldValue(List.of(first), object);
if (nested == null) {
return null;
}
return getFieldValue(rest, object);
} else if (propertyName.matches(ARRAY_INDEXER_REGEX)) { //array indexer
var openingBracketIx = propertyName.indexOf(OPENING_BRACKET);
var closingBracketIx = propertyName.indexOf(CLOSING_BRACKET);
var propName = propertyName.substring(0, openingBracketIx);
var arrayIndex = Integer.parseInt(propertyName.substring(openingBracketIx + 1, closingBracketIx));
var rest = path.stream().skip(1).toList();
return getFieldValue(rest, nested);
} else if (first.toString().matches(ARRAY_INDEXER_REGEX)) { //array indexer
var openingBracketIx = first.toString().indexOf(OPENING_BRACKET);
var closingBracketIx = first.toString().indexOf(CLOSING_BRACKET);
var propName = first.toString().substring(0, openingBracketIx);
var arrayIndex = Integer.parseInt(first.toString().substring(openingBracketIx + 1, closingBracketIx));
var iterableObject = (List) getFieldValue(propName, object);
return (T) iterableObject.get(arrayIndex);
} else {
if (object instanceof Map<?, ?> map) {
return (T) map.get(propertyName);
return (T) map.get(first.toString());
} else if (object instanceof List<?> list) {
return (T) list.stream().filter(Objects::nonNull).map(it -> getRecursiveValue(propertyName, it)).toList();
return (T) list.stream().filter(Objects::nonNull).map(it -> getRecursiveValue(first.toString(), it)).toList();
} else {
return getRecursiveValue(propertyName, object);
return getRecursiveValue(first.toString(), object);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.util.reflection;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class PathItemTest {

@Test
void shouldParse_singleElement() {
var result = PathItem.parse("test");

assertThat(result).hasSize(1).first().extracting(PathItem::toString).isEqualTo("test");
}

@Test
void shouldParse_singleWrappedElement() {
var result = PathItem.parse("'test.data'");

assertThat(result).hasSize(1).first().extracting(PathItem::toString).isEqualTo("test.data");
}

@Test
void shouldParse_multipleSeparatedByDots() {
var result = PathItem.parse("test.data.path");

assertThat(result).hasSize(3).extracting(PathItem::toString)
.containsExactly("test", "data", "path");
}

@Test
void shouldParse_multipleWrappedSeparatedByDots() {
var result = PathItem.parse("'test.test'.data.'path.path'");

assertThat(result).hasSize(3).extracting(PathItem::toString)
.containsExactly("test.test", "data", "path.path");
}
}
Loading

0 comments on commit 4c48e4f

Please sign in to comment.