<commit_before>﻿#region (c) 2010 Lokad Open Source - New BSD License 

// Copyright (c) Lokad 2010, http://www.lokad.com
// This code is released as Open Source under the terms of the New BSD Licence

#endregion

using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using ProtoBuf;

namespace Lokad.Cqrs.ProtoBuf
{
	public static class ProtoBufUtil
	{
		public static string GetContractReference(Type type)
		{
			var attribs = type.GetCustomAttributes(false);
			var helper = new AttributeHelper(attribs);


			var name = Maybe.String
				.GetValue(() => helper.GetString<ProtoContractAttribute>(p => p.Name))
				.GetValue(() => helper.GetString<DataContractAttribute>(p => p.Name))
				.GetValue(() => helper.GetString<XmlTypeAttribute>(p => p.TypeName))
				.GetValue(() => type.Name);

			var ns = Maybe.String
				.GetValue(() => helper.GetString<DataContractAttribute>(p => p.Namespace))
				.GetValue(() => helper.GetString<XmlTypeAttribute>(p => p.Namespace))
				.Convert(s => s.Trim() + "/", "");

			return ns + name;
		}


		public static IFormatter CreateFormatter(Type type)
		{
			try
			{
				typeof (Serializer)
					.GetMethod("PrepareSerializer")
					.MakeGenericMethod(type)
					.Invoke(null, null);

				return (IFormatter) typeof (Serializer)
					.GetMethod("CreateFormatter")
					.MakeGenericMethod(type)
					.Invoke(null, null);
			}
			catch (TargetInvocationException tie)
			{
				throw Throw.InnerExceptionWhilePreservingStackTrace(tie);
			}
		}
	}
}<commit_msg>Framework: Better messages, when ProtoBuf serializer fails to prepare.<commit_after>﻿#region (c) 2010 Lokad Open Source - New BSD License 

// Copyright (c) Lokad 2010, http://www.lokad.com
// This code is released as Open Source under the terms of the New BSD Licence

#endregion

using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using ProtoBuf;

namespace Lokad.Cqrs.ProtoBuf
{
	public static class ProtoBufUtil
	{
		public static string GetContractReference(Type type)
		{
			var attribs = type.GetCustomAttributes(false);
			var helper = new AttributeHelper(attribs);


			var name = Maybe.String
				.GetValue(() => helper.GetString<ProtoContractAttribute>(p => p.Name))
				.GetValue(() => helper.GetString<DataContractAttribute>(p => p.Name))
				.GetValue(() => helper.GetString<XmlTypeAttribute>(p => p.TypeName))
				.GetValue(() => type.Name);

			var ns = Maybe.String
				.GetValue(() => helper.GetString<DataContractAttribute>(p => p.Namespace))
				.GetValue(() => helper.GetString<XmlTypeAttribute>(p => p.Namespace))
				.Convert(s => s.Trim() + "/", "");

			return ns + name;
		}


		public static IFormatter CreateFormatter(Type type)
		{
			try
			{
				typeof (Serializer)
					.GetMethod("PrepareSerializer")
					.MakeGenericMethod(type)
					.Invoke(null, null);
			}
			catch (TargetInvocationException tie)
			{
				var message = string.Format("Failed to prepare ProtoBuf serializer for '{0}'.", type);
				throw new InvalidOperationException(message, tie.InnerException);
			}

			try
			{
				return (IFormatter) typeof (Serializer)
					.GetMethod("CreateFormatter")
					.MakeGenericMethod(type)
					.Invoke(null, null);
			}
			catch (TargetInvocationException tie)
			{
				var message = string.Format("Failed to create ProtoBuf formatter for '{0}'.", type);
				throw new InvalidOperationException(message, tie.InnerException);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12769010043041606, 'fraction_numerical': 0.005164992826398852, 'mean_word_length': 3.8215767634854774, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '821452', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1043, 'n_words': 260}