Utility Function: StructMerge

If all you need to do is merge two simple structs, then StructAppend() is a great function to get you there, but if you have complex structs (meaning structs that contain structs) you may be wanting to find a solution that does a deep merge:

<cffunction name="StructMerge" access="public" output="false" returntype="struct">
    <cfargument name="Struct1" type="struct" required="true" />
    <cfargument name="Struct2" type="struct" required="true" />
    <cfargument name="Overwrite" type="boolean" required="false" default="true" />
    <!--- Loop Keys --->
    <cfloop collection="#Arguments.Struct2#" item="Local.Key">
        <!--- Find if the new key from Struct2 Exists in Struct1 --->
        <cfif StructKeyExists(Arguments.Struct1, Local.Key)>
            <!--- If they are both structs, we need to merge those structs, too --->
            <cfif IsStruct(Arguments.Struct1[Local.Key]) AND IsStruct(Arguments.Struct2[Local.Key])>
                <!--- Recursively call StructMerge to merge those structs --->
                <cfset StructMerge(Arguments.Struct1[Local.Key], Arguments.Struct2[Local.Key], Arguments.Overwrite) />
            <!--- We already checked that the key existed, now we just check if we can overwrite it --->
            <cfelseif Arguments.Overwrite>
                <cfset Arguments.Struct1[Local.Key] = Arguments.Struct2[Local.Key] />
            <!--- The unused case here is if Overwrite is false, in which case Struct1 is not changed --->
        <!--- If it doesn't exist, you're free to merge --->
            <cfset Arguments.Struct1[Local.Key] = Arguments.Struct2[Local.Key] />
    <cfreturn Arguments.Struct1 />

This code merges all sub structures as well as the core one. I'd also say it would be an easy adjustment to merge arrays and other data types if necessary. I'm not sure if CF10 will be adding something like this or not, but I know for CF9.0.1 I needed it.


ColdFusion 9 ISAPI Only Runs in 32-Bit Mode

I recently updated my work laptop to 64-Bit Windows 7 so that I could up my RAM, and found that after installing CF IIS 7.0 threw 0x800700c1 errors. Luckily, after some googling, I found the answer.


ColdFusion Spreadsheet Headers Ignore Empty Columns

This is one of those notes-for-my-future-self type posts related to a bizarre behavior with ColdFusion's support. ColdFusion ignores any column at the end of your document where the only data was in the row you specified as a headerrow (assuming you ignore the header row). Got that?


Utility Functions: Two UDFs to Make Spreadsheets Easier

I started diving into using ColdFusion's capabilities to work with Excel spreadsheets for the first time today. Even though its been a part of ColdFusion since CF9 dropped in 2009, I've not been called on to use the functionality, so I'm a bit behind the curve. Once I got moving though, I found some annoyances and gotcha's that drove me to come up with a couple of new UDFs to ease my pain. Find SpreadsheetExpandedInfo() and SpreadsheetGetData() after the jump.


Problems with Day() and CreateTimeSpan()

I'm attempting to use CreateTimeSpan() to hold information about how long my system will be down for maintenance given a starting date and time, and I've been running into a few interesting issues. I'd have thought that this would be a simple matter, as adding a timespan to an existing time stamp would be a useful mechanic, but it seems as though there are some hidden gotchas and, perhaps, some outright bugs.


Useful Link: Hacking your ColdFusion Administrator Password

I found myself in a tight spot earlier today; I couldn't remember the password I supplied to ColdFusion Administrator on one of my machines. Since Adobe doesn't offer a recovery mechanism, I was worried I'd need to do a full re installation to purge the password. I turns out the solution is a lot easier than I expected:



More Entries

Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me.

Post a job. Find one. authenticjobs.com

Interested in becoming a sponsor? Contact me.