Skip to content

Commit

Permalink
Minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
SolalPirelli committed Aug 23, 2016
1 parent d68f175 commit 6f0752b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ThriftSharp.Tests/AttributesParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module ``Parsing: Converters`` =

[<Fact>]
let ``Converter not implementing IThriftValueConverter<,> is not allowed``() =
throws<string>("The type 'String' does not IThriftValueConverter<TFrom, TTo>")
throws<string>("The type 'String' does not implement IThriftValueConverter<TFrom, TTo>")


type BadConverter(notAParameterlessCtor: obj) =
Expand Down
32 changes: 19 additions & 13 deletions ThriftSharp/Internals/ThriftAttributesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ThriftSharp.Internals
internal static class ThriftAttributesParser
{
private static readonly Dictionary<TypeInfo, ThriftStruct> _knownStructs = new Dictionary<TypeInfo, ThriftStruct>();
private static readonly Dictionary<TypeInfo, ThriftService> _knownServices = new Dictionary<TypeInfo, ThriftService>();

/// <summary>
/// Parses a Thrift field from the specified PropertyInfo.
Expand Down Expand Up @@ -183,24 +184,29 @@ private static ThriftMethod ParseMethod( MethodInfo methodInfo )
/// </summary>
public static ThriftService ParseService( TypeInfo typeInfo )
{
if( !typeInfo.IsInterface )
if( !_knownServices.ContainsKey( typeInfo ) )
{
throw ThriftParsingException.NotAService( typeInfo );
}
if( !typeInfo.IsInterface )
{
throw ThriftParsingException.NotAService( typeInfo );
}

var attr = typeInfo.GetCustomAttribute<ThriftServiceAttribute>();
if( attr == null )
{
throw ThriftParsingException.ServiceWithoutAttribute( typeInfo );
}
var attr = typeInfo.GetCustomAttribute<ThriftServiceAttribute>();
if( attr == null )
{
throw ThriftParsingException.ServiceWithoutAttribute( typeInfo );
}

var methods = typeInfo.DeclaredMethods.ToDictionary( m => m.Name, m => ParseMethod( m ) );
if( methods.Count == 0 )
{
throw ThriftParsingException.NoMethods( typeInfo );
var methods = typeInfo.DeclaredMethods.ToDictionary( m => m.Name, m => ParseMethod( m ) );
if( methods.Count == 0 )
{
throw ThriftParsingException.NoMethods( typeInfo );
}

_knownServices.Add( typeInfo, new ThriftService( attr.Name, methods ) );
}

return new ThriftService( attr.Name, methods );
return _knownServices[typeInfo];
}
}
}
5 changes: 3 additions & 2 deletions ThriftSharp/Internals/ThriftConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace ThriftSharp.Internals
{
/// <summary>
/// Thrift converter, including commonly-needed properties.
/// Thrift converter.
/// </summary>
internal sealed class ThriftConverter
{
Expand All @@ -34,7 +34,7 @@ public ThriftConverter( Type type )
var ifaces = typeInfo.GetGenericInterfaces( typeof( IThriftValueConverter<,> ) );
if( ifaces.Length == 0 )
{
throw new ArgumentException( $"The type '{type.Name}' does not IThriftValueConverter<TFrom, TTo>." );
throw new ArgumentException( $"The type '{type.Name}' does not implement IThriftValueConverter<TFrom, TTo>." );
}
if( ifaces.Length > 1 )
{
Expand All @@ -57,6 +57,7 @@ public ThriftConverter( Type type )
/// </summary>
public Expression CreateCall( string methodName, Expression arg )
{
// TODO Use only 1 converter of each type.
return Expression.Call(
Expression.New( _type ),
_interfaceTypeInfo.GetDeclaredMethod( methodName ),
Expand Down

0 comments on commit 6f0752b

Please sign in to comment.