public Object GetBeanPropInternal(Object @object, Object key)
        {
            try {
                var result = _field.GetValue(@object);
                if (result == null) {
                    return null;
                }

                if (result is Map) {
                    return ((Map) result).Get(key);
                }

                var resultType = result.GetType();
                if (resultType.IsGenericDictionary()) {
                    return MagicMarker
                        .GetDictionaryFactory(resultType)
                        .Invoke(result)
                        .Get(key);
                }

                return null;
            }
            catch (PropertyAccessException) {
                throw;
            }
            catch (InvalidCastException e) {
                throw PropertyUtility.GetMismatchException(_field, @object, e);
            }
            catch (ArgumentException e) {
                throw PropertyUtility.GetIllegalArgumentException(_field, e);
            }
            catch (Exception e) {
                throw PropertyUtility.GetAccessExceptionField(_field, e);
            }
        }
        public Object GetBeanPropInternal(Object @object, Object key)
        {
            try {
                var result = _fastMethod.Invoke(@object, null);
                if (result == null) {
                    return null;
                }

                if (result is Map) {
                    return ((Map) result).Get(key);
                }

                if (result.GetType().IsGenericDictionary()) {
                    return MagicMarker
                        .GetDictionaryFactory(result.GetType())
                        .Invoke(result)
                        .Get(key);
                }

                return null;
            }
            catch (PropertyAccessException) {
                throw;
            }
            catch (InvalidCastException e) {
                throw PropertyUtility.GetMismatchException(_fastMethod.Target, @object, e);
            }
            catch (TargetInvocationException e) {
                throw PropertyUtility.GetInvocationTargetException(_fastMethod.Target, e);
            }
            catch (Exception e) {
                throw PropertyUtility.GetAccessExceptionMethod(_fastMethod.Target, e);
            }
        }
Exemple #3
0
            public void Add(ICollection<Object> constants, Object value)
            {
                IEnumerable<object> mapKeys;

                if (value.GetType().IsGenericDictionary())
                    mapKeys = MagicMarker.GetDictionaryFactory(value.GetType()).Invoke(value).Keys;
                else
                    throw new ArgumentException("invalid value", nameof(value));

                constants.AddAll(mapKeys);
            }
Exemple #4
0
        public static IDictionary <string, object> UnwrapStringDictionary(this object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is IDictionary <string, object> )
            {
                return((IDictionary <string, object>)value);
            }

            var valueType = value.GetType();

            if (valueType.IsGenericStringDictionary())
            {
                return(MagicMarker.GetStringDictionaryFactory(valueType).Invoke(value));
            }

            if (value is IEnumerable <KeyValuePair <string, object> > )
            {
                var valueDataMap = new Dictionary <string, object>();
                foreach (var valueKeyValuePair in (IEnumerable <KeyValuePair <string, object> >)value)
                {
                    valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                }

                return(valueDataMap);
            }

            if (value is KeyValuePair <string, object> )
            {
                var valueDataMap      = new Dictionary <string, object>();
                var valueKeyValuePair = (KeyValuePair <string, object>)value;
                valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                return(valueDataMap);
            }

            // use this sparingly since its more expensive... we may need to write
            // a more generalized method if this becomes commonplace.

            var dictType = valueType.FindGenericInterface(typeof(IDictionary <,>));

            if (dictType != null)
            {
                var magicDictionary = MagicMarker.GetDictionaryFactory(valueType).Invoke(value);
                return(magicDictionary.Transform(
                           ki => Convert.ToString(ki),
                           ke => ke));
            }

            throw new ArgumentException("unable to convert input to string dictionary");
        }
Exemple #5
0
        public static string Render <K, V>(this IEnumerable <KeyValuePair <K, V> > source)
        {
            var fieldDelimiter = string.Empty;

            var builder = new StringBuilder();

            builder.Append('[');

            if (source != null)
            {
                foreach (var current in source)
                {
                    builder.Append(fieldDelimiter);
                    builder.Append(RenderAny(current.Key));
                    builder.Append('=');
                    if (ReferenceEquals(current.Value, null))
                    {
                        builder.Append("null");
                    }
                    else if (current.Value.GetType().IsGenericDictionary())
                    {
                        builder.Append(MagicMarker.GetDictionaryFactory(current.Value.GetType()).Invoke(current.Value));
                    }
                    else if (current.Value is string)
                    {
                        builder.Append(RenderAny(current.Value));
                    }
                    else if (current.Value is IEnumerable)
                    {
                        builder.Append(Render((IEnumerable)current.Value));
                    }
                    else
                    {
                        builder.Append(RenderAny(current.Value));
                    }

                    fieldDelimiter = ", ";
                }
            }

            builder.Append(']');
            return(builder.ToString());
        }
Exemple #6
0
        public static IDictionary <object, object> UnwrapDictionary(this object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is IDictionary <object, object> )
            {
                return((IDictionary <object, object>)value);
            }

            var valueType = value.GetType();

            if (valueType.IsGenericDictionary())
            {
                return(MagicMarker.GetDictionaryFactory(valueType).Invoke(value));
            }

            if (value is IEnumerable <KeyValuePair <object, object> > )
            {
                var valueDataMap = new Dictionary <object, object>();
                foreach (var valueKeyValuePair in (IEnumerable <KeyValuePair <string, object> >)value)
                {
                    valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                }

                return(valueDataMap);
            }

            if (value is KeyValuePair <object, object> )
            {
                var valueDataMap      = new Dictionary <object, object>();
                var valueKeyValuePair = (KeyValuePair <object, object>)value;
                valueDataMap[valueKeyValuePair.Key] = valueKeyValuePair.Value;
                return(valueDataMap);
            }

            throw new ArgumentException("unable to convert input to string dictionary");
        }
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            // Must have 2 child nodes
            var childNodes = ChildNodes;

            if (childNodes.Count < 1)
            {
                throw new IllegalStateException("Group relational op node must have 1 or more parameters");
            }

            _evaluators = ExprNodeUtility.GetEvaluators(childNodes);

            var typeOne = _evaluators[0].ReturnType.GetBoxedType();

            // collections, array or map not supported
            if ((typeOne.IsArray) ||
                (typeOne.IsGenericCollection()) ||
                (typeOne.IsGenericDictionary()))
            {
                throw new ExprValidationException(
                          "Collection or array comparison is not allowed for the IN, ANY, SOME or ALL keywords");
            }

            _transformList = new Func <object, object> [childNodes.Count];

            var comparedTypes = new List <Type>();

            comparedTypes.Add(typeOne);
            _hasCollectionOrArray = false;
            for (int i = 1; i < childNodes.Count; i++)
            {
                var propType = _evaluators[i].ReturnType;
                if (propType.IsArray)
                {
                    _hasCollectionOrArray = true;
                    if (propType.GetElementType() != typeof(Object))
                    {
                        comparedTypes.Add(propType.GetElementType());
                    }
                }
                else if (propType.IsGenericDictionary())
                {
                    var baseTransform = MagicMarker.GetDictionaryFactory(propType);
                    _transformList[i]     = o => baseTransform(o);
                    _hasCollectionOrArray = true;
                }
                else if (propType.IsGenericCollection())
                {
                    var baseTransform = MagicMarker.GetCollectionFactory(propType);
                    _transformList[i]     = o => baseTransform(o);
                    _hasCollectionOrArray = true;
                }
                else
                {
                    comparedTypes.Add(propType);
                }
            }

            // Determine common denominator type
            Type coercionType;

            try
            {
                coercionType = TypeHelper.GetCommonCoercionType(comparedTypes.ToArray());
            }
            catch (CoercionException ex)
            {
                throw new ExprValidationException("Implicit conversion not allowed: " + ex.Message);
            }

            // Must be either numeric or string
            if (coercionType != typeof(String))
            {
                if (!coercionType.IsNumeric())
                {
                    throw new ExprValidationException(string.Format("Implicit conversion from datatype '{0}' to numeric is not allowed", Name.Clean(coercionType)));
                }
            }

            _computer = _relationalOp.GetComputer(coercionType, coercionType, coercionType);

            return(null);
        }
Exemple #8
0
        public void ValidateWithoutContext()
        {
            if (ChildNodes.Length < 2)
            {
                throw new ExprValidationException("The IN operator requires at least 2 child expressions");
            }
            _evaluators = ExprNodeUtility.GetEvaluators(ChildNodes);

            // Must be the same boxed type returned by expressions under this
            var typeOne = _evaluators[0].ReturnType.GetBoxedType();

            // collections, array or map not supported
            if ((typeOne.IsArray) ||
                (typeOne.IsGenericCollection()) ||
                (typeOne.IsGenericDictionary()))
            {
                throw new ExprValidationException("Collection or array comparison is not allowed for the IN, ANY, SOME or ALL keywords");
            }

            _transformList = new Func <object, object> [_evaluators.Length];

            var comparedTypes = new List <Type> {
                typeOne
            };

            _hasCollectionOrArray = false;

            var length = ChildNodes.Length - 1;

            for (int i = 1; i <= length; i++)
            {
                var propType = _evaluators[i].ReturnType;
                if (propType == null)
                {
                    continue;
                }
                if (propType.IsArray)
                {
                    _hasCollectionOrArray = true;
                    if (propType.GetElementType() != typeof(Object))
                    {
                        comparedTypes.Add(propType.GetElementType());
                    }
                }
                else if (propType.IsGenericDictionary())
                {
                    var baseTransform = MagicMarker.GetDictionaryFactory(propType);
                    _transformList[i]     = o => baseTransform(o);
                    _hasCollectionOrArray = true;
                }
                else if (propType.IsGenericCollection())
                {
                    var baseTransform = MagicMarker.GetCollectionFactory(propType);
                    _transformList[i]     = o => baseTransform(o);
                    _hasCollectionOrArray = true;
                }
                else
                {
                    comparedTypes.Add(propType);
                }
            }

            // Determine common denominator type
            Type coercionType;

            try {
                coercionType = TypeHelper.GetCommonCoercionType(comparedTypes);
            }
            catch (CoercionException ex)
            {
                throw new ExprValidationException("Implicit conversion not allowed: " + ex.Message);
            }

            // Check if we need to coerce
            _mustCoerce = false;
            if (coercionType.IsNumeric())
            {
                foreach (Type compareType in comparedTypes)
                {
                    if (coercionType != compareType.GetBoxedType())
                    {
                        _mustCoerce = true;
                    }
                }
                if (_mustCoerce)
                {
                    _coercer = CoercerFactory.GetCoercer(null, coercionType.GetBoxedType());
                }
            }
        }
Exemple #9
0
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            // Must have 2 child nodes
            var childNodes = ChildNodes;

            if (childNodes.Length < 1)
            {
                throw new IllegalStateException("Equals group node does not have 1 or more parameters");
            }

            _evaluators = ExprNodeUtility.GetEvaluators(ChildNodes);

            // Must be the same boxed type returned by expressions under this
            Type typeOne = _evaluators[0].ReturnType.GetBoxedType();

            // collections, array or map not supported
            if ((typeOne.IsArray) ||
                (typeOne.IsGenericCollection()) ||
                (typeOne.IsGenericDictionary()))
            {
                throw new ExprValidationException(
                          "Collection or array comparison is not allowed for the IN, ANY, SOME or ALL keywords");
            }

            _transformList = new Func <object, object> [childNodes.Length];

            var comparedTypes = new List <Type>();

            comparedTypes.Add(typeOne);
            _hasCollectionOrArray = false;
            for (int i = 1; i < childNodes.Length; i++)
            {
                Type propType = _evaluators[i].ReturnType;
                if (propType.IsArray)
                {
                    _hasCollectionOrArray = true;
                    if (propType.GetElementType() != typeof(Object))
                    {
                        comparedTypes.Add(propType.GetElementType());
                    }
                }
                else if (propType.IsGenericDictionary())
                {
                    var baseTransform = MagicMarker.GetDictionaryFactory(propType);
                    _transformList[i]     = o => baseTransform(o);
                    _hasCollectionOrArray = true;
                }
                else if (propType.IsGenericCollection())
                {
                    var baseTransform = MagicMarker.GetCollectionFactory(propType);
                    _transformList[i]     = o => baseTransform(o);
                    _hasCollectionOrArray = true;
                }
                else
                {
                    comparedTypes.Add(propType);
                }
            }

            // Determine common denominator type
            Type coercionType;

            try
            {
                coercionType = TypeHelper.GetCommonCoercionType(comparedTypes);
            }
            catch (CoercionException ex)
            {
                throw new ExprValidationException("Implicit conversion not allowed: " + ex.Message);
            }

            // Check if we need to coerce
            _mustCoerce = false;
            if (coercionType.IsNumeric())
            {
                foreach (Type compareType in comparedTypes)
                {
                    if (coercionType != compareType.GetBoxedType())
                    {
                        _mustCoerce = true;
                    }
                }
                if (_mustCoerce)
                {
                    _coercer = CoercerFactory.GetCoercer(null, coercionType.GetBoxedType());
                }
            }

            return(null);
        }