Inheritance: global::ProtoBuf.IExtensible
Ejemplo n.º 1
0
        private void DumpDescriptor(DescriptorProto proto, FileDescriptorProto set, StringBuilder sb, int level)
        {
            PushDescriptorName(proto);

            string levelspace = new String('\t', level);

            sb.AppendLine(levelspace + "message " + proto.name + " {");

            foreach (var option in DumpOptions(proto.options))
            {
                sb.AppendLine(levelspace + "\toption " + option.Key + " = " + option.Value + ";");
            }

            foreach (DescriptorProto field in proto.nested_type)
            {
                DumpDescriptor(field, set, sb, level + 1);
            }

            DumpExtensionDescriptor(proto.extension, sb, levelspace + '\t');

            foreach (EnumDescriptorProto field in proto.enum_type)
            {
                DumpEnumDescriptor(field, sb, level + 1);
            }

            foreach (FieldDescriptorProto field in proto.field)
            {
                var enumLookup = new List<EnumDescriptorProto>();

                enumLookup.AddRange( set.enum_type ); // add global enums
                enumLookup.AddRange( proto.enum_type ); // add this message's nested enums

                sb.AppendLine(levelspace + "\t" + BuildDescriptorDeclaration(field));
            }

            if (proto.extension_range.Count > 0)
                sb.AppendLine();

            foreach (DescriptorProto.ExtensionRange range in proto.extension_range)
            {
                string max = Convert.ToString( range.end );

                // http://code.google.com/apis/protocolbuffers/docs/proto.html#extensions
                // If your numbering convention might involve extensions having very large numbers as tags, you can specify
                // that your extension range goes up to the maximum possible field number using the max keyword:
                // max is 2^29 - 1, or 536,870,911. 
                if ( range.end >= 536870911 )
                {
                    max = "max";
                }

                sb.AppendLine(levelspace + "\textensions " + range.start + " to " + max + ";");
            }

            sb.AppendLine(levelspace + "}");
            sb.AppendLine();

            PopDescriptorName();
        }
Ejemplo n.º 2
0
        private void DumpFileDescriptor(FileDescriptorProto set, StringBuilder sb)
        {
            if(!String.IsNullOrEmpty(set.package))
                PushDescriptorName(set);

            bool marker = false;

            foreach (string dependency in set.dependency)
            {
                sb.AppendLine("import \"" + dependency + "\";");
                marker = true;
            }

            if (marker)
            {
                sb.AppendLine();
                marker = false;
            }

            if (!string.IsNullOrEmpty(set.package))
            {
                sb.AppendLine("package " + set.package + ";");
                marker = true;
            }

            if (marker)
            {
                sb.AppendLine();
                marker = false;
            }

            foreach (var option in DumpOptions(set.options))
            {
                sb.AppendLine("option " + option.Key + " = " + option.Value + ";");
                marker = true;
            }

            if (marker)
            {
                sb.AppendLine();
                marker = false;
            }

            DumpExtensionDescriptor(set.extension, sb, String.Empty);

            foreach (EnumDescriptorProto field in set.enum_type)
            {
                DumpEnumDescriptor(field, sb, 0);
            }

            foreach (DescriptorProto proto in set.message_type)
            {
                DumpDescriptor(proto, set, sb, 0);
            }

            foreach (ServiceDescriptorProto service in set.service)
            {
                sb.AppendLine("service " + service.name + " {");

                foreach (var option in DumpOptions(service.options))
                {
                    sb.AppendLine("\toption " + option.Key + " = " + option.Value + ";");
                }

                foreach (MethodDescriptorProto method in service.method)
                {
                    string declaration = "\trpc " + method.name + " (" + method.input_type + ") returns (" + method.output_type + ")";

                    Dictionary<string, string> options = DumpOptions(method.options);

                    string parameters = String.Empty;
                    if (options.Count == 0)
                    {
                        sb.AppendLine(declaration + ";");
                    }
                    else
                    {
                        sb.AppendLine(declaration + " {");

                        foreach (var option in options)
                        {
                            sb.AppendLine("\t\toption " + option.Key + " = " + option.Value + ";");
                        }

                        sb.AppendLine("\t}");
                    }
                }

                sb.AppendLine("}");
            }

            if (!String.IsNullOrEmpty(set.package))
                PopDescriptorName();
        }
Ejemplo n.º 3
0
        private void DoParseFile(FileDescriptorProto set)
        {
            StringBuilder sb = new StringBuilder();

            DumpFileDescriptor(set, sb);

            FinalProtoDefinition.Add(new ProtoData { file = set, buffer = sb });
            ProtoList.Add(set.name);

            List<FileDescriptorProto> protosToRender = new List<FileDescriptorProto>();
            do
            {
                protosToRender.Clear();

                foreach (var proto in deferredProtos)
                {
                    if (!ShouldDeferProto(proto))
                        protosToRender.Add(proto);
                }

                foreach (var proto in protosToRender)
                {
                    deferredProtos.Remove(proto);
                    DoParseFile(proto);
                }
            }
            while (protosToRender.Count != 0);
        }
Ejemplo n.º 4
0
        private bool ShouldDeferProto(FileDescriptorProto set)
        {
            bool defer = false;
            foreach (string dependency in set.dependency)
            {
                if (!dependency.StartsWith("google") && !ProtoList.Contains(dependency))
                {
                    defer = true;
                    break;
                }
            }

            return defer;
        }
Ejemplo n.º 5
0
 private void PushDescriptorName(FileDescriptorProto file)
 {
     messageNameStack.Push(file.package);
 }
Ejemplo n.º 6
0
        private void DoParseFile(FileDescriptorProto set)
        {
            StringBuilder sb = new StringBuilder();

            DumpFileDescriptor(set, sb);

            FinalProtoDefinition.Add(new ProtoData { file = set, buffer = sb });
            ProtoList.Add(set.name);
        }