/// <summary>
        /// Computes values for enum members in the model with unspecified values.
        /// </summary>
        /// <param name="enumType">An enum type.</param>
        /// <returns>List of enum members with all values resolved.</returns>
        public IEnumerable<EnumMember> CalculateEnumMemberValues(EnumType enumType)
        {
            var enumMembers = new List<EnumMember>();
            long nextValue = 0;

            // Unspecified values are calculated based on the last specified value.
            foreach (var enumMember in enumType.Members)
            {
                object value = null;
                if (enumMember.Value != null)
                {
                    value = enumMember.Value;
                    nextValue = Convert.ToInt64(enumMember.Value, CultureInfo.InvariantCulture) + 1;
                }
                else
                {
                    value = nextValue;
                    nextValue++;
                }

                var resolvedEnumMember = this.CalculateValue(enumType, enumMember.Name, value);
                enumMembers.Add(resolvedEnumMember);
            }

            return enumMembers;
        }
        private EnumMember CalculateValue(EnumType enumType, string name, object value)
        {
            EnumMember enumMember;
             
            if (enumType.UnderlyingType == null)
                {
                    enumMember = new EnumMember(name, Convert.ChangeType(value, typeof(int), CultureInfo.InvariantCulture));
                }
                else
                {
                    enumMember = new EnumMember(name, Convert.ChangeType(value, enumType.UnderlyingType, CultureInfo.InvariantCulture));
                }

            return enumMember;
        }
Ejemplo n.º 3
0
 private XElement GenerateEnumType(EnumType enumType, XNamespace xmlNamespace)
 {
     return new XElement(
         xmlNamespace + "EnumType",
         new XAttribute("Name", enumType.Name),
         enumType.IsFlags == null ? null : new XAttribute("IsFlags", enumType.IsFlags),
         enumType.UnderlyingType == null ? null : new XAttribute("UnderlyingType", this.GetEdmNameForUnderlyingType(enumType.UnderlyingType)),
         this.GenerateDocumentation(xmlNamespace, enumType),
         this.GenerateEnumMembers(enumType.Members, xmlNamespace),
         this.GenerateAnnotations(xmlNamespace, enumType));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Removes an <see cref="EnumType"/> from the model
 /// </summary>
 /// <param name="enumType">Enum type to be removed</param>
 public void Remove(EnumType enumType)
 {
     ExceptionUtilities.CheckArgumentNotNull(enumType, "enumType");
     ExceptionUtilities.Assert(enumType.Model == this, "Enum type was not added to this model");
     ExceptionUtilities.Assert(this.enumTypesList.Remove(enumType), "Enum type was not added to this model");
     enumType.Model = null;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Adds new <see cref="EnumType"/> to the model
 /// </summary>
 /// <param name="enumType">Enum type to be added</param>
 public void Add(EnumType enumType)
 {
     ExceptionUtilities.CheckArgumentNotNull(enumType, "enumType");
     ExceptionUtilities.Assert(enumType.Model == null, "Enum type was already added to another model");
     enumType.Model = this;
     this.enumTypesList.Add(enumType);
 }
        private EnumType ConvertToTaupoEnumType(IEdmEnumType edmEnum)
        {
            var taupoEnumType = new EnumType(edmEnum.Namespace, edmEnum.Name);
            if (edmEnum.IsFlags)
            {
                taupoEnumType.IsFlags = true;
            }

            if (edmEnum.UnderlyingType != null)
            {
                taupoEnumType.UnderlyingType = this.ConvertToClrType(edmEnum.UnderlyingType);
            }

            foreach (var edmEnumMember in edmEnum.Members)
            {
                var taupoEnumMember = new EnumMember(edmEnumMember.Name);

                if (edmEnumMember.Value != null)
                {
                    taupoEnumMember.Value = this.ConvertToClrObject(edmEnumMember.Value);
                }

                taupoEnumType.Add(taupoEnumMember);
            }

            return taupoEnumType;
        }
Ejemplo n.º 7
0
 private void CompareEnumMember(EnumType expectedEnumType, EnumMember expectedMemeber, EnumType actualEnumType, EnumMember actualMember)
 {
     this.SatisfiesEquals(expectedMemeber.Name, actualMember.Name, "Enum member name not match.");
     this.SatisfiesEquals(this.GetIntegralEnumMemberValue(expectedEnumType, expectedMemeber), this.GetIntegralEnumMemberValue(actualEnumType, actualMember), "Enum member value does not match.");
     this.CompareAnnotations(expectedMemeber.Annotations, actualMember.Annotations);
 }
Ejemplo n.º 8
0
        private void CompareEnumMembers(EnumType expectedEnumType, EnumType actualEnumType)
        {
            this.SatisfiesEquals(expectedEnumType.Members.Count, actualEnumType.Members.Count, "Count of Members of '{0}' does not match.", expectedEnumType.FullName);

            foreach (var expectedMemeber in expectedEnumType.Members)
            {
                var actualMembers = actualEnumType.Members.Where(m => m.Name == expectedMemeber.Name);
                if (this.SatisfiesEquals(1, actualMembers.Count(), "Should find exactly one member '{0}'.", expectedMemeber.Name))
                {
                    var actualMember = actualMembers.Single();
                    this.CompareEnumMember(expectedEnumType, expectedMemeber, actualEnumType, actualMember);
                }
            }
        }
Ejemplo n.º 9
0
        private object GetIntegralEnumMemberValue(EnumType enumType, EnumMember enumMember)
        {
            var targetEnumMember = enumType.Members.Single(n => n == enumMember);

            if (targetEnumMember.Value != null)
            {
                long integralValue = 0;
                if (long.TryParse(targetEnumMember.Value.ToString(), out integralValue))
                {
                    return integralValue;
                }

                return targetEnumMember.Value;
            }

            if (enumType.Members.ElementAt(0) == targetEnumMember)
            {
                return 0L;
            }

            int memberIndex = 0;
            for (int i = 1; i < enumType.Members.Count; ++i)
            {
                if (enumType.Members.ElementAt(i) == targetEnumMember)
                {
                    memberIndex = i;
                    break;
                }
            }

            var previousMember = enumType.Members.ElementAt(memberIndex - 1);
            var previousValue = previousMember.Value;
            if (previousValue == null)
            {
                previousValue = this.GetIntegralEnumMemberValue(enumType, previousMember);
            }

            long previousIntegralValue = 0;
            if (long.TryParse(previousValue.ToString(), out previousIntegralValue))
            {
                return previousIntegralValue + 1;
            }
            else
            {
                return targetEnumMember.Value;
            }
        }
Ejemplo n.º 10
0
        private void CompareEnumType(EnumType expectedEnumType, EnumType actualEnumType)
        {
            this.SatisfiesEquals(expectedEnumType.FullName, actualEnumType.FullName, "Enum type name does not match.");

            if (expectedEnumType.IsFlags == null)
            {
                this.SatisfiesCondition(actualEnumType.IsFlags == null, "IsFlags should be null.");
            }
            else
            {
                this.SatisfiesEquals(expectedEnumType.IsFlags, actualEnumType.IsFlags, "IsFlags does not match.");
            }

            if (expectedEnumType.UnderlyingType == null)
            {
                this.SatisfiesCondition(actualEnumType.UnderlyingType == null, "Underlying type should be null.");
            }
            else
            {
                this.SatisfiesEquals(expectedEnumType.UnderlyingType, actualEnumType.UnderlyingType, "Underlying type does not match.");
            }

            this.CompareEnumMembers(expectedEnumType, actualEnumType);
        }
Ejemplo n.º 11
0
        private EnumType ParseEnumType(XElement enumTypeElement)
        {
            string name = enumTypeElement.GetRequiredAttributeValue("Name");
            string underlyingTypeString = enumTypeElement.GetOptionalAttributeValue("UnderlyingType", null);

            Type underlyingType = null;
            if (underlyingTypeString != null)
            {
                string underlyingTypeName = this.ParseEdmTypeName(underlyingTypeString)[1];
                underlyingType = Type.GetType("System." + underlyingTypeName);
            }

            var isFlagsString = enumTypeElement.Attribute("IsFlags");
            var enumType = new EnumType(this.CurrentNamespace, name) { IsFlags = isFlagsString == null ? (bool?)null : bool.Parse(isFlagsString.Value), UnderlyingType = underlyingType };

            foreach (var memberElement in enumTypeElement.Elements().Where(el => this.IsXsdlElement(el, "Member")))
            {
                enumType.Members.Add(this.ParseEnumMember(memberElement));
            }

            this.ParseAnnotations(enumType, enumTypeElement);
            return enumType;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Builds a code representation of an <see cref="EnumType"/>.
        /// </summary>
        /// <param name="type">The <see cref="EnumType"/> from which to generate code.</param>
        /// <returns>A <see cref="CodeTypeDeclaration"/> which represents the <see cref="EnumType"/>.</returns>
        protected virtual CodeTypeDeclaration BuildType(EnumType type)
        {
            var codeEnum = new CodeTypeDeclaration(type.Name);

            codeEnum.IsEnum = true;

            if (type.UnderlyingType != null)
            {
                codeEnum.BaseTypes.Add(type.UnderlyingType);
            }

            if (type.IsFlags == true)
            {
                codeEnum.AddCustomAttribute(typeof(FlagsAttribute));
            }

            ApplyTypeAccessModifier(codeEnum, type.Annotations.OfType<TypeAccessModifierAnnotation>().SingleOrDefault());

            if (type.Annotations.Any(a => a is SerializableAnnotation))
            {
                codeEnum.AddCustomAttribute(typeof(SerializableAttribute));
            }

            this.AddEnumMembers(codeEnum, type);

            return codeEnum;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Adds enum members to the generated type class.
        /// </summary>
        /// <param name="codeEnum">The <see cref="CodeTypeDeclaration"/> to which to add the enum members.</param>
        /// <param name="type">The definition of the enum type.</param>
        protected virtual void AddEnumMembers(CodeTypeDeclaration codeEnum, EnumType type)
        {
            foreach (var member in type.Members)
            {
                var codeMember = new CodeMemberField(codeEnum.Name, member.Name);
                if (member.Value != null)
                {
                    codeMember.InitExpression = new CodeSnippetExpression(member.Value.ToString());
                }

                codeEnum.Members.Add(codeMember);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Visit Enum type
        /// </summary>
        /// <param name="enumType">enum type to visit</param>
        protected virtual void VisitEnumType(EnumType enumType)
        {
            this.VisitAnnotatedItem(enumType);

            foreach (var enumMember in enumType.Members)
            {
                this.VisitEnumMember(enumMember);
            }
        }
 private QueryScalarType CreateStubEnumType(EnumType enumType)
 {
     return new QueryClrEnumType(this.EvaluationStrategy, enumType, null);
 }