internal void WriteStringConstantExpressionElement(IEdmStringConstantExpression expression)
        {
            this.xmlWriter.WriteStartElement(CsdlConstants.Element_String);

            this.xmlWriter.WriteString(EdmValueWriter.StringAsXml(expression.Value));
            this.WriteEndElement();
        }
        /// <summary>
        /// Get the collection of string from the record using the given property name.
        /// </summary>
        /// <param name="record">The record expression.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The collection of string or null.</returns>
        public static IList <string> GetCollection(this IEdmRecordExpression record, string propertyName)
        {
            Utils.CheckArgumentNull(record, nameof(record));
            Utils.CheckArgumentNull(propertyName, nameof(propertyName));

            IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);

            if (property != null)
            {
                IEdmCollectionExpression collection = property.Value as IEdmCollectionExpression;
                if (collection != null && collection.Elements != null)
                {
                    IList <string> items = new List <string>();
                    foreach (var item in collection.Elements)
                    {
                        IEdmStringConstantExpression itemRecord = item as IEdmStringConstantExpression;
                        items.Add(itemRecord.Value);
                    }

                    return(items);
                }
            }

            return(null);
        }
예제 #3
0
 private static bool TryAssertStringConstantAsType(IEdmStringConstantExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
 {
     if (type.IsString())
     {
         IEdmStringTypeReference edmStringTypeReference = type.AsString();
         int?maxLength = edmStringTypeReference.MaxLength;
         if (maxLength.HasValue)
         {
             int?nullable = edmStringTypeReference.MaxLength;
             if (expression.Value.Length > nullable.Value)
             {
                 EdmError[] edmError   = new EdmError[1];
                 int?       maxLength1 = edmStringTypeReference.MaxLength;
                 edmError[0]      = new EdmError(expression.Location(), EdmErrorCode.StringConstantLengthOutOfRange, Strings.EdmModel_Validator_Semantic_StringConstantLengthOutOfRange(expression.Value.Length, maxLength1.Value));
                 discoveredErrors = edmError;
                 return(false);
             }
         }
         discoveredErrors = Enumerable.Empty <EdmError>();
         return(true);
     }
     else
     {
         EdmError[] edmErrorArray = new EdmError[1];
         edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
         discoveredErrors = edmErrorArray;
         return(false);
     }
 }
        public override bool Equals(object obj)
        {
            IEdmStringConstantExpression another = obj as IEdmStringConstantExpression;

            if (another == null)
            {
                return(false);
            }

            return(this.Value == another.Value);
        }
예제 #5
0
        private static bool TryAssertStringConstantAsType(IEdmStringConstantExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            if (!type.IsString())
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType) };
                return(false);
            }

            IEdmStringTypeReference stringType = type.AsString();

            if (stringType.MaxLength.HasValue && expression.Value.Length > stringType.MaxLength.Value)
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.StringConstantLengthOutOfRange, Edm.Strings.EdmModel_Validator_Semantic_StringConstantLengthOutOfRange(expression.Value.Length, stringType.MaxLength.Value)) };
                return(false);
            }

            discoveredErrors = Enumerable.Empty <EdmError>();
            return(true);
        }
        /// <summary>
        /// Gets the string value of a property in the given record expression.
        /// </summary>
        /// <param name="record">The given record.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The property string value.</returns>
        public static string GetString(this IEdmRecordExpression record, string propertyName)
        {
            Utils.CheckArgumentNull(record, nameof(record));
            Utils.CheckArgumentNull(propertyName, nameof(propertyName));

            if (record.Properties != null)
            {
                IEdmPropertyConstructor property = record.Properties.FirstOrDefault(e => e.Name == propertyName);
                if (property != null)
                {
                    IEdmStringConstantExpression value = property.Value as IEdmStringConstantExpression;
                    if (value != null)
                    {
                        return(value.Value);
                    }
                }
            }

            return(null);
        }
예제 #7
0
        /// <summary>
        /// Gets the Org.OData.Core.V1.AcceptableMediaTypes
        /// </summary>
        /// <param name="model">The Edm model.</param>
        /// <param name="target">The vocabulary annotatable target.</param>
        /// <returns>null or the collection of media type.</returns>
        internal static IList <string> GetAcceptableMediaTypes(this IEdmModel model, IEdmVocabularyAnnotatable target)
        {
            if (model == null)
            {
                throw Error.ArgumentNull(nameof(model));
            }

            if (target == null)
            {
                throw Error.ArgumentNull(nameof(target));
            }

            IList <string>           mediaTypes  = null;
            var                      annotations = model.FindVocabularyAnnotations <IEdmVocabularyAnnotation>(target, CoreVocabularyModel.AcceptableMediaTypesTerm);
            IEdmVocabularyAnnotation annotation  = annotations.FirstOrDefault();

            if (annotation != null)
            {
                IEdmCollectionExpression properties = annotation.Value as IEdmCollectionExpression;
                if (properties != null)
                {
                    mediaTypes = new List <string>();
                    foreach (var element in properties.Elements)
                    {
                        IEdmStringConstantExpression elementValue = element as IEdmStringConstantExpression;
                        if (elementValue != null)
                        {
                            mediaTypes.Add(elementValue.Value);
                        }
                    }
                }
            }

            if (mediaTypes == null || mediaTypes.Count == 0)
            {
                return(null);
            }

            return(mediaTypes);
        }
예제 #8
0
 protected override void ProcessStringConstantExpression(IEdmStringConstantExpression expression)
 {
     this.schemaWriter.WriteStringConstantExpressionElement(expression);
 }
예제 #9
0
        private static bool TryCastStringConstantAsType(IEdmStringConstantExpression expression, IEdmTypeReference type, out IEnumerable<EdmError> discoveredErrors)
        {
            if (!type.IsString())
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType) };
                return false;
            }

            IEdmStringTypeReference stringType = type.AsString();
            if (stringType.MaxLength.HasValue && expression.Value.Length > stringType.MaxLength.Value)
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.StringConstantLengthOutOfRange, Edm.Strings.EdmModel_Validator_Semantic_StringConstantLengthOutOfRange(expression.Value.Length, stringType.MaxLength.Value)) };
                return false;
            }

            discoveredErrors = Enumerable.Empty<EdmError>();
            return true;
        }
예제 #10
0
 protected virtual void ProcessStringConstantExpression(IEdmStringConstantExpression expression)
 {
     this.ProcessExpression(expression);
 }
예제 #11
0
 internal abstract void WriteStringConstantExpressionElement(IEdmStringConstantExpression expression);
예제 #12
0
        /// <summary>
        /// Given a model element, see if it is marked as deprecated.
        /// </summary>
        /// <param name="model">The model containing the element</param>
        /// <param name="element">The IEdmElement to be validated to see if it's marked as deprecated.</param>
        /// <param name="message">The returned deprecation message if the element is deprecated.</param>
        /// <param name="version">The returned version if the element is deprecated, if specified.</param>
        /// <param name="date">The returned date if the element is deprecated, if specified.</param>
        /// <param name="removalDate">The returned removal date if the element is deprecated, if specified.</param>
        /// <returns>True if the element is marked as deprecated, otherwise false.</returns>
        private static bool IsDeprecated(IEdmModel model, IEdmElement element, out string message, out string version, out Date?date, out Date?removalDate)
        {
            if (!(element is IEdmPrimitiveType))
            {
                IEdmVocabularyAnnotatable annotatedElement = element as IEdmVocabularyAnnotatable;
                if (annotatedElement != null)
                {
                    foreach (IEdmVocabularyAnnotation annotation in GetRevisionAnnotations(model, annotatedElement))
                    {
                        IEdmCollectionExpression collectionExpression = annotation.Value as IEdmCollectionExpression;
                        if (collectionExpression != null)
                        {
                            foreach (IEdmExpression versionRecord in collectionExpression.Elements)
                            {
                                bool isDeprecated = false;
                                message     = string.Empty;
                                version     = string.Empty;
                                date        = null;
                                removalDate = null;
                                IEdmRecordExpression record = versionRecord as IEdmRecordExpression;
                                if (record != null)
                                {
                                    foreach (IEdmPropertyConstructor property in record.Properties)
                                    {
                                        switch (property.Name)
                                        {
                                        case RevisionKindProperty:
                                            IEdmEnumMemberExpression enumValue = property.Value as IEdmEnumMemberExpression;
                                            if (enumValue != null)
                                            {
                                                if (string.Equals(enumValue.EnumMembers.FirstOrDefault().Name, RevisionKindDeprecated, StringComparison.OrdinalIgnoreCase))
                                                {
                                                    isDeprecated = true;
                                                }
                                                else
                                                {
                                                    continue;
                                                }
                                            }

                                            break;

                                        case RevisionVersionProperty:
                                            IEdmStringConstantExpression versionValue = property.Value as IEdmStringConstantExpression;
                                            if (versionValue != null)
                                            {
                                                version = versionValue.Value;
                                            }

                                            break;

                                        case RevisionDescriptionProperty:
                                            IEdmStringConstantExpression descriptionValue = property.Value as IEdmStringConstantExpression;
                                            if (descriptionValue != null)
                                            {
                                                message = descriptionValue.Value;
                                            }

                                            break;

                                        case RevisionDateProperty:
                                            IEdmDateConstantExpression dateValue = property.Value as IEdmDateConstantExpression;
                                            if (dateValue != null)
                                            {
                                                date = dateValue.Value;
                                            }

                                            break;

                                        case RevisionRemovalDateProperty:
                                            IEdmDateConstantExpression removalDateValue = property.Value as IEdmDateConstantExpression;
                                            if (removalDateValue != null)
                                            {
                                                removalDate = removalDateValue.Value;
                                            }

                                            break;

                                        default:
                                            break;
                                        }
                                    }
                                }

                                if (isDeprecated)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

            message = version = string.Empty;
            date    = removalDate = null;
            return(false);
        }
예제 #13
0
            private bool CheckForDeprecationOnType(OdcmObject odcmObject, IEdmVocabularyAnnotation revisionsAnnotation)
            {
                if (revisionsAnnotation != null)
                {
                    IEdmCollectionExpression collectionExpression = revisionsAnnotation.Value as IEdmCollectionExpression;
                    if (collectionExpression != null)
                    {
                        foreach (IEdmExpression versionRecord in collectionExpression.Elements)
                        {
                            bool   isDeprecated = false;
                            string message      = string.Empty;
                            string version      = string.Empty;

                            IEdmRecordExpression record = versionRecord as IEdmRecordExpression;
                            if (record != null)
                            {
                                foreach (IEdmPropertyConstructor property in record.Properties)
                                {
                                    switch (property.Name.ToLower())
                                    {
                                    case "kind":
                                        IEdmEnumMemberExpression enumValue = property.Value as IEdmEnumMemberExpression;
                                        if (enumValue != null)
                                        {
                                            if (string.Equals(enumValue.EnumMembers.FirstOrDefault().Name, "deprecated", StringComparison.OrdinalIgnoreCase))
                                            {
                                                isDeprecated = true;
                                            }
                                            else
                                            {
                                                continue;
                                            }
                                        }
                                        break;

                                    case "description":
                                        IEdmStringConstantExpression descriptionValue = property.Value as IEdmStringConstantExpression;
                                        if (descriptionValue != null)
                                        {
                                            message = descriptionValue.Value;
                                        }
                                        break;

                                    case "version":
                                        IEdmStringConstantExpression versionValue = property.Value as IEdmStringConstantExpression;
                                        if (versionValue != null)
                                        {
                                            version = versionValue.Value;
                                        }
                                        break;

                                    default:
                                        break;
                                    }
                                }

                                if (isDeprecated)
                                {
                                    odcmObject.Deprecation = new OdcmDeprecation
                                    {
                                        Description = message,
                                        Version     = version
                                    };

                                    return(true);
                                }
                            }
                        }
                    }
                }

                return(false);
            }
예제 #14
0
        /// <summary>
        /// Convert an <see cref="IEdmExpression"/> to a <see cref="ODataValue"/>
        /// </summary>
        /// <param name="expression">The <see cref="IEdmExpression"/>.</param>
        /// <returns>The null or <see cref="ODataValue"/>.</returns>
        public static ODataValue Convert(this IEdmExpression expression)
        {
            if (expression == null)
            {
                return(null);
            }

            switch (expression.ExpressionKind)
            {
            case EdmExpressionKind.BinaryConstant:
                IEdmBinaryConstantExpression binaryConstant = (IEdmBinaryConstantExpression)expression;
                return(new ODataPrimitiveValue(binaryConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetBinary(false)
                });

            case EdmExpressionKind.BooleanConstant:
                IEdmBooleanConstantExpression booleanConstant = (IEdmBooleanConstantExpression)expression;
                return(new ODataPrimitiveValue(booleanConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetBoolean(false)
                });

            case EdmExpressionKind.DateTimeOffsetConstant:
                IEdmDateTimeOffsetConstantExpression dateTimeOffsetConstant = (IEdmDateTimeOffsetConstantExpression)expression;
                return(new ODataPrimitiveValue(dateTimeOffsetConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetDateTimeOffset(false)
                });

            case EdmExpressionKind.DecimalConstant:
                IEdmDecimalConstantExpression decimalConstant = (IEdmDecimalConstantExpression)expression;
                return(new ODataPrimitiveValue(decimalConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetDecimal(false)
                });

            case EdmExpressionKind.FloatingConstant:
                IEdmFloatingConstantExpression floatConstant = (IEdmFloatingConstantExpression)expression;
                return(new ODataPrimitiveValue(floatConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetDouble(false)
                });

            case EdmExpressionKind.GuidConstant:
                IEdmGuidConstantExpression guidConstant = (IEdmGuidConstantExpression)expression;
                return(new ODataPrimitiveValue(guidConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetGuid(false)
                });

            case EdmExpressionKind.IntegerConstant:
                IEdmIntegerConstantExpression integerConstant = (IEdmIntegerConstantExpression)expression;
                return(new ODataPrimitiveValue(integerConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetInt64(false)
                });

            case EdmExpressionKind.StringConstant:
                IEdmStringConstantExpression stringConstant = (IEdmStringConstantExpression)expression;
                return(new ODataPrimitiveValue(stringConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetString(false)
                });

            case EdmExpressionKind.DurationConstant:
                IEdmDurationConstantExpression durationConstant = (IEdmDurationConstantExpression)expression;
                return(new ODataPrimitiveValue(durationConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetDuration(false)
                });

            case EdmExpressionKind.TimeOfDayConstant:
                IEdmTimeOfDayConstantExpression timeOfDayConstant = (IEdmTimeOfDayConstantExpression)expression;
                return(new ODataPrimitiveValue(timeOfDayConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetTimeOfDay(false)
                });

            case EdmExpressionKind.DateConstant:
                IEdmDateConstantExpression dateConstant = (IEdmDateConstantExpression)expression;
                return(new ODataPrimitiveValue(dateConstant.Value)
                {
                    TypeReference = EdmCoreModel.Instance.GetDate(false)
                });

            case EdmExpressionKind.Record:
                IEdmRecordExpression recordExpression = (IEdmRecordExpression)expression;
                return(new ODataResourceValue
                {
                    TypeReference = recordExpression.DeclaredType,
                    Properties = recordExpression.Properties.ToDictionary(p => p.Name, p => p.Value.Convert())
                });

            case EdmExpressionKind.Collection:
                IEdmCollectionExpression collectionExpression = (IEdmCollectionExpression)expression;
                ODataCollectValue        collectionValue      = new ODataCollectValue
                {
                    TypeReference = collectionExpression.DeclaredType
                };

                collectionValue.Elements = collectionExpression.Elements.Select(e => e.Convert()).ToList();
                return(collectionValue);

            case EdmExpressionKind.Path:
            case EdmExpressionKind.PropertyPath:
            case EdmExpressionKind.NavigationPropertyPath:
            case EdmExpressionKind.EnumMember:
            default:
                throw new NotSupportedException(String.Format(SRResource.NotSupportedEdmExpressionKind, expression.ExpressionKind));
            }
        }
예제 #15
0
		private static bool TryAssertStringConstantAsType(IEdmStringConstantExpression expression, IEdmTypeReference type, out IEnumerable<EdmError> discoveredErrors)
		{
			if (type.IsString())
			{
				IEdmStringTypeReference edmStringTypeReference = type.AsString();
				int? maxLength = edmStringTypeReference.MaxLength;
				if (maxLength.HasValue)
				{
					int? nullable = edmStringTypeReference.MaxLength;
					if (expression.Value.Length > nullable.Value)
					{
						EdmError[] edmError = new EdmError[1];
						int? maxLength1 = edmStringTypeReference.MaxLength;
						edmError[0] = new EdmError(expression.Location(), EdmErrorCode.StringConstantLengthOutOfRange, Strings.EdmModel_Validator_Semantic_StringConstantLengthOutOfRange(expression.Value.Length, maxLength1.Value));
						discoveredErrors = edmError;
						return false;
					}
				}
				discoveredErrors = Enumerable.Empty<EdmError>();
				return true;
			}
			else
			{
				EdmError[] edmErrorArray = new EdmError[1];
				edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
				discoveredErrors = edmErrorArray;
				return false;
			}
		}