Skip to content

Commit

Permalink
ArrayToStruct add valueAsKey LDEV-3171 (#1165)
Browse files Browse the repository at this point in the history
* ArrayToStruct add valueAsKey  LDEV-3171

also changed the struct to be ordered

https://luceeserver.atlassian.net/browse/LDEV-3171

* use caster for key values

* add import java.util.Iterator;

* add imports for entry and key
  • Loading branch information
zspitzer authored Apr 1, 2021
1 parent 173fd5b commit b75a2a3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
package lucee.runtime.functions.arrays;

import java.util.Iterator;
import java.util.Map.Entry;

import lucee.runtime.PageContext;
import lucee.runtime.exp.FunctionException;
import lucee.runtime.exp.PageException;
Expand All @@ -30,25 +33,28 @@
import lucee.runtime.type.KeyImpl;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;
import lucee.runtime.type.Collection.Key;

public final class ArrayToStruct extends BIF {

private static final long serialVersionUID = 2050803318757965798L;

public static Struct call(PageContext pc, Array arr) throws PageException {
Struct sct = new StructImpl();
int[] keys = arr.intKeys();
for (int i = 0; i < keys.length; i++) {
int key = keys[i];
sct.set(KeyImpl.toKey(key + ""), arr.getE(key));
public static Struct call(PageContext pc, Array arr, boolean valueAsKey) throws PageException {
Struct sct = new StructImpl(Struct.TYPE_LINKED);
Iterator<Entry<Key, Object>> it = arr.entryIterator();
Entry<Key, Object> e;
while (it.hasNext()) {
e = it.next();
if (valueAsKey) sct.set(Caster.toKey(e.getValue()), e.getKey());
else sct.set(e.getKey(), e.getValue());
}

return sct;
}

@Override
public Object invoke(PageContext pc, Object[] args) throws PageException {
if (args.length == 1) return call(pc, Caster.toArray(args[0]));
else throw new FunctionException(pc, "ArrayToStruct", 1, 1, args.length);
if (args.length == 1) return call(pc, Caster.toArray(args[0]), false);
else if (args.length == 2) return call(pc, Caster.toArray(args[0]), Caster.toBooleanValue(args[1]));
else throw new FunctionException(pc, "ArrayToStruct", 1, 2, args.length);
}
}
13 changes: 10 additions & 3 deletions core/src/main/java/resource/fld/core-base.fld
Original file line number Diff line number Diff line change
Expand Up @@ -1487,13 +1487,20 @@ sort direction:
<class>lucee.runtime.functions.arrays.ArrayToStruct</class>
<member-name>toStruct</member-name>
<keywords>array,struct</keywords>
<description>Transform the array to a struct, the index of the array is the key of the struct</description>
<description>Transform the array to a struct, by default, the index of the array is the key of the struct</description>
<argument>
<name>array</name>
<type>array</type>
<required>Yes</required>
<description>array to translate</description>
</argument>
<description>array to translate</description>
</argument>
<argument>
<name>valueAsKey</name>
<type>boolean</type>
<required>No</required>
<default>false</default>
<description>use the value as the key of the struct, rather than the index</description>
</argument>
<return>
<type>struct</type>
</return>
Expand Down
11 changes: 11 additions & 0 deletions test/functions/ArrayToStruct.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ component extends="org.lucee.cfml.test.LuceeTestCase"{
sct=arrayToStruct(arr);
assertEquals("true",IsStruct(sct));
assertEquals("1,100",ListSort(arrayToList(StructKeyArray(sct)),"numeric"));

arr = ["a","b","c"];
sct = arrayToStruct(array=arr, valueAsKey=false);
assertEquals("true", IsStruct(sct));
assertEquals("1,2,3", structKeyList(sct) );
assertEquals("a", sct["1"]);

sct = arrayToStruct(array=arr, valueAsKey=true);
assertEquals("true", IsStruct(sct));
assertEquals("a,b,c", structKeyList(sct) );
assertEquals(1, sct.a);
});
});
}
Expand Down

0 comments on commit b75a2a3

Please sign in to comment.