Skip to content

Commit

Permalink
Improved PathResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale-Ko committed Aug 22, 2024
1 parent 494d6d5 commit a4448b9
Showing 1 changed file with 62 additions and 98 deletions.
160 changes: 62 additions & 98 deletions src/main/java/io/github/kale_ko/ejcl/PathResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import io.github.kale_ko.bjsl.elements.ParsedObject;
import io.github.kale_ko.bjsl.elements.ParsedPrimitive;
import java.util.*;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Contains methods useful for getting/setting nested values
*
* @version 1.0.0
* @version 4.0.0
* @since 1.0.0
*/
public class PathResolver {
protected static final Pattern arrayPathFixPattern = Pattern.compile("\\[([0-9]*)]");
protected static final Pattern pathSplitPattern = Pattern.compile("(?<!\\\\)(?:\\\\\\\\)*\\.");

private PathResolver() {
}

Expand All @@ -29,31 +33,9 @@ private PathResolver() {
* @since 1.0.0
*/
public static @Nullable Object resolve(@NotNull ParsedElement element, @NotNull String path) {
return resolve(element, path, true);
}
path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]");

/**
* Resolve a value on an element
*
* @param element The element to resolve on
* @param path The path to resolve to
* @param returnObjArrValues Weather or not to return the keys of objects and arrays when queried
*
* @return The value resolved
*
* @since 1.0.0
*/
public static @Nullable Object resolve(@NotNull ParsedElement element, @NotNull String path, boolean returnObjArrValues) {
path = path.replaceAll("\\[([0-9]*)]", ".[$1]");

List<String> keys = new ArrayList<>();
int i2 = 0;
for (int i = 1; i <= path.length(); i++) {
if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) {
keys.add(path.substring(i2, i).replace("\\.", "."));
i2 = i + 1;
}
}
List<String> keys = Arrays.asList(pathSplitPattern.split(path));

ParsedElement resolved = element;

Expand All @@ -67,7 +49,7 @@ private PathResolver() {
}
} else if (resolved.isArray()) {
if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) {
int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1"));
int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]"));

if (index >= 0 && index < resolved.asArray().getSize()) {
resolved = resolved.asArray().get(index);
Expand All @@ -85,17 +67,11 @@ private PathResolver() {
}
}

if (resolved == null) {
return null;
} else if (resolved.isObject() && returnObjArrValues) {
return "{obj}," + String.join(",", resolved.asObject().getKeys());
} else if (resolved.isArray() && returnObjArrValues) {
return "{arr}," + resolved.asArray().getSize();
} else if (resolved.isPrimitive()) {
if (resolved != null && resolved.isPrimitive()) {
return resolved.asPrimitive().get();
} else {
return null;
}

return null;
}

/**
Expand All @@ -109,16 +85,9 @@ private PathResolver() {
* @since 1.0.0
*/
public static @Nullable ParsedElement resolveElement(@NotNull ParsedElement element, @NotNull String path) {
path = path.replaceAll("\\[([0-9]*)]", ".[$1]");

List<String> keys = new ArrayList<>();
int i2 = 0;
for (int i = 1; i <= path.length(); i++) {
if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) {
keys.add(path.substring(i2, i).replace("\\.", "."));
i2 = i + 1;
}
}
path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]");

List<String> keys = Arrays.asList(pathSplitPattern.split(path));

ParsedElement resolved = element;

Expand All @@ -132,7 +101,7 @@ private PathResolver() {
}
} else if (resolved.isArray()) {
if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) {
int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1"));
int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]"));

