コード例 #1
0
 protected override IDbDataParameter CreateParameter(IDbCommand command, string parameterName, Type type, object value)
 {
     if (type.GetUnwrappedNullableType().IsEnum)
     {
         return new NpgsqlParameter(parameterName, NpgsqlDbType.Unknown) { Value = value };
     }
     else if (type.GetUnwrappedNullableType() == typeof(TimeSpan))
     {
         return new NpgsqlParameter(parameterName, NpgsqlDbType.Interval) { Value = value };
     }
     else
     {
         return base.CreateParameter(command, parameterName, type, value);
     }
 }
コード例 #2
0
		public override SqlDataType GetSqlDataType(Type type)
		{
			SqlDataType value;

			if (this.sqlDataTypesByType.TryGetValue(type, out value))
			{
				return value;
			}

			var underlyingType = type.GetUnwrappedNullableType();
			
			if (underlyingType.IsEnum)
			{
				var sqlDataType = this.GetEnumDataType(type);

				this.sqlDataTypesByType[type] = sqlDataType;

				return sqlDataType;
			}
			else if (underlyingType.IsArray && underlyingType == typeof(byte[]))
			{
				var sqlDataType = this.GetBlobDataType();

				this.sqlDataTypesByType[type] = sqlDataType;

				return sqlDataType;
			}

			return null;
		}
コード例 #3
0
ファイル: JavaBinderHelpers.cs プロジェクト: samcook/Fickle
        public static string GetValueResponseWrapperTypeName(Type type)
        {
            var unwrappedType = type.GetUnwrappedNullableType();

            if (unwrappedType is FickleListType)
            {
                return TypeSystem.GetPrimitiveName(type.GetFickleListElementType(), true) + "ListValueResponse";
            }

            return TypeSystem.GetPrimitiveName(type, true) + "ValueResponse";
        }
コード例 #4
0
ファイル: TypeSystem.cs プロジェクト: techpub/Fickle
        public static string GetPrimitiveName(Type type, bool naked = false)
        {
            if (type.GetUnwrappedNullableType().IsEnum)
            {
                if (type.GetUnderlyingType() == null || naked)
                {
                    return type.GetUnwrappedNullableType().Name;
                }
                else
                {
                    return type.GetUnwrappedNullableType().Name + "?";
                }
            }

            if (naked)
            {
                return primitiveTypeByName.FirstOrDefault(c => c.Value == type.GetUnwrappedNullableType()).Key;
            }
            else
            {
                return primitiveTypeByName.FirstOrDefault(c => c.Value == type).Key;
            }
        }
コード例 #5
0
        public static string GetValueResponseWrapperTypeName(Type type)
        {
            var unwrappedType = type.GetUnwrappedNullableType();

            if (unwrappedType.IsNumericType(true) || unwrappedType == typeof(bool) || (unwrappedType.IsEnum && type.IsNullable()))
            {
                return "NumberValueResponse";
            }
            else if (unwrappedType is FickleListType)
            {
                return "ArrayValueResponse";
            }
            else
            {
                return TypeSystem.GetPrimitiveName(unwrappedType, true) + "ValueResponse";
            }
        }
コード例 #6
0
        protected override ServiceClass CreateValueResponseServiceClass(Type type)
        {
            var unwrappedType = type.GetUnwrappedNullableType();

            string typeName;

            if (unwrappedType.IsNumericType() || unwrappedType == typeof(bool) || (unwrappedType.IsEnum && type.IsNullable()))
            {
                typeName = "int?";
            }
            else if (type is FickleListType)
            {
                typeName = "int?[]";
            }
            else
            {
                typeName = TypeSystem.GetPrimitiveName(type);
            }

            var properties = new List<ServiceProperty>
            {
                new ServiceProperty()
                {
                    Name = options.ResponseStatusPropertyName,
                    TypeName = options.ResponseStatusTypeName
                }
            };

            if (type != typeof(void))
            {
                properties.Add
                (
                    new ServiceProperty()
                    {
                        Name = "Value",
                        TypeName = typeName
                    }
                );
            }

            return new ServiceClass(ObjectiveBinderHelpers.GetValueResponseWrapperTypeName(type), null, properties);
        }
コード例 #7
0
        protected override ServiceClass CreateValueResponseServiceClass(Type type)
        {
            string typeName;

            type = type.GetUnwrappedNullableType();

            if (type is FickleListType)
            {
                var elementType = type.GetFickleListElementType();

                typeName = TypeSystem.GetPrimitiveName(elementType) + "?[]";
            }
            else
            {
                typeName = TypeSystem.GetPrimitiveName(type);
            }

            var properties = new List<ServiceProperty>
            {
                new ServiceProperty()
                {
                    Name = options.ResponseStatusPropertyName,
                    TypeName = options.ResponseStatusTypeName
                }
            };

            if (type != typeof(void))
            {
                properties.Add
                (
                    new ServiceProperty()
                    {
                        Name = "Value",
                        TypeName = typeName
                    }
                );
            }

            return new ServiceClass(JavaBinderHelpers.GetValueResponseWrapperTypeName(type), null, properties);
        }
コード例 #8
0
        private static string GetTypeName(Type type)
        {
            if (type == null)
            {
                return null;
            }

            if (TypeSystem.IsPrimitiveType(type))
            {
                if (type.GetUnwrappedNullableType().IsEnum)
                {
                    return TypeSystem.GetPrimitiveName(type);
                }
                else
                {
                    return TypeSystem.GetPrimitiveName(type).ToLower();
                }
            }

            if (type == typeof(object))
            {
                return null;
            }

            if (typeof(IEnumerable<>).IsAssignableFromIgnoreGenericParameters(type))
            {
                Type enumerableType;

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
                {
                    enumerableType = type;
                }
                else
                {
                    enumerableType = type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>));
                }

                if (enumerableType != null)
                {
                    return GetTypeName(enumerableType.GetGenericArguments()[0]) + "[]";
                }
            }

            return type.Name;
        }
コード例 #9
0
        internal static Expression GetSerializeExpression(Type valueType, Expression value, CodeGenerationOptions options, bool skipIfNull, Func<Expression, Expression> processOutputValue)
        {
            Expression expression;
            var nsNull = FickleExpression.StaticCall("NSNull", "id", "null", null);

            if (valueType.GetUnwrappedNullableType().IsEnum)
            {
                expression = value;

                if (options.SerializeEnumsAsStrings)
                {
                    expression = FickleExpression.Call(Expression.Convert(expression, valueType), typeof(string), "ToString", null);
                }

                expression = processOutputValue(expression);
            }
            else if (valueType.GetUnwrappedNullableType() == typeof(Guid))
            {
                expression = FickleExpression.Call(Expression.Convert(value, valueType), typeof(string), "ToString", null);

                expression = processOutputValue(expression);
            }
            else if (valueType.GetUnwrappedNullableType() == typeof(TimeSpan))
            {
                expression = FickleExpression.Call(value, typeof(string), "ToString", null);

                expression = processOutputValue(expression);
            }
            else if (valueType.GetUnwrappedNullableType() == typeof(DateTime))
            {
                expression = FickleExpression.Call(value, typeof(string), "ToString", null);

                expression = processOutputValue(expression);
            }
            else if (valueType is FickleListType)
            {
                var listType = valueType as FickleListType;

                if (listType.ListElementType.GetUnwrappedNullableType().IsNumericType()
                    || (listType.ListElementType.GetUnwrappedNullableType().IsEnum && !options.SerializeEnumsAsStrings))
                {
                    return processOutputValue(value);
                }
                else
                {
                    var arrayVar = FickleExpression.Variable("NSMutableArray", "array");
                    var variables = new[] { arrayVar };
                    var arrayItem = FickleExpression.Parameter(FickleType.Define("id"), "arrayItem");

                    var supportsNull = listType.ListElementType.IsNullable()
                        || !listType.ListElementType.IsValueType;

                    var forEachBody = Expression.IfThenElse
                    (
                        Expression.ReferenceEqual(arrayItem, nsNull),
                        supportsNull ? FickleExpression.Call(arrayVar, "addObject", Expression.Convert(nsNull, typeof(object))).ToStatementBlock() : Expression.Continue(Expression.Label()).ToStatementBlock(),
                        GetSerializeExpression(listType.ListElementType, arrayItem, options, true, c => FickleExpression.Call(arrayVar, "addObject", Expression.Convert(c, typeof(object))).ToStatement()).ToBlock()
                    );

                    expression = FickleExpression.Block
                    (
                        variables,
                        Expression.Assign(arrayVar, FickleExpression.New("NSMutableArray", "initWithCapacity", FickleExpression.Call(value, typeof(int), "count", null))).ToStatement(),
                        FickleExpression.ForEach(arrayItem, value, FickleExpression.Block(forEachBody)),
                        processOutputValue(Expression.Convert(arrayVar, valueType))
                    );
                }
            }
            else if (valueType.IsServiceType())
            {
                expression = processOutputValue(FickleExpression.Call(value, value.Type, "allPropertiesAsDictionary", null));
            }
            else
            {
                expression = processOutputValue(value);
            }

            if (!skipIfNull)
            {
                if (!TypeSystem.IsPrimitiveType(valueType) || valueType.IsNullable() || valueType.IsClass)
                {
                    if (value.Type == FickleType.Define("id") || value.Type == typeof(object))
                    {
                        expression = Expression.IfThen
                        (
                            Expression.And
                            (
                                Expression.ReferenceNotEqual(Expression.Convert(value, typeof(object)), Expression.Constant(null)),
                                Expression.ReferenceNotEqual(Expression.Convert(value, typeof(object)), nsNull)
                            ),
                            expression is BlockExpression ? expression : FickleExpression.Block(expression)
                        );
                    }
                    else
                    {
                        if (valueType.IsClass)
                        {
                            expression = Expression.IfThen
                            (
                                Expression.ReferenceNotEqual(Expression.Convert(value, typeof(object)), Expression.Constant(null)),
                                expression is BlockExpression ? expression : FickleExpression.Block(expression)
                            );
                        }
                        else
                        {
                            expression = Expression.IfThen
                            (
                                Expression.NotEqual(value, Expression.Constant(null, value.Type)),
                                expression is BlockExpression ? expression : FickleExpression.Block(expression)
                            );
                        }
                    }
                }
            }

            return expression;
        }
コード例 #10
0
ファイル: TypeSystem.cs プロジェクト: techpub/Fickle
 public static bool IsPrimitiveType(Type type)
 {
     return primitiveTypes.Contains(type)
            || type.GetUnwrappedNullableType().IsEnum
            || ((type is FickleType) && ((FickleType)type).IsPrimitive);
 }
コード例 #11
0
        internal static Expression GetDeserializeExpressionProcessValueDeserializer(Type valueType, Expression value, Func<Expression, Expression> processOutputValue)
        {
            Expression outputValue;
            var nsNull = FickleExpression.StaticCall("NSNull", FickleType.Define("id"), "null", null);

            if (TypeSystem.IsPrimitiveType(valueType))
            {
                ConditionalExpression ifExpression;
                var underlyingType = valueType.GetUnwrappedNullableType();

                if (underlyingType.IsNumericType() || underlyingType == typeof(bool))
                {
                    var typeToCompare = new FickleType("NSNumber");

                    if (underlyingType.IsEnum && valueType.GetUnderlyingType() == null)
                    {
                        outputValue = Expression.Convert(FickleExpression.Call(value, typeof(int), "intValue", null), valueType);
                    }
                    else
                    {
                        outputValue = Expression.Convert(value, valueType);
                    }

                    ifExpression = Expression.IfThen(Expression.TypeIs(value, typeToCompare), processOutputValue(outputValue).ToBlock());
                }
                else if (underlyingType.IsEnum)
                {
                    Expression firstIf;

                    if (valueType.IsNullable())
                    {
                        outputValue = Expression.Convert(value, valueType);

                        firstIf = Expression.IfThen(Expression.TypeIs(value, FickleType.Define("NSNumber")), processOutputValue(outputValue).ToBlock());
                    }
                    else
                    {
                        outputValue = Expression.Convert(FickleExpression.Call(value, typeof(int), "intValue", null), valueType);

                        firstIf = Expression.IfThen(Expression.TypeIs(value, FickleType.Define("NSNumber")), processOutputValue(outputValue).ToBlock());
                    }

                    var parsedValue = FickleExpression.Variable(underlyingType, "parsedValue");
                    var success = Expression.Variable(typeof(bool), "success");

                    var parameters = new[] { new FickleParameterInfo(typeof(string), "value"), new FickleParameterInfo(underlyingType, "outValue", true) };
                    var methodInfo = new FickleMethodInfo(null, typeof(bool), TypeSystem.GetPrimitiveName(underlyingType, true) + "TryParse", parameters, true);

                    if (valueType.IsNullable())
                    {
                        outputValue = Expression.Convert(Expression.Condition(success, Expression.Convert(Expression.Convert(parsedValue, valueType), FickleType.Define("id")), Expression.Convert(FickleExpression.StaticCall(FickleType.Define("NSNull"), valueType, "null", null), FickleType.Define("id"))), valueType);
                    }
                    else
                    {
                        outputValue = Expression.Convert(parsedValue, valueType);
                    }

                    var secondIf = Expression.IfThenElse
                    (
                        Expression.TypeIs(value, FickleType.Define("NSString")),
                        FickleExpression.Block
                        (
                            new ParameterExpression[]
                            {
                                parsedValue,
                                success
                            },
                            Expression.IfThen
                            (
                                Expression.Not(Expression.Assign(success, Expression.Call(null, methodInfo, Expression.Convert(value, typeof(string)), parsedValue))),
                                Expression.Assign(parsedValue, Expression.Convert(Expression.Constant(0), underlyingType)).ToStatementBlock()
                            ),
                            processOutputValue(outputValue)
                        ),
                        firstIf
                    );

                    ifExpression = secondIf;
                }
                else
                {
                    ifExpression = Expression.IfThen(Expression.TypeIs(value, typeof(string)), processOutputValue(Expression.Convert(Expression.Convert(value, typeof(object)), valueType)).ToBlock());
                }

                if (valueType.IsNullable())
                {
                    ifExpression = Expression.IfThenElse
                    (
                        Expression.Equal(value, nsNull),
                        processOutputValue(Expression.Convert(nsNull, valueType)).ToBlock(),
                        ifExpression
                    );
                }

                return ifExpression;
            }
            else if (valueType is FickleType && ((FickleType)valueType).ServiceClass != null)
            {
                outputValue = FickleExpression.New(valueType, "initWithPropertyDictionary", FickleExpression.Convert(value, "NSDictionary"));

                return Expression.IfThen(Expression.TypeIs(value, FickleType.Define("NSDictionary")), processOutputValue(outputValue).ToBlock());
            }
            else if (valueType is FickleType && ((FickleType)valueType).ServiceEnum != null)
            {
                return Expression.IfThen(Expression.TypeIs(value, FickleType.Define("NSNumber")), processOutputValue(value).ToBlock());
            }
            else if (valueType is FickleListType)
            {
                var listType = valueType as FickleListType;

                var arrayVar = FickleExpression.Variable("NSMutableArray", "array");
                var variables = new[] { arrayVar };
                var arrayItem = FickleExpression.Parameter(FickleType.Define("id"), "arrayItem");

                var forEachBody = GetDeserializeExpressionProcessValueDeserializer(listType.ListElementType, arrayItem, c => FickleExpression.Call(arrayVar, "addObject", Expression.Convert(c, typeof(object))).ToStatement());

                var ifThen = Expression.IfThen
                (
                    Expression.TypeIs(value, FickleType.Define("NSArray")),
                    FickleExpression.Block
                    (
                        variables,
                        Expression.Assign(arrayVar, FickleExpression.New("NSMutableArray", "initWithCapacity", FickleExpression.Call(Expression.Convert(value, FickleType.Define("NSArray")), typeof(int), "count", null))).ToStatement(),
                        FickleExpression.ForEach(arrayItem, value, FickleExpression.Block(forEachBody)),
                        processOutputValue(Expression.Convert(arrayVar, valueType))
                    )
                );

                return ifThen;
            }
            else
            {
                throw new InvalidOperationException("Unsupported property type: " + valueType);
            }
        }
コード例 #12
0
        private string GetNormalizeRequestMethodName(Type forType, string format = "json")
        {
            format = format.Capitalize();

            forType = forType.GetFickleListElementType() ?? forType;
            forType = forType.GetUnwrappedNullableType();

            if (forType == typeof(TimeSpan) || forType == typeof(Guid) || forType == typeof(bool) || forType.IsEnum)
            {
                return "normalizeRequest" + forType.Name + format;
            }
            else if (forType.IsNumericType())
            {
                return "normalizeRequestNumber" + format;
            }
            else
            {
                return "normalizeRequestObject" + format;
            }
        }
コード例 #13
0
		protected override DbType GetDbType(Type type)
		{
			var unwrapped = type.GetUnwrappedNullableType();

			if (unwrapped.IsEnum)
			{
				return DbType.AnsiString;
			}

			return base.GetDbType(type);
		}