Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added EmptyType for issue #97

  • Loading branch information...
commit d563c26efeb589f8e7597bb48e24b08da3b80ec2 1 parent 8825803
@nberardi nberardi authored
View
1  src/FluentCassandra.csproj
@@ -279,6 +279,7 @@
<Compile Include="Types\CounterColumnType.cs" />
<Compile Include="Types\DateType.cs" />
<Compile Include="Types\DateTypeConverter.cs" />
+ <Compile Include="Types\EmptyType.cs" />
<Compile Include="Types\Int32Type.cs" />
<Compile Include="Types\Int32TypeConverter.cs" />
<Compile Include="Types\FloatType.cs" />
View
4 src/Types/CassandraType.cs
@@ -23,6 +23,7 @@ public sealed class CassandraType
public static readonly CassandraType UTF8Type = new CassandraType("org.apache.cassandra.db.marshal.UTF8Type");
public static readonly CassandraType UUIDType = new CassandraType("org.apache.cassandra.db.marshal.UUIDType");
public static readonly CassandraType CounterColumnType = new CassandraType("org.apache.cassandra.db.marshal.CounterColumnType");
+ public static readonly CassandraType EmptyType = new CassandraType("org.apache.cassandra.db.marshal.EmptyType");
private static readonly CassandraType _CompositeType = new CassandraType("org.apache.cassandra.db.marshal.CompositeType");
private static readonly CassandraType _DynamicCompositeType = new CassandraType("org.apache.cassandra.db.marshal.DynamicCompositeType");
@@ -176,6 +177,7 @@ private Type Parse(string dbType)
case "dynamiccompositetype": type = typeof(DynamicCompositeType); break;
case "countercolumntype": type = typeof(CounterColumnType); break;
case "reversedtype": type = typeof(ReversedType); break;
+ case "emptytype": type = typeof(EmptyType); break;
default: throw new CassandraException("Type '" + _dbType + "' not found.");
}
@@ -246,7 +248,7 @@ public static CassandraType GetCassandraType(CassandraObject obj)
case "timeuuidtype": cassandraType = TimeUUIDType; break;
case "utf8type": cassandraType = UTF8Type; break;
case "uuidtype": cassandraType = UUIDType; break;
-
+ case "emptytype": cassandraType = EmptyType; break;
// these need work
//case "compositetype": cassandraType = CompositeType; break;
//case "dynamiccompositetype": cassandraType = DynamicCompositeType; break;
View
109 src/Types/EmptyType.cs
@@ -0,0 +1,109 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace FluentCassandra.Types
+{
+ public class EmptyType : CassandraObject
+ {
+ #region Implimentation
+
+ public override void SetValue(object obj)
+ {
+ if (obj == null)
+ return;
+
+ var type = obj.GetType();
+
+ if (type == typeof(string) && String.IsNullOrEmpty((string)obj))
+ return;
+
+ if (type == typeof(byte[]) && ((byte[])obj).Length == 0)
+ return;
+
+ throw new NotSupportedException("You cannot set the value of an EmptyType.");
+ }
+
+ protected override object GetValueInternal(Type type)
+ {
+ if (type == typeof(string))
+ return "";
+ else if (type == typeof(byte[]))
+ return _value;
+ else
+ throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", typeof(byte[]), type));
+ }
+
+ protected override TypeCode TypeCode
+ {
+ get { return TypeCode.Object; }
+ }
+
+ public override byte[] ToBigEndian()
+ {
+ return _value;
+ }
+
+ public override void SetValueFromBigEndian(byte[] value)
+ {
+ }
+
+ #endregion
+
+ public override object GetValue()
+ {
+ return _value;
+ }
+
+ private readonly byte[] _value = new byte[0];
+
+ #region Equality
+
+ public override bool Equals(object obj)
+ {
+ if (obj is EmptyType)
+ return _value == ((EmptyType)obj)._value;
+
+ if (obj is string)
+ return String.IsNullOrEmpty((string)obj);
+
+ if (obj is byte[])
+ return ((byte[])obj).Length == 0;
+
+ return false;
+ }
+
+ public override int GetHashCode()
+ {
+ return _value.GetHashCode();
+ }
+
+ #endregion
+
+ #region Conversion
+
+ public static implicit operator EmptyType(byte o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(string o) { return ConvertFrom(o); }
+
+ public static implicit operator byte(EmptyType o) { return ConvertTo<byte>(o); }
+ public static implicit operator string(EmptyType o) { return ConvertTo<string>(o); }
+
+ private static T ConvertTo<T>(EmptyType type)
+ {
+ if (type == null)
+ return default(T);
+
+ return type.GetValue<T>();
+ }
+
+ private static EmptyType ConvertFrom(object o)
+ {
+ var type = new EmptyType();
+ type.SetValue(o);
+ return type;
+ }
+
+ #endregion
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.