コード例 #1
0
        private void WriteEnum(IREnum e, TextWriter w, string prefix)
        {
            string reference = String.Format(_Reference, e.OriginalName);

            WriteComments(w, prefix, reference);

            // Type names are kept in PascalCase!
            w.WriteLine("{0}enum {1} {{", prefix, e.ShortName);

            if (ProtoHelper.HasEnumAlias(e))
            {
                w.WriteLine("{0}option allow_alias = true;", prefix + "\t");
            }

            // Write out all properties of the enum.
            foreach (IREnumProperty prop in e.Properties.OrderBy(prop => prop.Value))
            {
                // Enum property names are NOT converted to snake case!
                w.WriteLine("{0}{1} = {2};", prefix + "\t", prop.Name, prop.Value);
            }

            // End enum.
            w.WriteLine("{0}}}", prefix);
            w.WriteLine();
        }
コード例 #2
0
        // Returns true if the provided enum uses aliases inside the body.
        public static bool HasEnumAlias(IREnum enumType)
        {
            var propertyValues         = enumType.Properties.Select(x => x.Value);
            var propertyLength         = propertyValues.Count();
            var distinctPropertyLength = propertyValues.Distinct().Count();

            // TRUE if distinct length is different from normal length.
            return(propertyLength != distinctPropertyLength);
        }
コード例 #3
0
        private void WriteEnum(IREnum e, TextWriter w, string prefix)
        {
            string reference = String.Format(_Reference, e.OriginalName);

            WriteComments(w, prefix, reference);

            // Type names are kept in PascalCase!
            w.WriteLine("{0}enum {1} {{", prefix, e.ShortName);

            if (ProtoHelper.HasEnumAlias(e))
            {
                w.WriteLine("{0}option allow_alias = true;", prefix + "\t");
            }

            // Make a copy of all properties.
            var propList = e.Properties.ToList();
            // For enums, the default value is the first defined enum value, which must be 0.
            // Find or create that property with value 0
            IREnumProperty zeroProp;
            IEnumerable <IREnumProperty> zeroPropEnumeration = propList.Where(prop => prop.Value == 0);

            if (!zeroPropEnumeration.Any())
            {
                zeroProp = new IREnumProperty()
                {
                    // Enum values are all shared within the same namespace, so they must be
                    // unique within that namespace!
                    Name  = e.ShortName.ToUpper() + "_AUTO_INVALID",
                    Value = 0,
                };
            }
            else
            {
                zeroProp = zeroPropEnumeration.First();
                // And remove the property from the collection.
                propList.Remove(zeroProp);
            }

            // Write the zero property first - AS REQUIRED PER PROTO3!
            w.WriteLine("{0}{1} = {2};", prefix + "\t", zeroProp.Name, zeroProp.Value);

            // Write out the other properties of the enum next
            foreach (IREnumProperty prop in propList.OrderBy(prop => prop.Value))
            {
                // Enum property names are NOT converted to snake case!
                w.WriteLine("{0}{1} = {2};", prefix + "\t", prop.Name, prop.Value);
            }

            // End enum.
            w.WriteLine("{0}}}", prefix);
            w.WriteLine();
        }