public void Add(ICollection<Object> constants, Object value) { ICollection<object> collection; if (value is ICollection<object>) collection = (ICollection<object>)value; else if (value.GetType().IsGenericCollection()) collection = MagicMarker.GetCollectionFactory(value.GetType()).Invoke(value); else throw new ArgumentException("invalid value", nameof(value)); constants.AddAll(collection); }
private static bool AreCollectionsEqual(Object expected, Object actual) { var magicExpected = MagicMarker.GetCollectionFactory(expected.GetType()) .Invoke(expected); var magicActual = MagicMarker.GetCollectionFactory(actual.GetType()) .Invoke(actual); if (magicExpected.Count == magicActual.Count) { using (var magicExpectedEnum = magicExpected.GetEnumerator()) { using (var magicActualEnum = magicActual.GetEnumerator()) { while (true) { var mvExpected = magicExpectedEnum.MoveNext(); var mvActual = magicActualEnum.MoveNext(); if (mvExpected && mvActual) { if (!Equals(magicExpectedEnum.Current, magicActualEnum.Current)) { break; } } else if (!mvExpected && !mvActual) { return(true); } else { throw new IllegalStateException("collection has been modified"); } } } } } return(false); }
public ICollection <object> Convert(Object result) { if (result == null) { return(null); } if (result is ICollection <object> ) { return((ICollection <object>)result); } if (result.GetType().IsGenericCollection()) { return(MagicMarker.GetCollectionFactory(result.GetType()).Invoke(result)); } if (result is ICollection) { return(((ICollection)result).Cast <object>().ToList()); } Log.Warn("Expected collection-type input from method '{0}' but received {1}", _methodName, result.GetType().FullName); return(null); }
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); }
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()); } } }
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); }