Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaned up some code releated to ReversedType

  • Loading branch information...
commit 4ec2dd15aeb458285fd9db8b4aa888a51e837f07 1 parent f5700b6
@nberardi nberardi authored
View
1  src/FluentCassandra.csproj
@@ -280,6 +280,7 @@
<Compile Include="Types\LexicalUUIDTypeConverter.cs" />
<Compile Include="Types\LongTypeConverter.cs" />
<Compile Include="Types\NullType.cs" />
+ <Compile Include="Types\ReversedType.cs" />
<Compile Include="Types\TimeUUIDTypeConverter.cs" />
<Compile Include="Types\UTF8TypeConverter.cs" />
<Compile Include="Types\LexicalUUIDType.cs" />
View
81 src/Types/CassandraType.cs
@@ -27,8 +27,11 @@ public sealed class CassandraType
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");
+ private const string _ReversedType = "org.apache.cassandra.db.marshal.ReversedType";
+
private readonly string _dbType;
private Type _type;
+ private bool? _dbTypeReversed;
private IList<CassandraType> _compositeTypes;
private IDictionary<char, CassandraType> _dynamicCompositeType;
@@ -63,6 +66,17 @@ public CassandraObject CreateInstance()
public string DatabaseType { get { return _dbType; } }
+ public bool Reversed
+ {
+ get
+ {
+ if (_dbTypeReversed == null)
+ Parse();
+
+ return _dbTypeReversed.Value;
+ }
+ }
+
public Type FluentType
{
get
@@ -74,44 +88,37 @@ public Type FluentType
}
}
- private void Parse()
+ private void Parse()
{
- // Pay attention to any ReversedType on the Column Index
- const string reversedTypeString = "ReversedType(";
- var reversedIndex = _dbType.IndexOf(reversedTypeString, StringComparison.Ordinal);
- var workingtype = _dbType;
- if (reversedIndex > 0)
- {
- workingtype = workingtype.Substring(reversedIndex + reversedTypeString.Length);
- var lastClosing = workingtype.LastIndexOf(")", StringComparison.Ordinal);
- workingtype = workingtype.Substring(0, lastClosing).TrimEnd('.');
- }
-
- var compositeStart = workingtype.IndexOf('(');
+ _dbTypeReversed = false;
+ int compositeStart = _dbType.IndexOf('(');
// check for composite type
if (compositeStart == -1) {
- _type = Parse(workingtype);
+ _type = Parse(_dbType);
return;
}
- var part1 = workingtype.Substring(0, compositeStart);
- var part2 = workingtype.Substring(compositeStart);
+ var part1 = _dbType.Substring(0, compositeStart);
+ var part2 = _dbType.Substring(compositeStart);
_type = Parse(part1);
if (_type == typeof(CompositeType))
- {
ParseCompositeType(part2);
- }
else if (_type == typeof(DynamicCompositeType))
- {
ParseDynamicCompositeType(part2);
- }
+ else if (_type == typeof(ReversedType))
+ ParseReversedType(part2);
else
- {
throw new CassandraException("Type '" + _dbType + "' not found.");
- }
+ }
+
+ private void ParseReversedType(string part)
+ {
+ part = part.Trim('(', ')');
+ _dbTypeReversed = true;
+ _type = Parse(part);
}
private void ParseCompositeType(string part)
@@ -145,7 +152,7 @@ private void ParseDynamicCompositeType(string part)
}
}
- private Type Parse(string dbType)
+ private Type Parse(string dbType)
{
Type type;
@@ -168,7 +175,8 @@ private Type Parse(string dbType)
case "compositetype": type = typeof(CompositeType); break;
case "dynamiccompositetype": type = typeof(DynamicCompositeType); break;
case "countercolumntype": type = typeof(CounterColumnType); break;
- default: throw new CassandraException("Type '" + _dbType + "' not found.");
+ case "reversedtype": type = typeof(ReversedType); break;
+ default: throw new CassandraException("Type '" + _dbType + "' not found.");
}
return type;
@@ -201,6 +209,17 @@ public static CassandraType DynamicCompositeType(IDictionary<char, CassandraType
return new CassandraType(sb.ToString());
}
+ public static CassandraType ReversedType(CassandraType baseType)
+ {
+ var sb = new StringBuilder();
+ sb.Append(_ReversedType);
+ sb.Append("(");
+ sb.Append(baseType.DatabaseType);
+ sb.Append(")");
+
+ return new CassandraType(sb.ToString());
+ }
+
public static CassandraType GetCassandraType(Type sourceType)
{
if (sourceType.BaseType == typeof(CassandraObject))
@@ -280,19 +299,5 @@ public static CassandraType GetCassandraType(Type sourceType)
{
return GetCassandraType(type);
}
-
- public static CassandraType Reversed(CassandraType baseType)
- {
- var underlyingType = baseType.DatabaseType;
- // Add the Reversed keyword
- var lastDot = underlyingType.LastIndexOf('.');
- underlyingType = underlyingType.Insert(lastDot + 1, "ReversedType(");
- underlyingType = underlyingType + ")";
-
- var result = new CassandraType(underlyingType);
- result.Parse();
-
- return result;
- }
}
}
View
38 src/Types/ReversedType.cs
@@ -0,0 +1,38 @@
+using System;
+using System.Linq;
+
+namespace FluentCassandra.Types
+{
+ public class ReversedType : CassandraObject
+ {
+ public override void SetValue(object obj)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override object GetValueInternal(Type type)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override object GetRawValue()
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override TypeCode TypeCode
+ {
+ get { throw new NotImplementedException(); }
+ }
+
+ public override byte[] ToBigEndian()
+ {
+ throw new NotImplementedException();
+ }
+
+ public override void SetValueFromBigEndian(byte[] value)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
50 test/FluentCassandra.Tests/Types/CassandraTypeTest.cs
@@ -43,21 +43,21 @@ public void Parse_Type()
Assert.AreEqual(expected, actual);
}
- [Test]
- public void Parse_Reverse_Type()
- {
- // arrange
- Type expected = typeof(LongType);
- string cassandraString = "org.apache.cassandra.db.marshal.ReversedType(LongType)";
-
- // act
- Type actual = new CassandraType(cassandraString).FluentType;
-
- // assert
- Assert.AreEqual(expected, actual);
- }
-
- [Test, ExpectedException(typeof(CassandraException))]
+ [Test]
+ public void Parse_Reversed_Type()
+ {
+ // arrange
+ Type expected = typeof(LongType);
+ string cassandraString = "org.apache.cassandra.db.marshal.ReversedType(LongType)";
+
+ // act
+ Type actual = new CassandraType(cassandraString).FluentType;
+
+ // assert
+ Assert.AreEqual(expected, actual);
+ }
+
+ [Test, ExpectedException(typeof(CassandraException))]
public void Parse_CompositeType_UnknownInnerType()
{
// arranage
@@ -88,17 +88,17 @@ public void Parse_UnknownTypeWithParams()
Type actual = new CassandraType(cassandraString).FluentType;
}
- [Test]
- public void ReversedTypeHelper()
- {
- // arrange
- Type expected = typeof (AsciiType);
+ [Test]
+ public void ReversedTypeHelper()
+ {
+ // arrange
+ var expected = "org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.UTF8Type)";
- // act
- var actual = CassandraType.Reversed(CassandraType.AsciiType).FluentType;
+ // act
+ var actual = CassandraType.ReversedType(CassandraType.UTF8Type).DatabaseType;
- // assert
- Assert.AreEqual(expected, actual);
- }
+ // assert
+ Assert.AreEqual(expected, actual);
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.