CFLib.org – Common Function Library Project

flattenStruct(stObject [, delimiter] [, prefix] [, stResult] [, addPrefix])

Last updated August 26, 2008
Download UDF

author

Tom de Manincor Tom de Manincor

Version: 1 | Requires: ColdFusion MX | Library: DataManipulationLib

Description:
Recursively loops through a structure with nested structures and builds nested keys and values into a single struct. http://tomdeman.com/blog/UDFs

Return Values:
Returns a structure.

Example:

<cfscript>
    stTest = structNew();
    stTest['test_root_val'] = 1;
    stTest['EC'] = structNew();
    stTest['EC'].bCreateBeanFile = true;
    stTest['EC'].bCreateColdSpringFile = true;
    stTest['EC'].sConfigBeanObjPath = 'models.GlobalConfig';
    stTest['EC'].sColdSpringDefFilePath = '/config/GlobalConfigColdspring.xml.cfm';
    stTest['EC'].sECDefinitionFilePath = '/config/environment.xml.cfm';
    stTest['EC'].sub = structNew();
    stTest['EC']['sub'].test_sub_val = 'test';
</cfscript>

<cfdump var="#flattenStruct(stTest)#" label="result">

Parameters:

Name Description Required
stObject Structure to flatten. Yes
delimiter Value to use in new keys. Defaults to a period. No
prefix Value placed in front of flattened keys. Defaults to nothing. No
stResult Structure containing result. No
addPrefix Boolean value that determines if prefix should be used. Defaults to true. No

Full UDF Source:

<!---
Builds nested structs into a single struct.

@param stObject      Structure to flatten. (Required)
@param delimiter      Value to use in new keys. Defaults to a period. (Optional)
@param prefix      Value placed in front of flattened keys. Defaults to nothing. (Optional)
@param stResult      Structure containing result. (Optional)
@param addPrefix      Boolean value that determines if prefix should be used. Defaults to true. (Optional)
@return Returns a structure.
@author Tom de Manincor (tomdeman@gmail.com)
@version 1, August 26, 2008
--->

<cffunction name="flattenStruct" access="public" output="false" returntype="struct">
    <cfargument name="stObject" required="true" type="struct" />
    <cfargument name="delimiter" required="false" type="string" default="." />
    <cfargument name="prefix"      required="false" type="string" default="" />
    <cfargument name="stResult" required="false" type="struct" default="#structNew()#" />
    <cfargument name="addPrefix" required="false" type="boolean" default="true" />
    
    <cfset var sKey = '' />
    
    <cfloop collection="#arguments.stObject#" item="sKey">        
        <cfif isSimpleValue(arguments.stObject[sKey])>
            <cfif arguments.addPrefix and len(arguments.prefix)>
                <cfset arguments.stResult[arguments.prefix & arguments.delimiter & sKey] = arguments.stObject[sKey] />
            <cfelse>
                <cfset arguments.stResult[sKey] = arguments.stObject[sKey] />
            </cfif>
        <cfelseif isStruct(arguments.stObject[sKey])>
            <cfset flattenStruct(arguments.stObject[sKey],arguments.delimiter,sKey,arguments.stResult) />    
        </cfif>
    </cfloop>
    <cfreturn arguments.stResult />
</cffunction>

Search CFLib.org


Latest Additions

Raymond Compton Raymond Compton added
structBlend
19 day(s) ago

Duncan Duncan added
IsZIPUK
19 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
25 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson