Skip to content

Commit

Permalink
More path resolver functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale-Ko committed Apr 10, 2023
1 parent 8f72000 commit c054415
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
java_version=17

project_version=2.0.0
project_version=2.1.0
project_description=A semi-advanced Java configuration library with support for local files, mysql, and more.

bjsl_version=1.4.3
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/io/github/kale_ko/ejcl/PathResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,52 @@ public static Object resolve(ParsedElement element, String path) {
return null;
}

/**
* Resolve an element on an element
*
* @param element
* The element to resolve on
* @param path
* The path to resolve to
* @return The value resolved
* @since 1.0.0
*/
public static ParsedElement resolveElement(ParsedElement element, String path) {
String[] keys = path.replaceAll("\\[([0-9])\\]", ".[$1]").split("\\.");

ParsedElement resolved = element;

for (String key : keys) {
if (resolved.isObject()) {
if (resolved.asObject().has(key)) {
resolved = resolved.asObject().get(key);
} else {
resolved = null;
break;
}
} else if (resolved.isArray()) {
if (key.startsWith("[") && key.endsWith("]")) {
int index = Integer.parseInt(key.replaceAll("\\[([0-9])\\]", "$1"));

if (index >= 0 && index < resolved.asArray().getSize()) {
resolved = resolved.asArray().get(index);
} else {
resolved = null;
break;
}
} else {
resolved = null;
break;
}
} else {
resolved = null;
break;
}
}

return resolved;
}

/**
* Update a value on an element
*
Expand Down Expand Up @@ -164,6 +210,72 @@ public static ParsedElement update(ParsedElement element, String path, Object va
return element;
}

/**
* Update an element on an element
*
* @param element
* The element to update on
* @param path
* The path to update
* @param value
* The value to update to
* @return element for chaining
* @since 1.0.0
*/
public static ParsedElement updateElement(ParsedElement element, String path, ParsedElement value) {
String[] keys = path.replaceAll("\\[([0-9])\\]", ".[$1]").split("\\.");
String valueKey = keys[keys.length - 1];
keys = Arrays.copyOf(keys, keys.length - 1);

ParsedElement resolved = element;

for (String key : keys) {
if (resolved.isObject()) {
if (resolved.asObject().has(key)) {
resolved = resolved.asObject().get(key);
} else {
resolved = null;
break;
}
} else if (resolved.isArray()) {
if (key.startsWith("[") && key.endsWith("]")) {
int index = Integer.parseInt(key.replaceAll("\\[([0-9])\\]", "$1"));

if (index >= 0 && index < resolved.asArray().getSize()) {
resolved = resolved.asArray().get(index);
} else {
resolved = null;
break;
}
} else {
resolved = null;
break;
}
} else {
resolved = null;
break;
}
}

if (resolved != null) {
if (resolved.isObject()) {
resolved.asObject().set(valueKey, value);
} else if (resolved.isArray()) {
if (valueKey.startsWith("[") && valueKey.endsWith("]")) {
Integer resolvedValueKey = Integer.parseInt(valueKey.replaceAll("\\[([0-9])\\]", "$1"));

if (resolvedValueKey >= 0 && resolvedValueKey < resolved.asArray().getSize()) {
resolved.asArray().set(resolvedValueKey, value);
} else {
resolved.asArray().add(value);
}
}
}
}

return element;
}

/**
* Get all the keys in an element
*
Expand Down

0 comments on commit c054415

Please sign in to comment.