#region Copyright notice and license // Copyright 2015, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion using System.Collections.Generic; using Grpc.Core.Utils; using Google.Protobuf.Reflection; namespace Grpc.Reflection { /// Registry of protobuf symbols public class SymbolRegistry { private readonly Dictionary filesByName; private readonly Dictionary filesBySymbol; private SymbolRegistry(Dictionary filesByName, Dictionary filesBySymbol) { this.filesByName = new Dictionary(filesByName); this.filesBySymbol = new Dictionary(filesBySymbol); } /// /// Creates a symbol registry from the specified set of file descriptors. /// /// The set of files to include in the registry. Must not contain null values. /// A symbol registry for the given files. public static SymbolRegistry FromFiles(IEnumerable fileDescriptors) { GrpcPreconditions.CheckNotNull(fileDescriptors); var builder = new Builder(); foreach (var file in fileDescriptors) { builder.AddFile(file); } return builder.Build(); } /// /// Gets file descriptor for given file name (including package path). Returns null if not found. /// public FileDescriptor FileByName(string filename) { FileDescriptor file; filesByName.TryGetValue(filename, out file); return file; } /// /// Gets file descriptor that contains definition of given symbol full name (including package path). Returns null if not found. /// public FileDescriptor FileContainingSymbol(string symbol) { FileDescriptor file; filesBySymbol.TryGetValue(symbol, out file); return file; } /// /// Builder class which isn't exposed, but acts as a convenient alternative to passing round two dictionaries in recursive calls. /// private class Builder { private readonly Dictionary filesByName; private readonly Dictionary filesBySymbol; internal Builder() { filesByName = new Dictionary(); filesBySymbol = new Dictionary(); } internal void AddFile(FileDescriptor fileDescriptor) { if (filesByName.ContainsKey(fileDescriptor.Name)) { return; } filesByName.Add(fileDescriptor.Name, fileDescriptor); foreach (var dependency in fileDescriptor.Dependencies) { AddFile(dependency); } foreach (var enumeration in fileDescriptor.EnumTypes) { AddEnum(enumeration); } foreach (var message in fileDescriptor.MessageTypes) { AddMessage(message); } foreach (var service in fileDescriptor.Services) { AddService(service); } } private void AddEnum(EnumDescriptor enumDescriptor) { filesBySymbol[enumDescriptor.FullName] = enumDescriptor.File; } private void AddMessage(MessageDescriptor messageDescriptor) { foreach (var nestedEnum in messageDescriptor.EnumTypes) { AddEnum(nestedEnum); } foreach (var nestedType in messageDescriptor.NestedTypes) { AddMessage(nestedType); } filesBySymbol[messageDescriptor.FullName] = messageDescriptor.File; } private void AddService(ServiceDescriptor serviceDescriptor) { foreach (var method in serviceDescriptor.Methods) { filesBySymbol[method.FullName] = method.File; } filesBySymbol[serviceDescriptor.FullName] = serviceDescriptor.File; } internal SymbolRegistry Build() { return new SymbolRegistry(filesByName, filesBySymbol); } } } }