Пример #1
0
        internal static bool TryAssertRecordAsType(this IEdmRecordExpression expression, IEdmTypeReference type, IEdmType context, bool matchExactly, out IEnumerable <EdmError> discoveredErrors)
        {
            EdmUtil.CheckArgumentNull(expression, "expression");
            EdmUtil.CheckArgumentNull(type, "type");

            if (!type.IsStructured())
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.RecordExpressionNotValidForNonStructuredType, Edm.Strings.EdmModel_Validator_Semantic_RecordExpressionNotValidForNonStructuredType) };
                return(false);
            }

            HashSetInternal <string> foundProperties = new HashSetInternal <string>();
            List <EdmError>          errors          = new List <EdmError>();

            IEdmStructuredTypeReference structuredType = type.AsStructured();

            foreach (IEdmProperty typeProperty in structuredType.StructuredDefinition().Properties())
            {
                IEdmPropertyConstructor expressionProperty = expression.Properties.FirstOrDefault(p => p.Name == typeProperty.Name);
                if (expressionProperty == null)
                {
                    errors.Add(new EdmError(expression.Location(), EdmErrorCode.RecordExpressionMissingRequiredProperty, Edm.Strings.EdmModel_Validator_Semantic_RecordExpressionMissingProperty(typeProperty.Name)));
                }
                else
                {
                    IEnumerable <EdmError> recursiveErrors;
                    if (!expressionProperty.Value.TryAssertType(typeProperty.Type, context, matchExactly, out recursiveErrors))
                    {
                        foreach (EdmError error in recursiveErrors)
                        {
                            errors.Add(error);
                        }
                    }

                    foundProperties.Add(typeProperty.Name);
                }
            }

            if (!structuredType.IsOpen())
            {
                foreach (IEdmPropertyConstructor property in expression.Properties)
                {
                    if (!foundProperties.Contains(property.Name))
                    {
                        errors.Add(new EdmError(expression.Location(), EdmErrorCode.RecordExpressionHasExtraProperties, Edm.Strings.EdmModel_Validator_Semantic_RecordExpressionHasExtraProperties(property.Name)));
                    }
                }
            }

            if (errors.FirstOrDefault() != null)
            {
                discoveredErrors = errors;
                return(false);
            }

            discoveredErrors = Enumerable.Empty <EdmError>();
            return(true);
        }
Пример #2
0
        internal static bool TryAssertRecordAsType(this IEdmRecordExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            IEnumerable <EdmError> edmErrors = null;

            EdmUtil.CheckArgumentNull <IEdmRecordExpression>(expression, "expression");
            EdmUtil.CheckArgumentNull <IEdmTypeReference>(type, "type");
            if (type.IsStructured())
            {
                HashSetInternal <string>    strs       = new HashSetInternal <string>();
                List <EdmError>             edmErrors1 = new List <EdmError>();
                IEdmStructuredTypeReference edmStructuredTypeReference = type.AsStructured();
                IEnumerator <IEdmProperty>  enumerator = edmStructuredTypeReference.StructuredDefinition().Properties().GetEnumerator();
                using (enumerator)
                {
                    Func <IEdmPropertyConstructor, bool> func = null;
                    while (enumerator.MoveNext())
                    {
                        IEdmProperty current = enumerator.Current;
                        IEnumerable <IEdmPropertyConstructor> properties = expression.Properties;
                        if (func == null)
                        {
                            func = (IEdmPropertyConstructor p) => p.Name == current.Name;
                        }
                        IEdmPropertyConstructor edmPropertyConstructor = properties.FirstOrDefault <IEdmPropertyConstructor>(func);
                        if (edmPropertyConstructor != null)
                        {
                            if (!edmPropertyConstructor.Value.TryAssertType(current.Type, out edmErrors))
                            {
                                IEnumerator <EdmError> enumerator1 = edmErrors.GetEnumerator();
                                using (enumerator1)
                                {
                                    while (enumerator1.MoveNext())
                                    {
                                        EdmError edmError = enumerator1.Current;
                                        edmErrors1.Add(edmError);
                                    }
                                }
                            }
                            strs.Add(current.Name);
                        }
                        else
                        {
                            edmErrors1.Add(new EdmError(expression.Location(), EdmErrorCode.RecordExpressionMissingRequiredProperty, Strings.EdmModel_Validator_Semantic_RecordExpressionMissingProperty(current.Name)));
                        }
                    }
                }
                if (!edmStructuredTypeReference.IsOpen())
                {
                    foreach (IEdmPropertyConstructor property in expression.Properties)
                    {
                        if (strs.Contains(property.Name))
                        {
                            continue;
                        }
                        edmErrors1.Add(new EdmError(expression.Location(), EdmErrorCode.RecordExpressionHasExtraProperties, Strings.EdmModel_Validator_Semantic_RecordExpressionHasExtraProperties(property.Name)));
                    }
                }
                if (edmErrors1.FirstOrDefault <EdmError>() == null)
                {
                    discoveredErrors = Enumerable.Empty <EdmError>();
                    return(true);
                }
                else
                {
                    discoveredErrors = edmErrors1;
                    return(false);
                }
            }
            else
            {
                EdmError[] edmErrorArray = new EdmError[1];
                edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.RecordExpressionNotValidForNonStructuredType, Strings.EdmModel_Validator_Semantic_RecordExpressionNotValidForNonStructuredType);
                discoveredErrors = edmErrorArray;
                return(false);
            }
        }