if (index >= 0 && index < resolved.asArray().getSize()) {
resolved = resolved.asArray().get(index);
Expand Down Expand Up @@ -165,7 +134,7 @@ private PathResolver() {
* @since 1.0.0
*/
public static @NotNull ParsedElement update(@NotNull ParsedElement element, @NotNull String path, @Nullable Object value) {
return PathResolver.update(element, path, value, true);
return update(element, path, value, true);
}

/**
Expand All @@ -181,16 +150,9 @@ private PathResolver() {
* @since 1.0.0
*/
public static @NotNull ParsedElement update(@NotNull ParsedElement element, @NotNull String path, @Nullable Object value, boolean force) {
path = path.replaceAll("\\[([0-9]*)]", ".[$1]");

List<String> keys = new ArrayList<>();
int i2 = 0;
for (int i = 1; i <= path.length(); i++) {
if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) {
keys.add(path.substring(i2, i).replace("\\.", "."));
i2 = i + 1;
}
}
path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]");

List<String> keys = Arrays.asList(pathSplitPattern.split(path));

ParsedElement resolved = element;

Expand All @@ -200,10 +162,12 @@ private PathResolver() {
resolved = resolved.asObject().get(keys.get(i));
} else {
if (force) {
if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) {
if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) {
resolved.asObject().set(keys.get(i), ParsedArray.create());
} else if (keys.size() > i + 1) {
resolved.asObject().set(keys.get(i), ParsedObject.create());
} else {
resolved.asObject().set(keys.get(i), ParsedArray.create());
resolved.asObject().set(keys.get(i), ParsedPrimitive.fromNull());
}

resolved = resolved.asObject().get(keys.get(i));
Expand All @@ -214,17 +178,19 @@ private PathResolver() {
}
} else if (resolved.isArray()) {
if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) {
int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1"));
int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]"));

if (index >= 0 && index < resolved.asArray().getSize()) {
resolved = resolved.asArray().get(index);
} else {
if (force) {
while (resolved.asArray().getSize() <= index) {
if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) {
if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) {
resolved.asArray().add(ParsedArray.create());
} else if (keys.size() > i + 1) {
resolved.asArray().add(ParsedObject.create());
} else {
resolved.asArray().add(ParsedArray.create());
resolved.asArray().add(ParsedPrimitive.fromNull());
}
}

Expand Down Expand Up @@ -253,31 +219,18 @@ private PathResolver() {
resolved.asObject().set(valueKey, ParsedPrimitive.from(value));
}
} else {
if (value instanceof String && ((String) value).startsWith("{obj},")) {
resolved.asObject().set(valueKey, ParsedObject.create());
} else if (value instanceof String && ((String) value).startsWith("{arr},")) {
resolved.asObject().set(valueKey, ParsedArray.create());
} else {
resolved.asObject().set(valueKey, ParsedPrimitive.from(value));
}
resolved.asObject().set(valueKey, ParsedPrimitive.from(value));
}
} else if (resolved.isArray() && (valueKey.startsWith("[") && valueKey.endsWith("]"))) {
int resolvedValueKey = Integer.parseInt(valueKey.replaceAll("\\[([0-9]*)]", "$1"));
int resolvedValueKey = Integer.parseInt(arrayPathFixPattern.matcher(valueKey).replaceAll(".[$1]"));

if (resolvedValueKey >= 0 && resolvedValueKey < resolved.asArray().getSize()) {
if (resolved.asArray().get(resolvedValueKey).isPrimitive()) {
resolved.asArray().set(resolvedValueKey, ParsedPrimitive.from(value));
}
} else {
if (value instanceof String && ((String) value).startsWith("{obj},")) {
resolved.asArray().add(ParsedObject.create());
} else if (value instanceof String && ((String) value).startsWith("{arr},")) {
resolved.asArray().add(ParsedArray.create());
} else {
resolved.asArray().add(ParsedPrimitive.from(value));
}
resolved.asArray().add(ParsedPrimitive.from(value));
}

}
}

Expand All @@ -296,7 +249,7 @@ private PathResolver() {
* @since 1.0.0
*/
public static @NotNull ParsedElement updateElement(@NotNull ParsedElement element, @NotNull String path, @NotNull ParsedElement value) {
return PathResolver.updateElement(element, path, value, true);
return updateElement(element, path, value, true);
}

/**
Expand All @@ -312,16 +265,11 @@ private PathResolver() {
* @since 1.0.0
*/
public static @NotNull ParsedElement updateElement(@NotNull ParsedElement element, @NotNull String path, @NotNull ParsedElement value, boolean force) {
path = path.replaceAll("\\[([0-9]*)]", ".[$1]");

List<String> keys = new ArrayList<>();
int i2 = 0;
for (int i = 1; i <= path.length(); i++) {
if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) {
keys.add(path.substring(i2, i).replace("\\.", "."));
i2 = i + 1;
}
}
path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]");

