示例#1
0
        public MessageRegistry(string descriptionFile)
        {
            FileDescriptorSet descriptorSet =
                FileDescriptorSet.ParseFrom(new FileStream(descriptionFile, FileMode.Open));

            foreach (FileDescriptorProto fdp in descriptorSet.FileList)
            {
                FileDescriptor fd = null;
                try
                {
                    fd = FileDescriptor.BuildFrom(fdp, new FileDescriptor[] { });
                }
                catch (DescriptorValidationException e)
                {
                    // TODO Auto-generated catch block
                    logger.Error(e);
                }

                foreach (MessageDescriptor descriptor in fd.MessageTypes)
                {
                    String className = fdp.Options.JavaPackage + "."
                                       + fdp.Options.JavaOuterClassname + "."
                                       + descriptor.Name;

                    Type type = ReflectionHelper.FindTypeFullSearch(className);

                    if (type != null)
                    {
                        mapping.Add(descriptor.FullName, type);
                    }
                }
            }
        }
        public void ImplicitFileClassWithDirectoryStructure()
        {
            FileDescriptorProto proto = new FileDescriptorProto.Builder {
                Name = "x/y/foo_bar"
            }.Build();
            FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);

            Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
        }
        public void ImplicitFileClassWithProtoDevelSuffix()
        {
            FileDescriptorProto proto = new FileDescriptorProto.Builder {
                Name = "foo_bar.protodevel"
            }.Build();
            FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);

            Assert.AreEqual("FooBar", descriptor.CSharpOptions.UmbrellaClassname);
        }
        public void NoNamespaceOrPackageFallsBackToEmptyString()
        {
            FileDescriptorProto proto = new FileDescriptorProto.Builder {
                Name = "x"
            }.Build();
            FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);

            Assert.AreEqual("", descriptor.CSharpOptions.Namespace);
        }
        public void ExplicitNamespace()
        {
            FileDescriptorProto proto = new FileDescriptorProto.Builder {
                Name = "x", Package = "pack", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpFileOptions,
                                                                                               new CSharpFileOptions.Builder {
                    Namespace = "Foo.Bar"
                }.Build()).Build()
            }.Build();
            FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);

            Assert.AreEqual("Foo.Bar", descriptor.CSharpOptions.Namespace);
        }
        public void ExplicitlyNamedFileClass()
        {
            FileDescriptorProto proto = new FileDescriptorProto.Builder {
                Name = "x", Options = new FileOptions.Builder().SetExtension(CSharpOptions.CSharpFileOptions,
                                                                             new CSharpFileOptions.Builder {
                    UmbrellaClassname = "Foo"
                }.Build()).Build()
            }.Build();
            FileDescriptor descriptor = FileDescriptor.BuildFrom(proto, null);

            Assert.AreEqual("Foo", descriptor.CSharpOptions.UmbrellaClassname);
        }
