Skip to content

Commit

Permalink
LDEV-5043 - add support for cb structures to cfconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 13, 2024
1 parent e7821b4 commit 0057f4c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
8 changes: 4 additions & 4 deletions core/src/main/java/lucee/runtime/config/ConfigAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ public void removeRestMapping(String virtual) throws ExpressionException, Securi
public void removeCustomTag(String virtual) throws SecurityException {
checkWriteAccess();

Array mappings = ConfigWebUtil.getAsArray("customTagMappings", root);
Array mappings = ConfigWebUtil.getAsArray(root, true, KeyConstants._virtual, KeyConstants._physical, false, "customTagMappings", "customTagPaths");
Key[] keys = mappings.keys();
Struct data;
String v;
Expand Down Expand Up @@ -981,7 +981,7 @@ private void _removeScheduledTask(String name) throws ExpressionException {
public void removeComponentMapping(String virtual) throws SecurityException {
checkWriteAccess();

Array mappings = ConfigWebUtil.getAsArray("componentMappings", root);
Array mappings = ConfigWebUtil.getAsArray(root, true, KeyConstants._virtual, KeyConstants._physical, false, "componentMappings", "componentPaths");
Key[] keys = mappings.keys();
Struct data;
String v;
Expand Down Expand Up @@ -1032,7 +1032,7 @@ private void _updateCustomTag(String virtual, String physical, String archive, S
throw new ExpressionException("physical must have a value when primary has value physical");
}

Array mappings = ConfigWebUtil.getAsArray("customTagMappings", root);
Array mappings = ConfigWebUtil.getAsArray(root, true, KeyConstants._virtual, KeyConstants._physical, false, "customTagMappings", "customTagPaths");
Key[] keys = mappings.keys();
// Update
String v;
Expand Down Expand Up @@ -1131,7 +1131,7 @@ private void _updateComponentMapping(String virtual, String physical, String arc
throw new ExpressionException("physical must have a value when primary has value physical");
}

Array componentMappings = ConfigWebUtil.getAsArray("componentMappings", root);
Array componentMappings = ConfigWebUtil.getAsArray(root, true, KeyConstants._virtual, KeyConstants._physical, false, "componentMappings", "componentPaths");
Key[] keys = componentMappings.keys();
Struct el;

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/lucee/runtime/config/ConfigWebFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2924,8 +2924,7 @@ else if (hasCS) {
}
}

// Struct customTag = ConfigWebUtil.getAsStruct("customTag", root);
Array ctMappings = ConfigWebUtil.getAsArray("customTagMappings", root);
Array ctMappings = ConfigWebUtil.getAsArray(root, true, KeyConstants._virtual, KeyConstants._physical, true, "customTagMappings", "customTagPaths");

// Web Mapping
boolean hasSet = false;
Expand Down Expand Up @@ -5267,7 +5266,8 @@ else if (configServer != null) {
// sct.setEL("physical", "{lucee-config}/components/");

// Web Mapping
Array compMappings = ConfigWebUtil.getAsArray("componentMappings", root);

Array compMappings = ConfigWebUtil.getAsArray(root, true, KeyConstants._virtual, KeyConstants._physical, false, "componentMappings", "componentPaths");
hasSet = false;
Mapping[] mappings = null;
if (hasAccess) {
Expand Down
76 changes: 76 additions & 0 deletions core/src/main/java/lucee/runtime/config/ConfigWebUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,82 @@ public static Array getAsArray(String name, Struct sct) {
return tmp;
}

/**
* get an array that matches oe of the given key or creates the array in place
*
* @param input struct to look for the names
* @param convertStructToArray if true and the value is a struct, convert it to an array
* @param convertKey if the value is a struct the key of that strcut will be copied to the array
* with that name
* @param stringKey in case the array contains string values and this key is set, create a struct
* containing a key with the string as value
* @param names
* @return
* @throws PageException
*/
public static Array getAsArray(Struct input, boolean convertStructToArray, Key convertKey, Key stringKey, boolean replacePlaceHolder, String... names) {
Array arr = null;
if (input == null) return arr;

Object obj;
for (String name: names) {
obj = input.get(KeyImpl.init(name), null);
if (obj instanceof Array && (arr = (Array) obj).size() > 0) {
if (name != names[0]) {
input.setEL(KeyImpl.init(names[0]), arr);
input.removeEL(KeyImpl.init(name));
}
break;
}
if (arr == null && convertStructToArray && obj instanceof Struct) {
Struct sct = (Struct) obj;
arr = new ArrayImpl();
input.setEL(KeyImpl.init(name), arr);
if (name != names[0]) {
input.setEL(KeyImpl.init(names[0]), arr);
input.removeEL(KeyImpl.init(name));
}
Iterator<Entry<Key, Object>> it = sct.entryIterator();
Entry<Key, Object> e;
Struct s;
Object v;
while (it.hasNext()) {
e = it.next();
v = e.getValue();
if (convertKey != null && v instanceof Struct) {
s = (Struct) v;
if (!s.containsKey(convertKey)) s.setEL(convertKey, e.getKey().getString());
}
arr.appendEL(v);
}
break;
}
}

// validate the array values
if (arr != null && stringKey != null) {
Key[] keys = arr.keys();
Object val;
for (Key k: keys) {
val = arr.get(k, null);
if (val instanceof CharSequence) {
Struct sct = new StructImpl();
sct.setEL(stringKey, val);
arr.setEL(k, sct);
}
}
}
else if (arr == null) {
arr = new ArrayImpl();
input.setEL(KeyImpl.init(names[0]), arr);
return arr;
}

if (replacePlaceHolder) return (Array) replaceConfigPlaceHolders(arr);

return arr;
}

public static String getAsString(String name, Struct sct, String defaultValue) {
if (sct == null) return defaultValue;
Object obj = sct.get(KeyImpl.init(name), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2993,6 +2993,7 @@ public class KeyConstants {
public static final Key _metric = KeyImpl._const("metric");
public static final Key _monitoring = KeyImpl._const("monitoring");
public static final Key _expression = KeyImpl._const("expression");
public static final Key _physical = KeyImpl._const("physical");
private static Map<String, Key> _____keys;

static {
Expand Down

0 comments on commit 0057f4c

Please sign in to comment.