コード例 #1
0
        public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options)
        {
            if (value == null)
            {
                dictionary.Add(fullPropertyName, null);
                return;
            }

            if (valueType.IsGenericType)
            {
                var valueGenericType = valueType.GetGenericArguments().First();

                var formatter = options.Formatters
                    .Where(x => x.GetType() != typeof(NullableFormatter))
                    .Where(x => x.IsMatch(valueGenericType, options))
                    .FirstOrDefault();

                if (formatter == null)
                {
                    dictionary.Add(fullPropertyName, value);
                }
                else
                {
                    formatter.Invoke(fullPropertyName, value, valueType, dictionary, options);
                }
            }
        }
コード例 #2
0
        protected virtual bool IsMatch(Type matchingType, Type currentType, SupurlativeOptions options)
        {
            if (matchingType == currentType)
                return true;

            return false;
        }
コード例 #3
0
 public PagingGenerator(
     HttpRequestMessage requestMessage,
     SupurlativeOptions options = null,
     string pagePropertyName    = "page")
     : base(requestMessage, options)
 {
     DefaultPagePropertyName = pagePropertyName;
 }
コード例 #4
0
        public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options)
        {
            var valueDictionary = value as Dictionary<string, string>;

            foreach (var kvp in valueDictionary)
            {
                dictionary.Add(kvp.Key, kvp.Value);
            }
        }
コード例 #5
0
ファイル: UrlGenerator.cs プロジェクト: ritterim/Supurlative
        public UrlGenerator(
            HttpRequestMessage requestMessage,
            SupurlativeOptions options = null)
        {
            if (requestMessage == null) throw new ArgumentNullException("requestMessage");

            _requestMessage = requestMessage;

            Options = options ?? SupurlativeOptions.Defaults;
            Options.Validate();
        }
コード例 #6
0
ファイル: TestHelper.cs プロジェクト: ritterim/Supurlative
 public static UrlGenerator CreateAUrlGenerator(
     string baseUrl,
     string routeName,
     string routeTemplate,
     object routeDefaults = null,
     object routeConstraints = null,
     SupurlativeOptions supurlativeOptions = null
     )
 {
     HttpRequestMessage request;
     request = CreateAHttpRequestMessage(baseUrl, routeName, routeTemplate, routeDefaults, routeConstraints);
     return new UrlGenerator(request, supurlativeOptions);
 }
コード例 #7
0
ファイル: TestHelper.cs プロジェクト: tgharold/Supurlative
        public static Generator CreateAGenerator(
            string baseUrl,
            string routeName,
            string routeTemplate,
            object routeDefaults    = null,
            object routeConstraints = null,
            SupurlativeOptions supurlativeOptions = null
            )
        {
            HttpRequestMessage request;

            request = CreateAHttpRequestMessage(baseUrl, routeName, routeTemplate, routeDefaults, routeConstraints);
            return(new Generator(request, supurlativeOptions));
        }
コード例 #8
0
        public void Should_throw_formatter_exception_if_formatter_throws_exception_during_generation()
        {
            const string       routeName     = "test";
            const string       routeTemplate = "test";
            SupurlativeOptions options       = SupurlativeOptions.Defaults;

            options.AddFormatter <DummyFormatter>();

            HttpRequestMessage request;

            request = TestHelper.CreateAHttpRequestMessage(_baseUrl, routeName, routeTemplate);

            var generator = new Generator(request, options);

            var exception = Assert.Throws <FormatterException>(
                () => generator.Generate("test", new { Id = 1 }));

            Assert.Equal(
                "There is a problem invoking the formatter: RimDev.Supurlative.Tests.DummyFormatter.",
                exception.Message);
        }
コード例 #9
0
 public abstract void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options);
コード例 #10
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return(true);
 }
コード例 #11
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options)
 {
     throw new NotImplementedException();
 }
コード例 #12
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return(IsMatch(typeof(Dictionary <string, object>), currentType, options));
 }
コード例 #13
0
        public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options)
        {
            var valueDictionary = value as Dictionary <string, string>;

            foreach (var kvp in valueDictionary)
            {
                dictionary.Add(kvp.Key, kvp.Value);
            }
        }
