Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trycf/8f8a2ef732acbab60aa08a71593986e7 to your computer and use it in GitHub Desktop.
Save trycf/8f8a2ef732acbab60aa08a71593986e7 to your computer and use it in GitHub Desktop.
TryCF Gist
<cfscript>
private struct function getBlankRateBodyStruct() output = "false" hint = "return default rate body struct" {
var jsonString = {
"RateRequest": {
"Request": {
"TransactionReference": {
"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": "ShipmentChargeType",
"BillShipper": {
"AccountNumber": "AccountNumber"
}
}
]
},
"Service": {
"Code": "ServiceCode",
"Description": "ServiceDescription"
},
"NumOfPieces": "NumOfPieces",
"Package": {
"SimpleRate": {
"Description": "SimpleRateDescription",
"Code": "SimpleRateCode"
},
"PackagingType": {
"Code": "PackagingTypeCode",
"Description": "PackagingTypeDescription"
},
"Dimensions": {
"UnitOfMeasurement": {
"Code": "IN",
"Description": "Inches"
},
"Length": "",
"Width": "",
"Height": ""
},
"PackageWeight": {
"UnitOfMeasurement": {
"Code": "LBS",
"Description": "Pounds"
},
"Weight": ""
}
},
"DeliveryTimeInformation": {
"PackageBillType": "PackageBillTypeCode",
"Pickup": {
"Date": "PickupDate",
"Time": "PickupTime"
}
}
}
}
};
return jsonString;
} // end getBlankRateBody
//---------------------------------------------------------------------------------------------------------------------------------
private void function updateMasterStruct(required struct masterStruct, required struct keyValueStruct) output="true"
hint="Updates the master struct (by reference) with values from the keyValueStruct where keys match either values in the master struct or array elements" {
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
for (var i = 1; i <= arrayLen(masterStruct[masterKey]); i++) {
if (isStruct(masterStruct[masterKey][i])) {
// If array element is a struct, recurse into it
updateMasterStruct(masterStruct[masterKey][i], keyValueStruct);
} else if (masterStruct[masterKey][i] == key) {
// If array element matches the key, update it
masterStruct[masterKey][i] = keyValueStruct[key];
}
}
} 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");
}
}
//---------------------------------------------------------------------------------------------------------------------------------
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";
}
packages = [];
localRateStruct = duplicate( getBlankRateBodyStruct() );
writeDump( var="#localRateStruct#", label="localRateStruct - pre", expand="false" );
// UPDATE based on unique/known VALUES in RateStruct
// this will update any values that match to a new value i.e. "ShipperName"
keyValueStruct = {};
keyValueStruct[ "ShipperName" ] = "IICL";
keyValueStruct[ "ShipFromAddressLine" ] = "UUSSAA";
writeDump( var="#keyValueStruct#", label="keyValueStruct", expand="true" );
updateMasterStruct( masterStruct = localRateStruct, keyValueStruct = keyValueStruct );
// UPDATE based on unique/known KEYS in RateStruct
// 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() : "" );
valStruct[ "NumOfPieces" ] = (( arraylen( packages )) ? arrayLen( packages ) : "" );
valStruct[ "ShipFromAddressLine" ] = "UUSSAA";
writeDump( var="#valStruct#", label="valStruct", expand="true" );
updateStructValues( sourceStruct = localRateStruct, updateStruct = valStruct );
//localRateStruct.RateRequest.Shipment.Package = packages;
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