Escape Keys - TomdeMan's Blog

I whipped this up to contribute to Rolando's environmentConfig project.

ColdSpring doesn't like being passed complex values, and it's not that big of a deal unless you are parsing settings from a 3rd party XML, that builds them into a nested struct.

So we came up with this to build all the nested structs into a single struct. By default it adds the parent struct's key as a prefix. This is to assure no values are overwritten.

I didn't see a UDF on CFLib for it, so I figured I'd submit and post here in case it can help anyone else out.

<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 />
</cffunction>

Comments:

[Add Comment]