コード例 #14
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return(IsMatch(typeof(LocationRequest.Coordinate), currentType, options));
 }
コード例 #15
0
        public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options)
        {
            var coordinates = value as LocationRequest.Coordinate;

            dictionary.Add(fullPropertyName, coordinates == null ? null : coordinates.ToString());
        }
コード例 #16
0
ファイル: Extensions.cs プロジェクト: ritterim/Supurlative
        internal static IDictionary<string, object> TraverseForKeys(
            this object target,
            SupurlativeOptions options,
            string parentKey = null)
        {
            var kvp = new Dictionary<string, object>();

            if (target == null)
                return kvp;

            var valueType = target as Type == null
                ? target.GetType()
                : target as Type;

            var properties = valueType
                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(x => x.CanWrite || valueType.CheckIfAnonymousType());

            foreach (var property in properties)
            {
                var fullPropertyName = parentKey == null
                        ? property.Name
                        : string.Format("{0}{1}{2}", parentKey, options.PropertyNameSeperator, property.Name);

                object valueOrPropertyType = property.PropertyType;

                if (target as Type == null)
                {
                    valueOrPropertyType = property.GetValue(target, null)
                        ?? property.PropertyType;
                }

                var formatterAttribute = property.GetCustomAttributes()
                    .Where(x => typeof(BaseFormatterAttribute).IsAssignableFrom(x.GetType()))
                    .Cast<BaseFormatterAttribute>()
                    .FirstOrDefault();

                if (formatterAttribute == null)
                {
                    // find any global formatters
                    formatterAttribute =
                        options
                        .Formatters
                        .Where(x => x.IsMatch(property.PropertyType, options))
                        .FirstOrDefault();
                }

                if (formatterAttribute != null)
                {
                    var targetValue = target == null || target as Type != null
                        ? null
                        : property.GetValue(target, null);

                    try
                    {
                        formatterAttribute.Invoke(
                            fullPropertyName,
                            targetValue,
                            property.PropertyType,
                            kvp,
                            options);
                    }
                    catch (Exception ex)
                    {
                        throw new FormatterException(
                            string.Format("There is a problem invoking the formatter: {0}.", formatterAttribute.GetType().FullName),
                            ex);
                    }
                }
                else
                {
                    var kvpValue = (valueOrPropertyType != null && valueOrPropertyType as Type == null
                            ? valueOrPropertyType.ToString()
                            : null);

                    if (property.PropertyType.IsPrimitive
                        || (!string.IsNullOrEmpty(property.PropertyType.Namespace)
                        && property.PropertyType.Namespace.StartsWith("System")))
                    {
                        kvp.Add(fullPropertyName, kvpValue);
                    }
                    else
                    {
                        var results = TraverseForKeys(valueOrPropertyType, options, fullPropertyName);

                        if (results.Count() == 0)
                        {
                            kvp.Add(fullPropertyName, kvpValue);
                        }
                        else
                        {
                            foreach (var result in results)
                            {
                                kvp.Add(result.Key, result.Value);
                            }
                        }
                    }
                }
            }

            return kvp.ToDictionary(
                x => options.LowercaseKeys ? x.Key.ToLower() : x.Key,
                x => x.Value
            );
        }
コード例 #17
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options)
 {
     return;
 }
コード例 #18
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options)
 {
     var coordinates = value as LocationRequest.Coordinate;
     dictionary.Add(fullPropertyName, coordinates == null ? null : coordinates.ToString());
 }
コード例 #19
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return true;
 }
コード例 #20
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options)
 {
     throw new NotImplementedException();
 }
コード例 #21
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return IsMatch(typeof(Dictionary<string, object>), currentType, options);
 }
コード例 #22
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return IsMatch(typeof(LocationRequest.Coordinate), currentType, options);
 }
コード例 #23
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return IsMatch(_type, currentType, options);
 }
コード例 #24
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return currentType.IsGenericType && currentType.GetGenericTypeDefinition() == typeof(Nullable<>);
 }
コード例 #25
0
 public abstract bool IsMatch(Type currentType, SupurlativeOptions options);
コード例 #26
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary<string, object> dictionary, SupurlativeOptions options)
 {
     dictionary.Add(fullPropertyName, _func(value));
 }