Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trycf/61610b14f7b58e3b5d37f23e096912b5 to your computer and use it in GitHub Desktop.
Save trycf/61610b14f7b58e3b5d37f23e096912b5 to your computer and use it in GitHub Desktop.
TryCF Gist
<cfscript>
/* ===============================================================================
Differences regarding array notation in setVariable() and structKeyTranslate()
================================================================================ */
writeOutput('
<h2>Differences regarding array notation in setVariable() and structKeyTranslate()</h2>
<p>When looking for a quick technique for setting values in a deeply nested struct I found 2 methods: setVariable() and structKeyTranslate(). <br />
</p>
<h3>In the scenario below I want to set a value into a struct within the first position of an array that is further nested within structures.</h3>
');
writeOutput('
<p>Using setVariable() I get the desired result. </p>
');
// myStruct1 = {};
setVariable('myStruct1.foo.servers[1].description', 'Customer Integration Environment');
writeDump(var=myStruct1, label="myStruct1 - using setVariable()");
writeOutput('
<p>Using structKeyTranslate() I get a key named "servers[1]" instead of the struct-within-array result I was looking for.</p>
');
myStruct2 = {'foo.servers[1].description': 'Customer Integration Environment'};
structKeyTranslate(myStruct2);
writeDump(var=myStruct2, label="myStruct2 - using structKeyTranslate()");
writeOutput('<hr />');
/* ===============================================================================
Detecting nested keys within arrays.
================================================================================== */
writeOutput('<h2>Detecting nested keys within arrays. </h2>');
myStruct3 = {
'foo': 'bar'
, 'seasons': [
{'name': 'hot'}
, {'name': 'rainy'}
, {'name': 'cold'}
]
};
writeDump(var=myStruct3, label='myStruct3: Struct with nested array');
writeOutput("<dl>
<dt>Is there a key in myStruct3 for seasons[2].name?</dt>
<dd>isDefined('myStruct3.seasons[2].name') = " & isDefined('myStruct3.seasons[2].name') & '<br/>');
writeOutput(
'The value is: ' & myStruct3.seasons[2].name & '</dd></dl>'
);
writeOutput('
<h3>Inserting keys with setVariable() and structKeyTranslate()</h3>
<p>This extends the issue from the first section. The difference is the struct already exists.</p>
');
// SETVARIABLE adds the value to the 5th position, inserting a null in the 4th
setVariable('myStruct3.seasons[5].name', 'Dry');
// STRUCTKEYTRANSLATE adds a 'seasons[6].name' key to root of the struct
// alt: structAppend(myStruct3, {'seasons[6].name':'Humid'});
// alt: myStruct3['seasons[6].name'] = 'Humid';
structInsert(myStruct3, 'seasons[6].name', 'Humid');
structKeyTranslate(myStruct3);
writeDump(var=myStruct3, label='Inserting keys using setVariable() and structKeyTranslate()');
writeOutput('<hr />');
writeOutput('<h1>Fun With Struct Keys</h1>');
writeOutput('<h2>Start With A Simple Struct</h2>');
OpenAPI = {
'info': {'title': 'ship'}
};
writeDump(var=OpenAPI, label="OpenAPI ORIGINAL");
writeOutput('<h2>Use StructAppend() to add key/vals to the struct</h2>');
// Many ways to add keys to a struct. I just happen to use this one.
keysToAdd = {
"paths./shipments/{version}/ship.post.description": "The Shipping API makes UPS shipping services... "
, "components.schemas.SHIPRequestWrapper.properties.ShipmentRequest.$ref": "##/components/schemas/ShipmentRequest"
, "servers[1].description": "Customer Integration Environment"
};
structAppend(OpenAPI, keysToAdd);
writeDump(var=OpenAPI, label="OpenAPI after appending the deep keys");
writeOutput(
'<h4>Does the key "components.schemas.SHIPRequestWrapper.properties.ShipmentRequest.$ref" exist in the OpenAPI struct? '
& isDefined('OpenAPI.components.schemas.SHIPRequestWrapper.properties.ShipmentRequest.$ref')
& '</h4>'
)
writeOutput('<h2>Call: StructKeyTranslate(OpenAPI)</h2>');
// Call StructKeyTranslate()
StructKeyTranslate(OpenAPI);
writeDump(var=OpenAPI, label="OpenAPI AFTER StructKeyTranslate()");
writeOutput(
'<h4>Does the key "components.schemas.SHIPRequestWrapper.properties.ShipmentRequest.$ref" exist in the OpenAPI struct? '
& isDefined('OpenAPI.components.schemas.SHIPRequestWrapper.properties.ShipmentRequest.$ref')
& '</h4>'
)
writeOutput(
'<h4>Does the key "OpenAPI.servers[1].description" exist in the OpenAPI struct? '
& isDefined('OpenAPI.servers[1].description')
& '</h4>'
)
writeOutput('<h3>Lets see if I can use setVariable to get the array correct</h3>');
// Lets see if I can use setVariable to get the array correct
setVariable('OpenAPI.servers[2].description', 'Development Environment');
writeDump(var=OpenAPI, label="OpenAPI AFTER setVariable");
writeOutput(
'<div>Does the key "OpenAPI.servers[2].description" exist in the OpenAPI struct? '
& isDefined('OpenAPI.servers[2].description')
& '</div>'
)
writeOutput('<hr />');
writeOutput('<h2>setVariable() errors when attempting to set unusal keys</h2>
<p>So why don''t I just loop over the keys and call setVariable() on each? <br/>
Because setVariable() doesn''t like the slashes, curly braces, etc. that StructKeyTranslate() allows.</p>
<div class="editor"><pre><code class="language-json" contenteditable="true">
keysToAdd = {
"paths./shipments/{version}/ship.post.description": "The Shipping API makes UPS shipping services... "
, "components.schemas.SHIPRequestWrapper.properties.ShipmentRequest.$ref": "##/components/schemas/ShipmentRequest"
, "servers[1].description": "Customer Integration Environment"
};
openAPI2 = {}
keysToAdd.each(function(key,val){
setVariable("openAPI2." & key, val);
});
writeDump(openAPI2);
</code></pre></div>
');
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment