I've written a small function to normalize structure key case (for use in AMF data transfer on MX 6.1) and it works well so far, but when it comes to components, I get some strange behavior that I think must have something to do with passing objects by reference. My code looks like this:

    <cffunction name="structNormalize" returnType="struct" output="false" access="public"
        hint="Format a structure for use in AMF data transfer by normalizing the case of all keys.">
        <cfargument name="struct" type="struct" required="true" hint="Struct to Normalize" />
        <cfargument name="case" type="string" required="false" default="upper" hint="The case to use: upper or lower" />
        <cfset var retStruct = structNew() />
        <cfloop collection="#arguments.struct#" item="prop">
            <cfif not IsStruct(arguments.struct[prop])>
                <cfif arguments.case EQ "upper">
                    <cfset retStruct['#UCase(prop)#'] = arguments.struct[prop] />
                <cfelse>
                    <cfset retStruct['#LCase(prop)#'] = arguments.struct[prop] />
                </cfif>
            <cfelse>
                <cfif arguments.case EQ "upper">
                    <cfset retStruct['#UCase(prop)#'] = structNormalize(arguments.struct[prop]) />
                <cfelse>
                    <cfset retStruct['#LCase(prop)#'] = structNormalize(arguments.struct[prop]) />
                </cfif>
            </cfif>
        </cfloop>
        <cfreturn retStruct />
    </cffunction>

If one of the values is a component, the reference should be copied to the new struct, right? Since I'm not doing any further manipulation on the structure or it's values, I thought this wouldn't be a problem. But when I examine the structure that is returned, the object has been transformed into a structure, with key/value pairs that match up with the public functions on the object.

In my application, there are no calls to this function that will actually include an object in the structure being worked on, so this is more academic than anything. What's wrong with this function, though? I know I can't duplicate a component, but how come assignment makes a structure, instead of passing a reference to the original object? How come IsStruct() returns TRUE when passed a component instance? The docs say IsStruct() returns true on anything that implements java.lang.Map. Do I need to add in a second check for IsObject() and treat that value differently? Should I try and manipulate the key names on the original struct, instead of using a new one?

 - Ken

--
It's a metaphor for life itself. For that matter, everything in life is a metaphor for life. Except life. Life is probably a metaphor for metaphor. -- James Lileks ----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

An archive of the CFCDev list is available at www.mail-archive.com/[email protected]

Reply via email to