Created
July 15, 2012 21:34
-
-
Save skratchdot/3118727 to your computer and use it in GitHub Desktop.
A ColdFusion function to convert dot notation strings to structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
private struct function convertStringToStruct(required string key, required any value, string delimiter = ".") { | |
var obj = StructNew(); | |
var first = ListFirst(arguments.key, arguments.delimiter); | |
var rest = ListRest(arguments.key, arguments.delimiter); | |
if (Len(rest)) { | |
obj[first] = convertStringToStruct(rest, arguments.value, arguments.delimiter); | |
} else { | |
obj[first] = arguments.value; | |
} | |
return obj; | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// Declare a struct. We will later append to this | |
obj = StructNew(); | |
// Create a few top level keys | |
obj["ab"] = "foo"; | |
obj["b"] = "bar"; | |
// Append a dynamically created structure | |
StructAppend(obj, convertStringToStruct("a.b.c", "baz"), false); | |
// Show our output | |
WriteDump(obj); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment