From b75a2a30e9abe1969438b83cf4106383321b3528 Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Thu, 1 Apr 2021 17:51:43 +0200 Subject: [PATCH] ArrayToStruct add valueAsKey LDEV-3171 (#1165) * 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 --- .../functions/arrays/ArrayToStruct.java | 24 ++++++++++++------- core/src/main/java/resource/fld/core-base.fld | 13 +++++++--- test/functions/ArrayToStruct.cfc | 11 +++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/lucee/runtime/functions/arrays/ArrayToStruct.java b/core/src/main/java/lucee/runtime/functions/arrays/ArrayToStruct.java index 48d14c4b25..3779f96852 100644 --- a/core/src/main/java/lucee/runtime/functions/arrays/ArrayToStruct.java +++ b/core/src/main/java/lucee/runtime/functions/arrays/ArrayToStruct.java @@ -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; @@ -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> it = arr.entryIterator(); + Entry 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); } } \ No newline at end of file diff --git a/core/src/main/java/resource/fld/core-base.fld b/core/src/main/java/resource/fld/core-base.fld index 4b921dd5a6..3cb30b8c61 100755 --- a/core/src/main/java/resource/fld/core-base.fld +++ b/core/src/main/java/resource/fld/core-base.fld @@ -1487,13 +1487,20 @@ sort direction: lucee.runtime.functions.arrays.ArrayToStruct toStruct array,struct - Transform the array to a struct, the index of the array is the key of the struct + Transform the array to a struct, by default, the index of the array is the key of the struct array array Yes - array to translate - + array to translate + + + valueAsKey + boolean + No + false + use the value as the key of the struct, rather than the index + struct diff --git a/test/functions/ArrayToStruct.cfc b/test/functions/ArrayToStruct.cfc index 7e03dd04d7..2ffd88dd31 100644 --- a/test/functions/ArrayToStruct.cfc +++ b/test/functions/ArrayToStruct.cfc @@ -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); }); }); }