示例#7
0
        /// <summary>
        /// Resolves any dependencies and converts FileDescriptorProtos into FileDescriptors.
        /// The list returned is in the same order as the protos are listed in the descriptor set.
        /// Note: this method is internal rather than private to allow testing.
        /// </summary>
        /// <exception cref="DependencyResolutionException">Not all dependencies could be resolved.</exception>
        public static IList <FileDescriptor> ConvertDescriptors(CSharpFileOptions options,
                                                                params FileDescriptorSet[] descriptorProtos)
        {
            // Simple strategy: Keep going through the list of protos to convert, only doing ones where
            // we've already converted all the dependencies, until we get to a stalemate
            List <FileDescriptorProto> fileList = new List <FileDescriptorProto>();

            foreach (FileDescriptorSet set in descriptorProtos)
            {
                fileList.AddRange(set.FileList);
            }

            FileDescriptor[] converted = new FileDescriptor[fileList.Count];

            Dictionary <string, FileDescriptor> convertedMap = new Dictionary <string, FileDescriptor>();

            int totalConverted = 0;

            bool madeProgress = true;

            while (madeProgress && totalConverted < converted.Length)
            {
                madeProgress = false;
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] != null)
                    {
                        // Already done this one
                        continue;
                    }
                    FileDescriptorProto candidate    = fileList[i];
                    FileDescriptor[]    dependencies = new FileDescriptor[candidate.DependencyList.Count];


                    CSharpFileOptions.Builder builder = options.ToBuilder();
                    if (candidate.Options.HasExtension(CSharpOptions.CSharpFileOptions))
                    {
                        builder.MergeFrom(
                            candidate.Options.GetExtension(CSharpOptions.CSharpFileOptions));
                    }
                    CSharpFileOptions localOptions = builder.Build();

                    bool foundAllDependencies = true;
                    for (int j = 0; j < dependencies.Length; j++)
                    {
                        if (!convertedMap.TryGetValue(candidate.DependencyList[j], out dependencies[j]))
                        {
                            // We can auto-magically resolve these since we already have their description
                            // This way if the file is only referencing options it does not need to be built with the
                            // --include_imports definition.
                            if (localOptions.IgnoreGoogleProtobuf &&
                                (candidate.DependencyList[j] == "google/protobuf/csharp_options.proto"))
                            {
                                dependencies[j] = CSharpOptions.Descriptor;
                                continue;
                            }
                            if (localOptions.IgnoreGoogleProtobuf &&
                                (candidate.DependencyList[j] == "google/protobuf/descriptor.proto"))
                            {
                                dependencies[j] = DescriptorProtoFile.Descriptor;
                                continue;
                            }
                            foundAllDependencies = false;
                            break;
                        }
                    }
                    if (!foundAllDependencies)
                    {
                        continue;
                    }
                    madeProgress = true;
                    totalConverted++;
                    converted[i] = FileDescriptor.BuildFrom(candidate, dependencies);
                    convertedMap[candidate.Name] = converted[i];
                }
            }
            if (!madeProgress)
            {
                StringBuilder remaining = new StringBuilder();
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] == null)
                    {
                        if (remaining.Length != 0)
                        {
                            remaining.Append(", ");
                        }
                        FileDescriptorProto failure = fileList[i];
                        remaining.Append(failure.Name);
                        remaining.Append(":");
                        foreach (string dependency in failure.DependencyList)
                        {
                            if (!convertedMap.ContainsKey(dependency))
                            {
                                remaining.Append(" ");
                                remaining.Append(dependency);
                            }
                        }
                        remaining.Append(";");
                    }
                }
                throw new DependencyResolutionException("Unable to resolve all dependencies: " + remaining);
            }
            return(Lists.AsReadOnly(converted));
        }
示例#8
0
        /// <summary>
        /// Resolves any dependencies and converts FileDescriptorProtos into FileDescriptors.
        /// The list returned is in the same order as the protos are listed in the descriptor set.
        /// Note: this method is internal rather than private to allow testing.
        /// </summary>
        /// <exception cref="DependencyResolutionException">Not all dependencies could be resolved.</exception>
        internal static IList <FileDescriptor> ConvertDescriptors(FileDescriptorSet descriptorProtos)
        {
            // Simple strategy: Keep going through the list of protos to convert, only doing ones where
            // we've already converted all the dependencies, until we get to a stalemate
            IList <FileDescriptorProto> fileList = descriptorProtos.FileList;

            FileDescriptor[] converted = new FileDescriptor[fileList.Count];

            Dictionary <string, FileDescriptor> convertedMap = new Dictionary <string, FileDescriptor>();

            int totalConverted = 0;

            bool madeProgress = true;

            while (madeProgress && totalConverted < converted.Length)
            {
                madeProgress = false;
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] != null)
                    {
                        // Already done this one
                        continue;
                    }
                    FileDescriptorProto candidate    = fileList[i];
                    FileDescriptor[]    dependencies = new FileDescriptor[candidate.DependencyList.Count];
                    bool foundAllDependencies        = true;
                    for (int j = 0; j < dependencies.Length; j++)
                    {
                        if (!convertedMap.TryGetValue(candidate.DependencyList[j], out dependencies[j]))
                        {
                            foundAllDependencies = false;
                            break;
                        }
                    }
                    if (!foundAllDependencies)
                    {
                        continue;
                    }
                    madeProgress = true;
                    totalConverted++;
                    converted[i] = FileDescriptor.BuildFrom(candidate, dependencies);
                    convertedMap[candidate.Name] = converted[i];
                }
            }
            if (!madeProgress)
            {
                StringBuilder remaining = new StringBuilder();
                for (int i = 0; i < converted.Length; i++)
                {
                    if (converted[i] == null)
                    {
                        if (remaining.Length != 0)
                        {
                            remaining.Append(", ");
                        }
                        FileDescriptorProto failure = fileList[i];
                        remaining.Append(failure.Name);
                        remaining.Append(":");
                        foreach (string dependency in failure.DependencyList)
                        {
                            if (!convertedMap.ContainsKey(dependency))
                            {
                                remaining.Append(" ");
                                remaining.Append(dependency);
                            }
                        }
                        remaining.Append(";");
                    }
                }
                throw new DependencyResolutionException("Unable to resolve all dependencies: " + remaining);
            }
            return(Lists.AsReadOnly(converted));
        }