Permalink
Browse files

normalized creating a CassandraObject from a System.Object

  • Loading branch information...
nberardi committed May 16, 2012
1 parent dcbcd0e commit 78e774c6a9019dcb84ea98b3f2e7e599b2477b4b
Showing with 5 additions and 70 deletions.
  1. +2 −70 src/Types/CassandraObject.cs
  2. +3 −0 src/Types/CassandraType.cs
@@ -208,78 +208,10 @@ public static CassandraObject GetTypeFromDatabaseValue(byte[] value, string type
public static CassandraObject GetTypeFromObject(object obj)
{
- if (obj == null)
- return NullType.Value;
-
var sourceType = obj.GetType();
- var destinationType = (Type)null;
-
- switch (Type.GetTypeCode(sourceType))
- {
-
- case TypeCode.DateTime:
- destinationType = typeof(DateType);
- break;
-
- case TypeCode.Boolean:
- destinationType = typeof(BooleanType);
- break;
-
- case TypeCode.Double:
- destinationType = typeof(DoubleType);
- break;
-
- case TypeCode.Single:
- destinationType = typeof(FloatType);
- break;
-
- case TypeCode.Int64:
- case TypeCode.UInt64:
- destinationType = typeof(LongType);
- break;
-
- case TypeCode.Int16:
- case TypeCode.Int32:
- case TypeCode.UInt16:
- case TypeCode.UInt32:
- destinationType = typeof(Int32Type);
- break;
-
- case TypeCode.Decimal:
- destinationType = typeof(DecimalType);
- break;
+ var cassandraType = CassandraType.GetCassandraType(sourceType);
- case TypeCode.Char:
- case TypeCode.String:
- destinationType = typeof(UTF8Type);
- break;
-
- case TypeCode.Byte:
- case TypeCode.SByte:
- destinationType = typeof(BytesType);
- break;
-
- default:
- if (sourceType == typeof(DateTimeOffset))
- destinationType = typeof(DateType);
-
- if (sourceType == typeof(BigInteger))
- destinationType = typeof(IntegerType);
-
- if (sourceType == typeof(Guid))
- destinationType = typeof(UUIDType);
-
- if (sourceType == typeof(char[]))
- destinationType = typeof(UTF8Type);
-
- if (destinationType == null)
- destinationType = typeof(BytesType);
- break;
- }
-
- var type = (CassandraObject)Activator.CreateInstance(destinationType);
- type.SetValue(obj);
- return type;
+ return GetTypeFromObject(obj, cassandraType);
}
public static CassandraObject GetTypeFromObject(object obj, CassandraType cassandraType)
@@ -287,6 +287,9 @@ public static CassandraType GetCassandraType(Type sourceType)
if (sourceType == typeof(char[]))
destinationType = UTF8Type;
+ if (sourceType == typeof(byte[]))
+ destinationType = BytesType;
+
if (destinationType == null)
destinationType = BytesType;
break;

0 comments on commit 78e774c

Please sign in to comment.