Пример #1
0
        public static void AppendProto(StringBuilder sb, int nestLevel)
        {
            string descText, name = Serializer.GetDefinedTypeName <T>();

            sb.AppendLine();
            Indent(sb, nestLevel).Append("message ").Append(name).Append(" {");

            DescriptionAttribute desc = AttributeUtils.GetAttribute <DescriptionAttribute>(typeof(T));

            descText = desc == null ? null : desc.Description;
            if (!string.IsNullOrEmpty(descText))
            {
                sb.Append(" //").Append(descText); // TODO: remove crlf
            }

            sb.AppendLine();
            nestLevel++;
            for (int i = 0; i < writeProps.Length; i++)
            {
                Property <T> prop = writeProps[i];
                Indent(sb, nestLevel).Append(' ')
                .Append(prop.IsRepeated ? "repeated" :
                        (prop.IsOptional ? "optional" : "required"))
                .Append(prop.IsGroup ? " group " : " ")
                .Append(prop.DefinedType).Append(' ')
                .Append(prop.Name).Append(" = ").Append(prop.Tag);

                object def = prop.DefaultValue;
                if (def != null)
                {
                    string defText = Convert.ToString(def, CultureInfo.InvariantCulture);
                    if (prop.DefinedType == ProtoFormat.STRING)
                    {
                        // note; waiting on clarification over quote escape rules
                        sb.Append(" [default = \"").Append(defText).Append("\"]");
                    }
                    else
                    {
                        sb.Append(" [default = ").Append(defText).Append("]");
                    }
                }

                sb.Append(";");
                descText = prop.Description;
                if (!string.IsNullOrEmpty(descText))
                {
                    sb.Append(" //").Append(descText); // TODO: remove crlf
                }

                sb.AppendLine();
            }

            nestLevel--;
            Indent(sb, nestLevel).AppendLine("}");
        }