Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active January 6, 2016 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/1ed396431dfb8e0e9f58 to your computer and use it in GitHub Desktop.
Save JamoCA/1ed396431dfb8e0e9f58 to your computer and use it in GitHub Desktop.
Update to an existing UDF to enable identification of a wider variety of object types available in Adobe ColdFusion 10. (Some object types & syntax will throw errors in ColdFusion 9.)
<!--- http://www.cflib.org/udf/TypeOf
Returns the type of the variable.
@param x The data to inspect. (optional)
@return Returns a string.
@author Jordan Clark (JordanClark@Telus.net)
@version 3, August 16, 2002
@version 4, February 20, 2015 - James Moberg (sunstarmedia.com)
Updated on Gist: 9/23/2015 7:20pm Pacific
--->
<cffunction name="TypeOf" returntype="string" output="no">
<cfscript>
var x = '';
var varType = '';
var tempVar1 = '';
var tempVar2 = '';
var canonicalName = '';
var className = '';
function isStructValueNull(struct, key) {
return listFind(structKeyList(struct), key) && !structKeyExists(struct, key);
}
function structNullKeyList(struct) {
var nulls = "";
for (var key in struct)
if (!structKeyExists(struct, key))
nulls = listAppend(nulls, key);
return nulls;
}
function getClassName(e){
return e.getClass().getName().toString();
}
if ( !ArrayLen(Arguments) ){
varType = "undefined";
} else if ( isNull(Arguments[1]) ){
varType = "null";
}
if ( !len(varType) ){
x = Arguments[1];
try {
canonicalName = getMetaData(x).getCanonicalName();
} catch (any e) {}
}
try {
className = x.getClass().getName();
} catch (any e) {}
if ( len(varType) ){
/* nothing */
} else if ( className IS "coldfusion.tagext.io.FileStreamWrapper" ){
varType = "fileHandler";
} else if ( className IS "java.io.FileReader" ){
varType = "fileHandlerJava";
} else if ( canonicalName IS "java.lang.Integer" ){
varType = "integer";
} else if ( canonicalName IS "java.lang.Float" ){
varType = "float";
} else if ( canonicalName IS "java.lang.Boolean" ){
varType = "boolean";
} else if ( canonicalName IS "java.math.BigDecimal" ){
varType = "bigdecimal";
} else if ( canonicalName IS "java.lang.Double" ){
varType = "double";
} else if ( canonicalName IS "java.util.LinkedHashMap" ){
varType = "linkedhashmap";
} else if ( canonicalName IS "coldfusion.runtime.Array" ){
varType = "array";
} else if ( canonicalName IS "java.util.ArrayList" ){
varType = "arraylist";
} else if ( canonicalName IS "coldfusion.runtime.Struct" ){
varType = "struct";
} else if ( canonicalName IS "java.lang.Long" ){
varType = "long";
} else if ( canonicalName IS "coldfusion.runtime.OleDateTime" ){
varType = "oledatetime";
} else if ( canonicalName IS "coldfusion.image.Image" ){
varType = "image";
} else if ( canonicalName IS "coldfusion.runtime.java.JavaProxy" ){
varType = "javaproxy";
} else if ( canonicalName IS "java.lang.Character" ){
varType = "character";
} else if ( canonicalName IS "coldfusion.xml.XmlNodeList" ){
varType = "xml";
} else if ( canonicalName IS "java.util.Collections.SynchronizedRandomAccessList" ){
varType = "java/synchronizedrandomaccesslist";
} else if ( canonicalName IS "java.lang.String" ){
varType = "string";
if (reFindNoCase("[0-9a-f]{8,8}-[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]{12,12}",x) IS 1 AND LEN(x) IS 36){
varType = "string/mssqluuid";
} else if ( isValid("integer", x) ){
varType = "string/integer";
} else if ( Listfindnocase("yes,no,true,false", x) ){
varType = "string/boolean";
} else if ( isDate(x)){
varType = "string/date";
} else if ( isValid("float", x) ){
varType = "string/float";
} else if ( isNumeric(x) ){
varType = "string/numeric";
} else if ( isJSON(x) ){
varType = "string/json";
} else if ( isWDDX(x) ){
varType = "string/wddx";
} else if ( isDDX(x) ){
varType = "string/ddx";
} else if ( isValid("UUID", x) ){
varType = "string/uuid";
} else if (isDate(x) AND (dateformat(parseDateTime(x),'m/d/yyyy') IS "1/1/1970" OR dateformat(parseDateTime(x),'m/d/yyyy') IS "12/30/1899")){
varType = "string/time";
}
} else if ( isClosure(x) ){
varType = "closure";
} else if ( IsPDFObject(x) ){
varType = "pdf"; /* 1/6/2016 Note: PDFs created in-memory may be incorrectly identified as "binary" */
} else if ( isBinary(x) ){
varType = "binary";
} else if ( isArray(x) ){
varType = "array";
} else if ( IsImage(x) ){
varType = "image";
} else if ( IsSpreadsheetObject(x) ){
varType = "spreadsheet object";
} else if ( IsObject(x) ){
varType = "object";
} else if ( isStruct(x) ){
varType = "structure";
} else if ( isQuery(x) ){
varType = "query";
for (tempvar=1; tempvar LTE ArrayLen(getMetaData(x)); tempvar=tempvar+1) {
if (compare(x.getMetaData().getColumnTypeName(tempvar), lcase(x.getMetaData().getColumnTypeName(tempvar))) NEQ 0 ){
varType = "query/query-of-queries";
break;
}
}
} else if ( isCustomFunction(x) ){
varType = "udf";
} else if ( isXMLDoc(x) ){
varType = "xml";
} else if ( isXMLAttribute(x) ){
varType = "xml attribute";
} else if ( IsXmlElem(x) ){
varType = "xml element";
} else if ( IsXmlRoot(x) ){
varType = "xml root";
} else if ( isXMLNode(x) ){
varType = "xml node";
} else {
varType = 'unknown';
}
return varType;
</cfscript>
</cffunction>
<!---
Functions used for tests
--->
<cfscript>
function testCanonicalName(x){
var r = '';
try {
r = getMetaData(x).getCanonicalName();
} catch (any e) {
r = 'FAIL';
}
return r;
}
function testClassName(x){
var r = '';
try {
r = x.getClass().getName();
} catch (any e) {
r = 'FAIL';
}
return r;
}
</cfscript>
<!---
Generate test objects (Some require CFCs or ODBC data sources)
---->
<cftry>
<cfset imageutils = createObject("component", "imageUtils")>
<cfcatch></cfcatch>
</cftry>
<cfxml variable="xmlobject">
<order id="4323251">
<customer firstname="Fred" lastname="Flintstone" accountNum="21"/>
<items>
<item id="43">
<quantity>1</quantity>
<unitprice>15.95</unitprice>
</item>
</items>
</order>
</cfxml>
<cftry>
<CFQUERY NAME="GetSQLData" DATASOURCE="ZipStuff">SELECT TOP 1 Zip, City, Date1, Date2 FROM Zips</CFQUERY>
<cfcatch></cfcatch>
</cftry>
<cftry>
<CFQUERY NAME="GetSQLData_QofQ" DBTYPE="query">SELECT * FROM GetSQLData</CFQUERY>
<cfcatch></cfcatch>
</cftry>
/* Create these sample files */
<cfdocument name="varPDF" format="pdf"><cfoutput>This is a PDF</cfoutput></cfdocument>
<cfpdf name="filePDF" source="c:\test.pdf" action="read">
<cfset TestFilePath = "c:\testfile.txt">
<cfscript>
test = {};
test["shouldBe_Xml"] = xmlobject;
test["shouldBe_Array"] = [1,2,3];
test["shouldBe_Structure"] = {a=1, b=2, c=3};
/* dependent on files */
test["shouldBe_PDFvar"] = duplicate(varPDF);
test["shouldBe_PDFfile"] = duplicate(filePDF);
test["shouldBe_fileHandler"] = fileOpen(TestFilePath);
test["shouldBe_fileHandlerJava"] = createobject("java","java.io.FileReader");
test["shouldBe_fileHandlerJava"].init("#FileRootDir#\wkhtmltopdf_CF.pdf");
test["shouldBe_Hashmap"] = CreateObject("java", "java.util.LinkedHashMap").init();
test["shouldBe_Hashmap"]["a"] = 1;
test["shouldBe_Hashmap"]["b"] = 2;
test["shouldBe_Hashmap"]["c"] = 3;
try {
test["shouldBe_Java"] = CreateObject("java", "ChartDirector.CFChart");
} catch (any e) {}
test["shouldBe_Date"] = "1/1/1999";
test["shouldBe_OleDate"] = createDate( 2002, 4, 1 );
test["shouldBe_OleDateTime"] = createTime( 5, 5, 5 );
test["shouldBe_Binary"] = Tobinary( ToBase64( "foo" ) );
test["shouldBe_Image"] = ImageNew();
test["shouldBe_IntegerString"] = 1;
test["shouldBe_Numeric"] = 1.05;
try {
test["shouldBe_Closure"] = function(a,b){ return a + b; };
} catch (any e) {}
test["shouldBe_JSON"] = '{"a":1, "b":2, "c":3, "d":null }';
test["shouldBe_JSONStruct"] = deserializeJSON(test["shouldBe_JSON"]);
test["shouldBe_String"] = "abc";
test["shouldBe_QueryofQueriesNew"] = queryNew("a,b,c", "Integer,Varchar,Date");
function shouldBe_UDF() { return; }
test["shouldBe_UDF"] = shouldBe_UDF;
try {
test["shouldBe_Object"] = imageutils;
} catch (any e) {}
test["shouldBe_Spreadsheet"] = SpreadsheetNew();
try {
test["shouldBe_SQLQuery"] = GetSQLData;
} catch (any e) {}
try {
test["shouldBe_QueryofQueries"] = GetSQLData_QofQ;
} catch (any e) {}
test["shouldBe_Integer"] = javacast("int", "1234");
test["shouldBe_Long"] = javacast("long", "1234.1231232");
test["shouldBe_Float"] = javacast("float", "1234.12312");
test["shouldBe_Double"] = javacast("double", "1234.12312");
test["shouldBe_BigDecimal"] = javacast("bigdecimal", "1234.12312");
test["shouldBe_Boolean"] = javacast("boolean", true);
test["shouldBe_BooleanString"] = "true";
test["shouldBe_Char"] = javacast("char", "a");
test["shouldBe_JavaString"] = javacast("string", "a");
test["shouldBe_UUID"] = CreateUUID();
test["shouldBe_MSSQLUUID"] = "18aafafe-4321-43f6-a565-00049d9d4d66";
test["shouldBe_List"] = test["shouldBe_Array"].subList(0,2);
test["shouldBe_ArrayList"] = CreateObject( "java", "java.util.ArrayList" ).Init();
test["shouldBe_ArrayList"].Add("a");
test["shouldBe_ArrayList"].Add("b");
test["shouldBe_ArrayList"].Add("c");
</cfscript>
<cfoutput>
<CFSET TestKeys = ListSort(StructKeyList(Test), "textnocase")>
<table border=1 cellspacing=0 cellpadding=3>
<thead>
<tr><th>Test</th><th>Response</th><th>getCanonicalName()</th><th>getClass().getName()</th><th>Dump</th></tr>
</thead>
<tbody>
<CFLOOP LIST="#TestKeys#" INDEX="thisTest">
<CFSET typeOfResult = typeOf( Test[thisTest] )>
<CFSET canonicalNameResult = testCanonicalName( Test[thisTest] )>
<CFSET classNameResult = testClassName( Test[thisTest] )>
<tr><td>#ListLast(thisTest,"_")#</td>
<td>#typeOfResult#</td>
<td>#canonicalNameResult#</td>
<td>#classNameResult#</td>
<td><cfdump var="#Test[thisTest]#" expand="no" showudfs="no" output="browser" metainfo="no"></td>
</tr>
</CFLOOP>
</tbody>
</table>
</cfoutput>
<cfscript>
try {
fileClose(test["shouldBe_fileHandler"]);
} catch (any e) {}
try {
test["shouldBe_fileHandlerJava"].close();
} catch (any e) {}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment