/// <summary>
        /// Gets the CLR Enum member for <see cref="IEdmEnumMember"/>.
        /// </summary>
        /// <param name="edmEnumMember">The Edm <see cref="IEdmEnumMember"/>.</param>
        /// <returns>The backing CLR Enum member info.</returns>
        public Enum GetClrEnumMember(IEdmEnumMember edmEnumMember)
        {
            Enum value;

            _reverseMap.TryGetValue(edmEnumMember, out value);
            return(value);
        }
        /// <summary>
        /// Get the Enum value from the record using the given property name.
        /// </summary>
        /// <typeparam name="T">The output enum type.</typeparam>
        /// <param name="record">The record expression.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The Enum value or null.</returns>
        public static T?GetEnum <T>(this IEdmRecordExpression record, string propertyName)
            where T : struct
        {
            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)
                {
                    IEdmEnumMemberExpression value = property.Value as IEdmEnumMemberExpression;
                    if (value != null && value.EnumMembers != null && value.EnumMembers.Any())
                    {
                        IEdmEnumMember member = value.EnumMembers.First();
                        T result;
                        if (Enum.TryParse(member.Name, out result))
                        {
                            return(result);
                        }
                    }
                }
            }

            return(null);
        }
Beispiel #3
0
        public void InitializeApiKeyWithRecordSuccess()
        {
            // Arrange
            EdmModel       model      = new EdmModel();
            IEdmType       edmType    = model.FindType("Org.OData.Authorization.V1.KeyLocation");
            IEdmEnumType   enumType   = edmType as IEdmEnumType;
            IEdmEnumMember enumMember = enumType.Members.FirstOrDefault(c => c.Name == "Header");

            Assert.NotNull(enumMember);

            IEdmRecordExpression record = new EdmRecordExpression(
                new EdmPropertyConstructor("Name", new EdmStringConstant("DelegatedWork")),
                new EdmPropertyConstructor("Description", new EdmStringConstant("Description of the authorization scheme")),
                new EdmPropertyConstructor("KeyName", new EdmStringConstant("keyName")),
                new EdmPropertyConstructor("Location", new EdmEnumMemberExpression(enumMember)));

            ApiKey apiKey = new ApiKey();

            Assert.Null(apiKey.Name);
            Assert.Null(apiKey.Description);
            Assert.Null(apiKey.Location);
            Assert.Null(apiKey.KeyName);

            // Act
            apiKey.Initialize(record);

            // Assert
            Assert.Equal("DelegatedWork", apiKey.Name);
            Assert.Equal("Description of the authorization scheme", apiKey.Description);
            Assert.Equal("keyName", apiKey.KeyName);
            Assert.Equal(KeyLocation.Header, apiKey.Location);
        }
        internal static void GenerateEnum(IEdmEnumMember member, EnumBuilder enumBuilder, ModuleBuilder moduleBuilder)
        {
            var memberName = member.Name;

            var memberValue = Convert.ToInt32(member.Value.Value);

            enumBuilder.DefineLiteral(memberName, memberValue);
        }
        internal void WriteEnumMemberElementHeader(IEdmEnumMember member)
        {
            this.xmlWriter.WriteStartElement("Member");
            this.WriteRequiredAttribute <string>("Name", member.Name, new Func <string, string>(EdmValueWriter.StringAsXml));
            bool?nullable = member.IsValueExplicit(this.model);

            if (!nullable.HasValue || nullable.Value)
            {
                this.WriteRequiredAttribute <IEdmPrimitiveValue>("Value", member.Value, new Func <IEdmPrimitiveValue, string>(EdmValueWriter.PrimitiveValueAsXml));
            }
        }
        internal void WriteEnumMemberElementHeader(IEdmEnumMember member)
        {
            this.xmlWriter.WriteStartElement(CsdlConstants.Element_Member);
            this.WriteRequiredAttribute(CsdlConstants.Attribute_Name, member.Name, EdmValueWriter.StringAsXml);
            bool?isExplicit = member.IsValueExplicit(this.model);

            if (!isExplicit.HasValue || isExplicit.Value)
            {
                this.WriteRequiredAttribute(CsdlConstants.Attribute_Value, member.Value, EdmValueWriter.PrimitiveValueAsXml);
            }
        }
Beispiel #7
0
        /// <inheritdoc />
        public override object ReadInline(object item, IEdmTypeReference edmType, ODataDeserializerContext readContext)
        {
            if (item == null)
            {
                return(null);
            }

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

            ODataProperty property = item as ODataProperty;

            if (property != null)
            {
                item = property.Value;
            }

            IEdmEnumTypeReference enumTypeReference = edmType.AsEnum();
            ODataEnumValue        enumValue         = item as ODataEnumValue;

            if (readContext.IsNoClrType)
            {
                Contract.Assert(edmType.TypeKind() == EdmTypeKind.Enum);
                return(new EdmEnumObject(enumTypeReference, enumValue.Value));
            }

            IEdmEnumType enumType = enumTypeReference.EnumDefinition();

            // Enum member supports model alias case. So, try to use the Edm member name to retrieve the Enum value.
            var memberMapAnnotation = readContext.Model.GetClrEnumMemberAnnotation(enumType);

            if (memberMapAnnotation != null)
            {
                if (enumValue != null)
                {
                    IEdmEnumMember enumMember = enumType.Members.FirstOrDefault(m => m.Name == enumValue.Value);
                    if (enumMember != null)
                    {
                        var clrMember = memberMapAnnotation.GetClrEnumMember(enumMember);
                        if (clrMember != null)
                        {
                            return(clrMember);
                        }
                    }
                }
            }

            Type clrType = readContext.Model.GetClrType(edmType);

            return(EnumDeserializationHelpers.ConvertEnumValue(item, clrType));
        }
        private IEdmEnumMember ConvertToStockMember(IEdmEnumMember edmMember, IEdmModel edmModel, EdmModel stockModel)
        {
            var stockMemberDeclaringType = stockModel.FindType(GetFullName(edmMember.DeclaringType)) as IEdmEnumType;

            var stockMember = new EdmEnumMember(
                stockMemberDeclaringType,
                edmMember.Name,
                edmMember.Value
                );

            ((EdmEnumType)stockMemberDeclaringType).AddMember(stockMember);

            // TODO: Documentation
            this.SetImmediateAnnotations(edmMember, stockMember, edmModel, stockModel);

            return(stockMember);
        }
        private static void AddEnumDescription(IEdmEnumMember member, OpenApiEnumValuesDescriptionExtension target, ODataContext context)
        {
            if (target == null)
            {
                return;
            }

            var enumDescription = context.Model.GetDescriptionAnnotation(member);

            if (!string.IsNullOrEmpty(enumDescription))
            {
                target.ValuesDescriptions.Add(new EnumDescription
                {
                    Name        = member.Name,
                    Value       = member.Name,
                    Description = enumDescription
                });
            }
        }
 private static string EnumMemberAsXml(IEdmEnumMember member)
 {
     return(string.Concat(member.DeclaringType.FullName(), "/", member.Name));
 }
 internal override void WriteEnumMemberElementEnd(IEdmEnumMember member)
 {
     this.xmlWriter.WriteEndElement();
 }
Beispiel #12
0
 protected override void ProcessEnumMember(IEdmEnumMember element)
 {
     this.BeginElement(element, this.schemaWriter.WriteEnumMemberElementHeader);
     this.EndElement(element, this.schemaWriter.WriteEnumMemberElementEnd);
 }
Beispiel #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EdmEnumMemberReferenceExpression"/> class.
 /// </summary>
 /// <param name="referencedEnumMember">Referenced enum member.</param>
 public EdmEnumMemberReferenceExpression(IEdmEnumMember referencedEnumMember)
 {
     EdmUtil.CheckArgumentNull(referencedEnumMember, "referencedEnumMember");
     this.referencedEnumMember = referencedEnumMember;
 }
Beispiel #14
0
 /// <summary>
 /// Adds a enum member
 /// </summary>
 /// <param name="member">the specified enum member</param>
 public void Add(IEdmEnumMember member)
 {
     this.members.Add(member);
     ((StubEdmEnumMember)member).DeclaringType = this;
 }
 /// <summary>
 /// Sets an annotation indicating whether the value of an enum member should be explicitly serialized.
 /// </summary>
 /// <param name="member">Member to set the annotation on.</param>
 /// <param name="model">Model containing the member.</param>
 /// <param name="isExplicit">If the value of the enum member should be explicitly serialized</param>
 public static void SetIsValueExplicit(this IEdmEnumMember member, IEdmModel model, bool?isExplicit)
 {
     EdmUtil.CheckArgumentNull(member, "member");
     EdmUtil.CheckArgumentNull(model, "model");
     model.SetAnnotationValue(member, EdmConstants.InternalUri, CsdlConstants.IsEnumMemberValueExplicitAnnotation, (object)isExplicit);
 }
Beispiel #16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EdmEnumValue"/> class. 
 /// </summary>
 /// <param name="type">A reference to the enumeration type that describes this value.</param>
 /// <param name="member">The enumeration type value.</param>
 public EdmEnumValue(IEdmEnumTypeReference type, IEdmEnumMember member)
     : this(type, member.Value)
 {
 }
Beispiel #17
0
		public void AddMember(IEdmEnumMember member)
		{
			this.members.Add(member);
		}
 internal abstract void WriteEnumMemberElementHeader(IEdmEnumMember member);
 internal virtual void WriteEnumMemberElementEnd(IEdmEnumMember member)
 {
     // Nothing here
 }
        /// <summary>
        /// Try parse enum members specified in a string value from declared schema types.
        /// </summary>
        /// <param name="value">Enum value string</param>
        /// <param name="enumType">The enum type.</param>
        /// <param name="location">The location of the enum member in csdl</param>
        /// <param name="result">Parsed enum members</param>
        /// <returns>True for successfully parsed, false for failed</returns>
        internal static bool TryParseJsonEnumMember(string value, IEdmEnumType enumType, EdmLocation location, out IEnumerable <IEdmEnumMember> result)
        {
            result = null;
            bool isUnresolved = enumType is UnresolvedEnumType;

            if (isUnresolved)
            {
                result = new List <IEdmEnumMember>
                {
                    new UnresolvedEnumMember(value, enumType, location)
                };

                return(true);
            }

            List <IEdmEnumMember> enumMembers = new List <IEdmEnumMember>();

            // "@self.HasPattern": "1"
            // or with numeric value 1 + 16
            if (long.TryParse(value, out long longValue))
            {
                // In numeric value
                IEdmEnumMember member = enumType.Members.SingleOrDefault(m => m.Value.Value == longValue);
                if (member == null)
                {
                    if (enumType.IsFlags)
                    {
                        long  memberValue = 1;
                        ulong ulongValue  = (ulong)longValue;
                        ulong mask        = 1;
                        while (ulongValue != 0)
                        {
                            ulong add = ulongValue & mask;
                            if (add != 0)
                            {
                                member = enumType.Members.SingleOrDefault(m => m.Value.Value == memberValue);
                                if (member == null)
                                {
                                    return(false);
                                }

                                enumMembers.Add(member);
                            }

                            ulongValue  >>= 1;
                            memberValue <<= 1;
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    enumMembers.Add(member);
                }
            }
            else
            {
                // in symbolic value
                // "@self.HasPattern": "Red,Striped"
                string[] enumValues = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                if (enumValues.Length > 1 && (!enumType.IsFlags || !EdmEnumValueParser.IsEnumIntegerType(enumType)))
                {
                    return(false);
                }

                foreach (string enumValue in enumValues)
                {
                    IEdmEnumMember member = enumType.Members.SingleOrDefault(m => m.Name == enumValue.Trim());
                    if (member == null)
                    {
                        return(false);
                    }

                    enumMembers.Add(member);
                }
            }

            result = enumMembers;
            return(true);
        }
 public static void SetIsValueExplicit(this IEdmEnumMember member, IEdmModel model, bool?isExplicit)
 {
     EdmUtil.CheckArgumentNull <IEdmEnumMember>(member, "member");
     EdmUtil.CheckArgumentNull <IEdmModel>(model, "model");
     model.SetAnnotationValue(member, "http://schemas.microsoft.com/ado/2011/04/edm/internal", "IsEnumMemberValueExplicit", isExplicit);
 }
 public static bool?IsValueExplicit(this IEdmEnumMember member, IEdmModel model)
 {
     EdmUtil.CheckArgumentNull <IEdmEnumMember>(member, "member");
     EdmUtil.CheckArgumentNull <IEdmModel>(model, "model");
     return((bool?)(model.GetAnnotationValue(member, "http://schemas.microsoft.com/ado/2011/04/edm/internal", "IsEnumMemberValueExplicit") as bool?));
 }
Beispiel #23
0
        internal static string FullyQualifiedName(IEdmVocabularyAnnotatable element)
        {
            IEdmSchemaElement schemaElement = element as IEdmSchemaElement;

            if (schemaElement != null)
            {
                IEdmOperation operation = schemaElement as IEdmOperation;
                if (operation != null)
                {
                    return(ParameterizedName(operation));
                }
                else
                {
                    return(schemaElement.FullName());
                }
            }
            else
            {
                IEdmEntityContainerElement containerElement = element as IEdmEntityContainerElement;
                if (containerElement != null)
                {
                    return(containerElement.Container.FullName() + "/" + containerElement.Name);
                }
                else
                {
                    IEdmProperty property = element as IEdmProperty;
                    if (property != null)
                    {
                        IEdmSchemaType declaringSchemaType = property.DeclaringType as IEdmSchemaType;
                        if (declaringSchemaType != null)
                        {
                            string propertyOwnerName = FullyQualifiedName(declaringSchemaType);
                            if (propertyOwnerName != null)
                            {
                                return(propertyOwnerName + "/" + property.Name);
                            }
                        }
                    }
                    else
                    {
                        IEdmOperationParameter parameter = element as IEdmOperationParameter;
                        if (parameter != null)
                        {
                            string parameterOwnerName = FullyQualifiedName(parameter.DeclaringOperation);
                            if (parameterOwnerName != null)
                            {
                                return(parameterOwnerName + "/" + parameter.Name);
                            }
                        }
                        else
                        {
                            IEdmEnumMember enumMember = element as IEdmEnumMember;
                            if (enumMember != null)
                            {
                                string enumMemberOwnerName = FullyQualifiedName(enumMember.DeclaringType);
                                if (enumMemberOwnerName != null)
                                {
                                    return(enumMemberOwnerName + "/" + enumMember.Name);
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
        private IEdmEnumMember ConvertToStockMember(IEdmEnumMember edmMember, IEdmModel edmModel, EdmModel stockModel)
        {
            var stockMemberDeclaringType = stockModel.FindType(GetFullName(edmMember.DeclaringType)) as IEdmEnumType;

            var stockMember = new EdmEnumMember(
                                        stockMemberDeclaringType,
                                        edmMember.Name,
                                        edmMember.Value
                                    );
            ((EdmEnumType)stockMemberDeclaringType).AddMember(stockMember);

            // TODO: Documentation
            this.SetImmediateAnnotations(edmMember, stockMember, edmModel, stockModel);

            return stockMember;
        }
 private static string EnumMemberAsXml(IEdmEnumMember member)
 {
     return(member.DeclaringType.FullName() + "/" + member.Name);
 }
Beispiel #26
0
 public void VisitEnumMember(IEdmEnumMember enumMember)
 {
     this.ProcessEnumMember(enumMember);
 }
 protected override void ProcessEnumMember(IEdmEnumMember element)
 {
     this.BeginElement <IEdmEnumMember>(element, new Action <IEdmEnumMember>(this.schemaWriter.WriteEnumMemberElementHeader), new Action <IEdmEnumMember> [0]);
     this.EndElement(element);
 }
Beispiel #28
0
 protected virtual void ProcessEnumMember(IEdmEnumMember enumMember)
 {
     this.ProcessNamedElement(enumMember);
 }
Beispiel #29
0
 public void AddMember(IEdmEnumMember member)
 {
     this.members.Add(member);
 }
Beispiel #30
0
 /// <summary>
 /// Adds a enum member
 /// </summary>
 /// <param name="member">the specified enum member</param>
 public void Add(IEdmEnumMember member)
 {
     this.members.Add(member);
     ((StubEdmEnumMember)member).DeclaringType = this;
 }
 /// <summary>
 /// Gets an annotation indicating whether the value of an enum member should be explicitly serialized.
 /// </summary>
 /// <param name="member">The member the annotation is on.</param>
 /// <param name="model">Model containing the member.</param>
 /// <returns>Whether the member should have its value serialized.</returns>
 public static bool?IsValueExplicit(this IEdmEnumMember member, IEdmModel model)
 {
     EdmUtil.CheckArgumentNull(member, "member");
     EdmUtil.CheckArgumentNull(model, "model");
     return(model.GetAnnotationValue(member, EdmConstants.InternalUri, CsdlConstants.IsEnumMemberValueExplicitAnnotation) as bool?);
 }
Beispiel #32
0
        /// <summary>
        /// Try parse enum members specified in a string value from declared schema types
        /// </summary>
        /// <param name="value">Enum value string</param>
        /// <param name="model">The model for resolving enum type.</param>
        /// <param name="location">The location of the enum member in csdl</param>
        /// <param name="result">Parsed enum members</param>
        /// <returns>True for successfully parsed, false for failed</returns>
        internal static bool TryParseEnumMember(string value, IEdmModel model, EdmLocation location, out IEnumerable <IEdmEnumMember> result)
        {
            result = null;
            if (value == null || model == null)
            {
                return(false);
            }

            bool isUnresolved = false;
            var  enumValues   = value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (!enumValues.Any())
            {
                return(false);
            }

            string enumTypeName = enumValues[0].Split('/').FirstOrDefault();

            if (string.IsNullOrEmpty(enumTypeName))
            {
                return(false);
            }

            IEdmEnumType enumType = model.FindType(enumTypeName) as IEdmEnumType;

            if (enumType == null)
            {
                enumType     = new UnresolvedEnumType(enumTypeName, location);
                isUnresolved = true;
            }
            else if (enumValues.Count() > 1 && (!enumType.IsFlags || !EdmEnumValueParser.IsEnumIntegerType(enumType)))
            {
                return(false);
            }

            List <IEdmEnumMember> enumMembers = new List <IEdmEnumMember>();

            foreach (var enumValue in enumValues)
            {
                string[] path = enumValue.Split('/');
                if (path.Count() != 2)
                {
                    return(false);
                }

                if (path[0] != enumTypeName)
                {
                    return(false);
                }

                if (!isUnresolved)
                {
                    IEdmEnumMember member = enumType.Members.SingleOrDefault(m => m.Name == path[1]);
                    if (member == null)
                    {
                        return(false);
                    }

                    enumMembers.Add(member);
                }
                else
                {
                    enumMembers.Add(new UnresolvedEnumMember(path[1], enumType, location));
                }
            }

            result = enumMembers;
            return(true);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EdmEnumMemberReferenceExpression"/> class.
 /// </summary>
 /// <param name="referencedEnumMember">Referenced enum member.</param>
 public EdmEnumMemberReferenceExpression(IEdmEnumMember referencedEnumMember)
 {
     EdmUtil.CheckArgumentNull(referencedEnumMember, "referencedEnumMember");
     this.referencedEnumMember = referencedEnumMember;
 }
Beispiel #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EdmEnumValue"/> class.
 /// </summary>
 /// <param name="type">A reference to the enumeration type that describes this value.</param>
 /// <param name="member">The enumeration type value.</param>
 public EdmEnumValue(IEdmEnumTypeReference type, IEdmEnumMember member)
     : this(type, member.Value)
 {
 }