Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

a better implimentation of #113

  • Loading branch information...
commit c210cdda6c993b5e0a6c0d0fda907f2e52526196 1 parent b5d5032
@nberardi nberardi authored
View
2  src/Types/CassandraObjectConverter.cs
@@ -4,7 +4,7 @@
namespace FluentCassandra.Types
{
- internal abstract class CassandraObjectConverter<T>
+ internal abstract class CassandraObjectConverter<T>
{
public abstract bool CanConvertFrom(Type sourceType);
View
108 src/Types/CassandraType.cs
@@ -94,18 +94,23 @@ public Type FluentType
private void Init()
{
_typeReversed = false;
- int compositeStart = _dbType.IndexOf('(');
+ Parse(_dbType);
+ }
+
+ private void Parse(string dbType)
+ {
+ int compositeStart = dbType.IndexOf('(');
// check for composite type
if (compositeStart == -1) {
- _type = Parse(_dbType);
+ _type = GetSystemType(dbType);
return;
}
- var part1 = _dbType.Substring(0, compositeStart);
- var part2 = _dbType.Substring(compositeStart);
+ var part1 = dbType.Substring(0, compositeStart);
+ var part2 = dbType.Substring(compositeStart);
- _type = Parse(part1);
+ _type = GetSystemType(part1);
if (_type == typeof(CompositeType))
ParseCompositeType(part2);
@@ -114,14 +119,14 @@ private void Init()
else if (_type == typeof(ReversedType))
ParseReversedType(part2);
else
- throw new CassandraException("Type '" + _dbType + "' not found.");
+ throw new CassandraException("Type '" + dbType + "' not found.");
}
private void ParseReversedType(string part)
{
part = part.Trim('(', ')');
_typeReversed = true;
- _type = Parse(part);
+ Parse(part);
}
private void ParseCompositeType(string part)
@@ -131,24 +136,7 @@ private void ParseCompositeType(string part)
_compositeTypes = new List<CassandraType>();
foreach (var p in parts)
- {
- int reversedStart = p.IndexOf('(');
-
- // check for reversed type
- if (reversedStart == -1)
- {
- _compositeTypes.Add(Parse(p));
- }
- else
- {
- var part1 = p.Substring(0, reversedStart);
-
- if (Parse(part1) == typeof(ReversedType))
- _compositeTypes.Add(new CassandraType(p));
- else
- throw new CassandraException("Type '" + part1 + "' not found.");
- }
- }
+ _compositeTypes.Add(GetSystemType(p));
}
private void ParseDynamicCompositeType(string part)
@@ -168,40 +156,8 @@ private void ParseDynamicCompositeType(string part)
throw new CassandraException("Expecting operator '=>' after the alias");
string type = p.Substring(3);
- _dynamicCompositeType.Add(alias, Parse(type));
- }
- }
-
- private Type Parse(string dbType)
- {
- Type type;
-
- switch (dbType.Substring(dbType.LastIndexOf('.') + 1).ToLower())
- {
- case "asciitype": type = typeof(AsciiType); break;
- case "booleantype": type = typeof(BooleanType); break;
- case "bytestype": type = typeof(BytesType); break;
- case "datetype": type = typeof(DateType); break;
- case "decimaltype": type = typeof(DecimalType); break;
- case "doubletype": type = typeof(DoubleType); break;
- case "floattype": type = typeof(FloatType); break;
- case "int32type": type = typeof(Int32Type); break;
- case "integertype": type = typeof(IntegerType); break;
- case "lexicaluuidtype": type = typeof(LexicalUUIDType); break;
- case "longtype": type = typeof(LongType); break;
- case "timeuuidtype": type = typeof(TimeUUIDType); break;
- case "utf8type": type = typeof(UTF8Type); break;
- case "uuidtype": type = typeof(UUIDType); break;
- case "compositetype": type = typeof(CompositeType); break;
- case "dynamiccompositetype": type = typeof(DynamicCompositeType); break;
- case "countercolumntype": type = typeof(CounterColumnType); break;
- case "reversedtype": type = typeof(ReversedType); break;
- case "emptytype": type = typeof(EmptyType); break;
- case "inetaddresstype": type = typeof(InetAddressType); break;
- default: throw new CassandraException("Type '" + _dbType + "' not found.");
+ _dynamicCompositeType.Add(alias, GetSystemType(type));
}
-
- return type;
}
public override string ToString()
@@ -367,6 +323,42 @@ public static CassandraType GetCassandraType(string type)
return new CassandraType(type);
}
+ public static Type GetSystemType(CassandraType baseType)
+ {
+ return GetSystemType(baseType.DatabaseType);
+ }
+
+ public static Type GetSystemType(string dbType)
+ {
+ Type type;
+
+ switch (dbType.Substring(dbType.LastIndexOf('.') + 1).ToLower()) {
+ case "asciitype": type = typeof(AsciiType); break;
+ case "booleantype": type = typeof(BooleanType); break;
+ case "bytestype": type = typeof(BytesType); break;
+ case "datetype": type = typeof(DateType); break;
+ case "decimaltype": type = typeof(DecimalType); break;
+ case "doubletype": type = typeof(DoubleType); break;
+ case "floattype": type = typeof(FloatType); break;
+ case "int32type": type = typeof(Int32Type); break;
+ case "integertype": type = typeof(IntegerType); break;
+ case "lexicaluuidtype": type = typeof(LexicalUUIDType); break;
+ case "longtype": type = typeof(LongType); break;
+ case "timeuuidtype": type = typeof(TimeUUIDType); break;
+ case "utf8type": type = typeof(UTF8Type); break;
+ case "uuidtype": type = typeof(UUIDType); break;
+ case "compositetype": type = typeof(CompositeType); break;
+ case "dynamiccompositetype": type = typeof(DynamicCompositeType); break;
+ case "countercolumntype": type = typeof(CounterColumnType); break;
+ case "reversedtype": type = typeof(ReversedType); break;
+ case "emptytype": type = typeof(EmptyType); break;
+ case "inetaddresstype": type = typeof(InetAddressType); break;
+ default: throw new CassandraException("Type '" + dbType + "' not found.");
+ }
+
+ return type;
+ }
+
public static implicit operator CassandraType(Type type)
{
return GetCassandraType(type);
View
2  src/Types/CompositeType.cs
@@ -79,7 +79,7 @@ protected override object GetValueInternal(Type type)
public override void SetValue(object obj)
{
if (obj != null && obj.GetType().GetInterfaces().Contains(typeof(IEnumerable<CassandraObject>)))
- ComponentTypeHints = ((IEnumerable<CassandraObject>)obj).Select(t => new CassandraType(t.GetType().Name)).ToList();
+ ComponentTypeHints = ((IEnumerable<CassandraObject>)obj).Select(t => t.GetCassandraType()).ToList();
_value = Converter.ConvertFrom(obj);
}
View
14 test/FluentCassandra.Tests/Types/CassandraTypeTest.cs
@@ -58,6 +58,20 @@ public void Parse_Reversed_Type()
}
[Fact]
+ public void Parse_Reversed_Type_With_Complex_Value()
+ {
+ // arrange
+ Type expected = typeof(CompositeType);
+ string cassandraString = "org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.LongType,org.apache.cassandra.db.marshal.AsciiType))";
+
+ // act
+ Type actual = new CassandraType(cassandraString).FluentType;
+
+ // assert
+ Assert.Equal(expected, actual);
+ }
+
+ [Fact]
public void Parse_CompositeType_UnknownInnerType()
{
// arrange
Please sign in to comment.
Something went wrong with that request. Please try again.