List<String> keys = Arrays.asList(pathSplitPattern.split(path));

System.out.println(keys);

ParsedElement resolved = element;

Expand All @@ -331,10 +279,12 @@ private PathResolver() {
resolved = resolved.asObject().get(keys.get(i));
} else {
if (force) {
if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) {
if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) {
resolved.asObject().set(keys.get(i), ParsedArray.create());
} else if (keys.size() > i + 1) {
resolved.asObject().set(keys.get(i), ParsedObject.create());
} else {
resolved.asObject().set(keys.get(i), ParsedArray.create());
resolved.asObject().set(keys.get(i), ParsedPrimitive.fromNull());
}

resolved = resolved.asObject().get(keys.get(i));
Expand All @@ -345,17 +295,19 @@ private PathResolver() {
}
} else if (resolved.isArray()) {
if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) {
int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1"));
int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]"));

if (index >= 0 && index < resolved.asArray().getSize()) {
resolved = resolved.asArray().get(index);
} else {
if (force) {
while (resolved.asArray().getSize() <= index) {
if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) {
if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) {
resolved.asArray().add(ParsedArray.create());
} else if (keys.size() > i + 1) {
resolved.asArray().add(ParsedObject.create());
} else {
resolved.asArray().add(ParsedArray.create());
resolved.asArray().add(ParsedPrimitive.fromNull());
}
}

Expand All @@ -381,7 +333,7 @@ private PathResolver() {
if (resolved.isObject()) {
resolved.asObject().set(valueKey, value);
} else if (resolved.isArray() && (valueKey.startsWith("[") && valueKey.endsWith("]"))) {
int resolvedValueKey = Integer.parseInt(valueKey.replaceAll("\\[([0-9]*)]", "$1"));
int resolvedValueKey = Integer.parseInt(arrayPathFixPattern.matcher(valueKey).replaceAll(".[$1]"));

if (resolvedValueKey >= 0 && resolvedValueKey < resolved.asArray().getSize()) {
resolved.asArray().set(resolvedValueKey, value);
Expand All @@ -405,7 +357,7 @@ private PathResolver() {
* @since 1.0.0
*/
public static @NotNull Set<String> getKeys(@NotNull ParsedElement element) {
return getKeys(element, true);
return getKeys(element, "", false);
}

/**
Expand Down Expand Up @@ -441,10 +393,16 @@ private PathResolver() {

for (Map.Entry<String, ParsedElement> entry : object.getEntries()) {
if (entry.getValue().isObject()) {
if (returnObjArrKeys) {
keys.add(path + entry.getKey().replace(".", "\\.")); // FIXME \. -> \\. -> split
}
keys.addAll(getKeys(entry.getValue(), path + entry.getKey().replace(".", "\\.") + ".", returnObjArrKeys));
} else if (entry.getValue().isArray()) {
if (returnObjArrKeys) {
keys.add(path + entry.getKey().replace(".", "\\."));
}
keys.addAll(getKeys(entry.getValue(), path + entry.getKey().replace(".", "\\."), returnObjArrKeys));
} else if (returnObjArrKeys || entry.getValue().isPrimitive()) {
} else if (entry.getValue().isPrimitive()) {
keys.add(path + entry.getKey().replace(".", "\\."));
}
}
Expand All @@ -453,10 +411,16 @@ private PathResolver() {

for (int i = 0; i < array.getSize(); i++) {
if (array.get(i).isObject()) {
if (returnObjArrKeys) {
keys.add(path + "[" + i + "]");
}
keys.addAll(getKeys(array.get(i), path + "[" + i + "]" + ".", returnObjArrKeys));
} else if (array.get(i).isArray()) {
if (returnObjArrKeys) {
keys.add(path + "[" + i + "]");
}
keys.addAll(getKeys(array.get(i), path + "[" + i + "]", returnObjArrKeys));
} else if (returnObjArrKeys || array.get(i).isPrimitive()) {
} else if (array.get(i).isPrimitive()) {
keys.add(path + "[" + i + "]");
}
}
Expand Down

0 comments on commit a4448b9

Please sign in to comment.