Helper class to control indentation. Used for TextFormat and by ProtoGen.
Exemple #1
0
 /// <summary>
 /// Outputs a textual representation of the Protocol Message builder supplied into
 /// the parameter output.
 /// </summary>
 public static void Print(IBuilder builder, TextWriter output)
 {
     TextGenerator generator = new TextGenerator(output, "\n");
     Print(builder, generator);
 }
Exemple #2
0
 /// <summary>
 /// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>.
 /// </summary>
 public static void Print(UnknownFieldSet fields, TextWriter output)
 {
     TextGenerator generator = new TextGenerator(output, "\n");
     PrintUnknownFields(fields, generator);
 }
Exemple #3
0
        private static void PrintUnknownFields(UnknownFieldSet unknownFields, TextGenerator generator)
        {
            foreach (KeyValuePair<int, UnknownField> entry in unknownFields.FieldDictionary)
            {
                String prefix = entry.Key.ToString() + ": ";
                UnknownField field = entry.Value;

                foreach (ulong value in field.VarintList)
                {
                    generator.Print(prefix);
                    generator.Print(value.ToString());
                    generator.Print("\n");
                }
                foreach (uint value in field.Fixed32List)
                {
                    generator.Print(prefix);
                    generator.Print(string.Format("0x{0:x8}", value));
                    generator.Print("\n");
                }
                foreach (ulong value in field.Fixed64List)
                {
                    generator.Print(prefix);
                    generator.Print(string.Format("0x{0:x16}", value));
                    generator.Print("\n");
                }
                foreach (ByteString value in field.LengthDelimitedList)
                {
                    generator.Print(entry.Key.ToString());
                    generator.Print(": \"");
                    generator.Print(EscapeBytes(value));
                    generator.Print("\"\n");
                }
                foreach (UnknownFieldSet value in field.GroupList)
                {
                    generator.Print(entry.Key.ToString());
                    generator.Print(" {\n");
                    generator.Indent();
                    PrintUnknownFields(value, generator);
                    generator.Outdent();
                    generator.Print("}\n");
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// Outputs a textual representation of the Protocol Message supplied into
 /// the parameter output.
 /// </summary>
 public static void Print(IMessage message, TextWriter output)
 {
     TextGenerator generator = new TextGenerator(output, "\n");
     Print(message, generator);
 }
Exemple #5
0
        private static void PrintSingleField(FieldDescriptor field, Object value, TextGenerator generator)
        {
            if (field.IsExtension)
            {
                generator.Print("[");
                // We special-case MessageSet elements for compatibility with proto1.
                if (field.ContainingType.Options.MessageSetWireFormat
                    && field.FieldType == FieldType.Message
                    && field.IsOptional
                    // object equality (TODO(jonskeet): Work out what this comment means!)
                    && field.ExtensionScope == field.MessageType)
                {
                    generator.Print(field.MessageType.FullName);
                }
                else
                {
                    generator.Print(field.FullName);
                }
                generator.Print("]");
            }
            else
            {
                if (field.FieldType == FieldType.Group)
                {
                    // Groups must be serialized with their original capitalization.
                    generator.Print(field.MessageType.Name);
                }
                else
                {
                    generator.Print(field.Name);
                }
            }

            if (field.MappedType == MappedType.Message)
            {
                generator.Print(" {\n");
                generator.Indent();
            }
            else
            {
                generator.Print(": ");
            }

            PrintFieldValue(field, value, generator);

            if (field.MappedType == MappedType.Message)
            {
                generator.Outdent();
                generator.Print("}");
            }
            generator.Print("\n");
        }
Exemple #6
0
        private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator)
        {
            switch (field.FieldType)
            {
                    // The Float and Double types must specify the "r" format to preserve their precision, otherwise,
                    // the double to/from string will trim the precision to 6 places.  As with other numeric formats
                    // below, always use the invariant culture so it's predictable.
                case FieldType.Float:
                    generator.Print(((float)value).ToString("r", FrameworkPortability.InvariantCulture));
                    break;
                case FieldType.Double:
                    generator.Print(((double)value).ToString("r", FrameworkPortability.InvariantCulture));
                    break;

                case FieldType.Int32:
                case FieldType.Int64:
                case FieldType.SInt32:
                case FieldType.SInt64:
                case FieldType.SFixed32:
                case FieldType.SFixed64:
                case FieldType.UInt32:
                case FieldType.UInt64:
                case FieldType.Fixed32:
                case FieldType.Fixed64:
                    // The simple Object.ToString converts using the current culture.
                    // We want to always use the invariant culture so it's predictable.
                    generator.Print(((IConvertible)value).ToString(FrameworkPortability.InvariantCulture));
                    break;
                case FieldType.Bool:
                    // Explicitly use the Java true/false
                    generator.Print((bool) value ? "true" : "false");
                    break;

                case FieldType.String:
                    generator.Print("\"");
                    generator.Print(EscapeText((string) value));
                    generator.Print("\"");
                    break;

                case FieldType.Bytes:
                    {
                        generator.Print("\"");
                        generator.Print(EscapeBytes((ByteString) value));
                        generator.Print("\"");
                        break;
                    }

                case FieldType.Enum:
                    {
                        if (value is IEnumLite && !(value is EnumValueDescriptor))
                        {
                            throw new NotSupportedException("Lite enumerations are not supported.");
                        }
                        generator.Print(((EnumValueDescriptor) value).Name);
                        break;
                    }

                case FieldType.Message:
                case FieldType.Group:
                    if (value is IMessageLite && !(value is IMessage))
                    {
                        throw new NotSupportedException("Lite messages are not supported.");
                    }
                    Print((IMessage) value, generator);
                    break;
            }
        }
Exemple #7
0
 private static void Print(IBuilder message, TextGenerator generator)
 {
     foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields)
     {
         PrintField(entry.Key, entry.Value, generator);
     }
     PrintUnknownFields(message.UnknownFields, generator);
 }
Exemple #8
0
 internal static void PrintField(FieldDescriptor field, object value, TextGenerator generator)
 {
     if (field.IsRepeated)
     {
         // Repeated field.  Print each element.
         foreach (object element in (IEnumerable) value)
         {
             PrintSingleField(field, element, generator);
         }
     }
     else
     {
         PrintSingleField(field, value, generator);
     }
 }
        private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator)
        {
            switch (field.FieldType) {
            case FieldType.Int32:
            case FieldType.Int64:
            case FieldType.SInt32:
            case FieldType.SInt64:
            case FieldType.SFixed32:
            case FieldType.SFixed64:
            case FieldType.Float:
            case FieldType.Double:
            case FieldType.UInt32:
            case FieldType.UInt64:
            case FieldType.Fixed32:
            case FieldType.Fixed64:
              // The simple Object.ToString converts using the current culture.
              // We want to always use the invariant culture so it's predictable.
              generator.Print(((IConvertible) value).ToString(CultureInfo.InvariantCulture));
              break;
            case FieldType.Bool:
              // Explicitly use the Java true/false
              generator.Print((bool) value ? "true" : "false");
              break;

            case FieldType.String:
              generator.Print("\"");
              generator.Print(EscapeText((string) value));
              generator.Print("\"");
              break;

            case FieldType.Bytes: {
              generator.Print("\"");
              generator.Print(EscapeBytes((ByteString) value));
              generator.Print("\"");
              break;
            }

            case FieldType.Enum: {
              generator.Print(((EnumValueDescriptor) value).Name);
              break;
            }

            case FieldType.Message:
            case FieldType.Group:
              Print((IMessage) value, generator);
              break;
              }
        }
Exemple #10
0
        /// <summary>
        /// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>.
        /// </summary>
        public static void Print(UnknownFieldSet fields, TextWriter output)
        {
            TextGenerator generator = new TextGenerator(output, "\n");

            PrintUnknownFields(fields, generator);
        }
Exemple #11
0
        /// <summary>
        /// Outputs a textual representation of the Protocol Message builder supplied into
        /// the parameter output.
        /// </summary>
        public static void Print(IBuilder builder, TextWriter output)
        {
            TextGenerator generator = new TextGenerator(output, "\n");

            Print(builder, generator);
        }
Exemple #12
0
        /// <summary>
        /// Outputs a textual representation of the Protocol Message supplied into
        /// the parameter output.
        /// </summary>
        public static void Print(IMessage message, TextWriter output)
        {
            TextGenerator generator = new TextGenerator(output, "\n");

            Print(message, generator);
        }
Exemple #13
0
        private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator)
        {
            switch (field.FieldType)
            {
            // The Float and Double types must specify the "r" format to preserve their precision, otherwise,
            // the double to/from string will trim the precision to 6 places.  As with other numeric formats
            // below, always use the invariant culture so it's predictable.
            case FieldType.Float:
                generator.Print(((float)value).ToString("r", FrameworkPortability.InvariantCulture));
                break;

            case FieldType.Double:
                generator.Print(((double)value).ToString("r", FrameworkPortability.InvariantCulture));
                break;

            case FieldType.Int32:
            case FieldType.Int64:
            case FieldType.SInt32:
            case FieldType.SInt64:
            case FieldType.SFixed32:
            case FieldType.SFixed64:
            case FieldType.UInt32:
            case FieldType.UInt64:
            case FieldType.Fixed32:
            case FieldType.Fixed64:
                // The simple Object.ToString converts using the current culture.
                // We want to always use the invariant culture so it's predictable.
                generator.Print(((IConvertible)value).ToString(FrameworkPortability.InvariantCulture));
                break;

            case FieldType.Bool:
                // Explicitly use the Java true/false
                generator.Print((bool)value ? "true" : "false");
                break;

            case FieldType.String:
                generator.Print("\"");
                generator.Print(EscapeText((string)value));
                generator.Print("\"");
                break;

            case FieldType.Bytes:
            {
                generator.Print("\"");
                generator.Print(EscapeBytes((ByteString)value));
                generator.Print("\"");
                break;
            }

            case FieldType.Enum:
            {
                if (value is IEnumLite && !(value is EnumValueDescriptor))
                {
                    throw new NotSupportedException("Lite enumerations are not supported.");
                }
                generator.Print(((EnumValueDescriptor)value).Name);
                break;
            }

            case FieldType.Message:
            case FieldType.Group:
                if (value is IMessageLite && !(value is IMessage))
                {
                    throw new NotSupportedException("Lite messages are not supported.");
                }
                Print((IMessage)value, generator);
                break;
            }
        }