Created
November 15, 2024 21:33
-
-
Save trycf/d2cd7932b790d2d663ffdf73ca8233c0 to your computer and use it in GitHub Desktop.
TryCF Gist
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 getBlankRateBodyStruct() output = "false" hint = "return default rate body struct" { | |
var jsonString = { | |
"RateRequest": { | |
"Request": { | |
"TransactionReference": { | |
"CustomerContext": "CustomerContext" | |
} | |
}, | |
"Shipment": { | |
"Shipper": { | |
"Name": "ShipperName", | |
"ShipperNumber": "ShipperNumber", | |
"Address": { | |
"AddressLine": [ | |
"ShipperAddressLine1", | |
"ShipperAddressLine2", | |
"ShipperAddressLine3" | |
], | |
"City": "ShipperCity", | |
"StateProvinceCode": "ShipperStateProvince", | |
"PostalCode": "ShipperPostalCode", | |
"CountryCode": "ShipperCountry" | |
} | |
}, | |
"ShipTo": { | |
"Name": "ShipToName", | |
"Address": { | |
"AddressLine": [ | |
"ShipToAddressLine", | |
"ShipToAddressLine", | |
"ShipToAddressLine" | |
], | |
"City": "ShipToCity", | |
"StateProvinceCode": "ShipToStateProvice", | |
"PostalCode": "ShipToPostalCode", | |
"CountryCode": "ShipToCountry" | |
} | |
}, | |
"ShipFrom": { | |
"Name": "ShipFromName", | |
"Address": { | |
"AddressLine": [ | |
"ShipFromAddressLine", | |
"ShipFromAddressLine", | |
"ShipFromAddressLine" | |
], | |
"City": "ShipFromCity", | |
"StateProvinceCode": "ShipFromStateProvince", | |
"PostalCode": "ShipFromPostalCode", | |
"CountryCode": "ShipFromCountry" | |
} | |
}, | |
"PaymentDetails": { | |
"ShipmentCharge": [ | |
{ | |
"Type": "01", | |
"BillShipper": { | |
"AccountNumber": "" | |
} | |
} | |
] | |
}, | |
"Service": { | |
"Code": "03", | |
"Description": "Ground" | |
}, | |
"NumOfPieces": "1", | |
"Package": { | |
"SimpleRate": { | |
"Description": "SimpleRateDescription", | |
"Code": "XS" | |
}, | |
"PackagingType": { | |
"Code": "02", | |
"Description": "Packaging" | |
}, | |
"Dimensions": { | |
"UnitOfMeasurement": { | |
"Code": "IN", | |
"Description": "Inches" | |
}, | |
"Length": "5", | |
"Width": "5", | |
"Height": "5" | |
}, | |
"PackageWeight": { | |
"UnitOfMeasurement": { | |
"Code": "LBS", | |
"Description": "Pounds" | |
}, | |
"Weight": "1" | |
} | |
}, | |
"DeliveryTimeInformation": { | |
"PackageBillType": "03", | |
"Pickup": { | |
"Date": "20240101", | |
"Time": "1000" | |
} | |
} | |
} | |
} | |
}; | |
return jsonString; | |
} // end getBlankRateBody | |
//--------------------------------------------------------------------------------------------------------------------------------- | |
private void function updateMasterStruct( required struct masterStruct, required struct keyValueStruct) output = "true" hint = "This function updates the master struct (by reference) with values from the keyValueStruct where keys match the values in the master struct" { | |
try{ | |
// Loop through each key in the keyValueStruct | |
for (var key in keyValueStruct) { | |
// Loop through the masterStruct to find the matching value | |
for (var masterKey in masterStruct) { | |
if (isStruct(masterStruct[masterKey])) { | |
// Recursively call the function for nested structs | |
updateMasterStruct(masterStruct[masterKey], keyValueStruct); | |
} else if (isArray(masterStruct[masterKey])) { | |
// Loop through the array of structs | |
for (var i = 1; i <= arrayLen(masterStruct[masterKey]); i++) { | |
if (isStruct(masterStruct[masterKey][i])) { | |
updateMasterStruct(masterStruct[masterKey][i], keyValueStruct); | |
} | |
} | |
} else if (masterStruct[masterKey] == key) { | |
// Update the value in the masterStruct | |
masterStruct[masterKey] = keyValueStruct[key]; | |
} | |
} | |
} | |
} catch( any e ){ | |
writeDump( var="#e#", label="e", expand="true" ); | |
} | |
} // end updateMasterStruct | |
//--------------------------------------------------------------------------------------------------------------------------------- | |
private void function updateStructValues( required struct sourceStruct, required struct updateStruct ) output = "false" hint = "Updates the values in the source structure with the values from the update structure where the key matches" { | |
for( var key in updateStruct ) { | |
// Call the recursive function to update the values | |
updateNestedStructValues( sourceStruct, key, updateStruct[ key ] ); | |
} | |
} // end updateStructValues | |
//--------------------------------------------------------------------------------------------------------------------------------- | |
private void function updateNestedStructValues( required struct oStructure, required any keyToUpdate, required any newValue ) output = "false" hint = "Updates the values of a specified key in a nested structure" { | |
for ( var key in oStructure ) { | |
if ( isStruct( oStructure[ key ] ) ) { | |
// If the value is a struct, call the function recursively | |
updateNestedStructValues( oStructure[ key ], keyToUpdate, newValue ); | |
} else if ( isArray( oStructure[ key ] ) ) { | |
// If the value is an array, iterate through the array | |
for ( var i = 1; i <= arrayLen( oStructure[ key ] ); i++ ) { | |
if ( isStruct( oStructure[ key ][ i ] ) ) { | |
// If the array element is a struct, call the function recursively | |
updateNestedStructValues( oStructure[ key ][ i ], keyToUpdate, newValue ); | |
} | |
} | |
} else if ( key == keyToUpdate ) { | |
// If the key matches, update the value | |
oStructure[ key ] = newValue; | |
} | |
} | |
} // end updateNestedStructValues | |
private string function getBillingAccountNumber(){ | |
return "1234554321"; | |
} | |
localRateStruct = duplicate( getBlankRateBodyStruct() ); | |
writeDump( var="#localRateStruct#", label="localRateStruct - pre", expand="true" ); | |
// this will update any values that match to a new value i.e. "ShipperName" | |
keyValueStruct = {}; | |
keyValueStruct[ "ShipperName" ] = "IICL"; | |
writeDump( var="#keyValueStruct#", label="keyValueStruct", expand="true" ); | |
updateMasterStruct( masterStruct = localRateStruct, keyValueStruct = keyValueStruct ); | |
// this will update any keys found in the sourceStruct with values from matching keys in the updateStruct, will change ALL regardless of position/depth i.e. CountryCode | |
valStruct = {}; | |
valStruct[ "ShipperNumber" ] = (( len( getBillingAccountNumber() )) ? getBillingAccountNumber() : "" ); | |
valStruct[ "AccountNumber" ] = (( len( getBillingAccountNumber() )) ? getBillingAccountNumber() : "" ); | |
writeDump( var="#valStruct#", label="valStruct", expand="true" ); | |
updateStructValues( sourceStruct = localRateStruct, updateStruct = valStruct ); | |
writeDump( var="#localRateStruct#", label="localRateStruct - post", expand="true" ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment