Esempio n. 1
0
        public override MapResult Map <T>(ValueMapperContext context)
        {
            try
            {
                var type   = context.Type.ElementType ?? context.Type;
                var result = context.Values.Select(x => (T)(x is string
                                                            ?x.ToString().ParseSimpleType(type) : x));

                var value = context.Type.IsArray
                    ? result.ToArray()
                    : (context.Type.IsGenericListCastable
                        ? (object)result.ToList()
                        : result.FirstOrDefault());

                return(MapResult.Success(value));
            }
            catch (FormatException exception)
            {
                var message = $"{context.Values.Select(x => $"'{x}'").Join(",")} " +
                              $"is not formatted correctly. {exception.Message}";

                return(MapResult.Failure(context.Parameter != null
                    ? $"Parameter {context.Parameter.Name} value {message}"
                    : $"Value {message}"));
            }
        }
Esempio n. 2
0
        public static MapResult Map(this IEnumerable <IValueMapper> mappers,
                                    object[] values, PropertyDescriptor property, RequestContext requestContext,
                                    Configuration configuration)
        {
            var mapperContext = new ValueMapperContext(configuration, requestContext, property, values);
            var mapper        = configuration.ValueMappers.ThatApplyTo(
                mappers, mapperContext).FirstOrDefault();

            return(mapper == null?MapResult.NotMapped() : MapResult.WasMapped(mapper.Map(mapperContext)));
        }
Esempio n. 3
0
        public static MapResult Map(this IEnumerable <IValueMapper> mappers,
                                    ActionMethod actionMethod, RouteDescriptor route,
                                    ActionParameter parameter, object[] values,
                                    Configuration configuration, HttpConfiguration httpConfiguration)
        {
            var mapperContext = new ValueMapperContext(parameter, values);
            var mapperConfigurationcontext = new ValueMapperConfigurationContext(
                configuration, httpConfiguration, actionMethod, route, parameter, values);
            var mapper = configuration.ValueMappers
                         .FirstThatAppliesToOrDefault(mappers, mapperConfigurationcontext, mapperContext);

            return(mapper == null
                ? MapResult.NoMapper()
                : mapper.Map(mapperContext));
        }
Esempio n. 4
0
        public override object Map <T>(ValueMapperContext context)
        {
            try
            {
                var type   = context.Type.ElementType ?? context.Type;
                var result = context.Values.Select(x => (T)(x is string
                                                            ?x.ToString().ParseSimpleType(type) : x));

                return(context.Type.IsArray
                    ? result.ToArray()
                    : (context.Type.IsGenericListCastable
                        ? (object)result.ToList()
                        : result.FirstOrDefault()));
            }
            catch (FormatException exception)
            {
                throw new BadRequestException($@"Parameter {context.Parameter.Name} value {context.Values
                    .Select(x => $"'{x}'").Join(",")} is not formatted correctly.", exception);
            }
        }
Esempio n. 5
0
 public MapResult Map(ValueMapperContext context)
 {
     return(Mappers(context.Type.ElementType?.Type ?? context.Type.Type)(this, context));
 }
Esempio n. 6
0
 public abstract MapResult Map <T>(ValueMapperContext context);
Esempio n. 7
0
 public abstract bool AppliesTo(ValueMapperContext context);
Esempio n. 8
0
 public override bool AppliesTo(ValueMapperContext context)
 {
     return((context.Type.ElementType ?? context.Type).Type.IsSimpleType());
 }
Esempio n. 9
0
 public abstract object Map <T>(ValueMapperContext context);