示例#1
0
        protected virtual bool IsMatch(Type matchingType, Type currentType, SupurlativeOptions options)
        {
            if (matchingType == currentType)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        public TemplateGenerator(HttpRequestMessage httpRequestMessage, SupurlativeOptions options = null)
        {
            if (httpRequestMessage == null)
            {
                throw new ArgumentNullException("httpRequestMessage");
            }

            Options = options ?? SupurlativeOptions.Defaults;
            Options.Validate();

            _httpRequestMessage = CloneHttpRequestMessageWithoutConstraints(httpRequestMessage);
        }
示例#3
0
        public UrlGenerator(
            HttpRequestMessage requestMessage,
            SupurlativeOptions options = null)
        {
            if (requestMessage == null)
            {
                throw new ArgumentNullException("requestMessage");
            }

            _requestMessage = requestMessage;

            Options = options ?? SupurlativeOptions.Defaults;
            Options.Validate();
        }
示例#4
0
        public Generator(
            HttpRequestMessage requestMessage,
            SupurlativeOptions options = null)
        {
            if (requestMessage == null)
            {
                throw new ArgumentNullException("requestMessage");
            }

            var generatorOptions = options ?? SupurlativeOptions.Defaults;

            generatorOptions.Validate();

            Options = generatorOptions;

            _templateGenerator = new TemplateGenerator(requestMessage, generatorOptions);
            _urlGenerator      = new UrlGenerator(requestMessage, generatorOptions);
        }
示例#5
0
        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
                       ));
        }
示例#6
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return(IsMatch(_type, currentType, options));
 }
示例#7
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options)
 {
     dictionary.Add(fullPropertyName, _func(value));
 }
示例#8
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);
                }
            }
        }
示例#9
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return(currentType.IsGenericType && currentType.GetGenericTypeDefinition() == typeof(Nullable <>));
 }
示例#10
0
 public override void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options)
 {
     return;
 }
示例#11
0
 public override bool IsMatch(Type currentType, SupurlativeOptions options)
 {
     return(false);
 }
示例#12
0
 public abstract bool IsMatch(Type currentType, SupurlativeOptions options);
示例#13
0
 public abstract void Invoke(string fullPropertyName, object value, Type valueType, IDictionary <string, object> dictionary, SupurlativeOptions options);