diff options
author | Jon Skeet <jonskeet@google.com> | 2018-08-30 14:53:06 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2018-09-22 09:09:15 +0100 |
commit | 1711999078ca1d435de3958bf963e95a742e972f (patch) | |
tree | 6ea6797a824da7cf26b2535ea4e3a9be9cd5ec95 /csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs | |
parent | a6e1cc7e328c45a0cb9856c530c8f6cd23314163 (diff) | |
download | protobuf-1711999078ca1d435de3958bf963e95a742e972f.tar.gz protobuf-1711999078ca1d435de3958bf963e95a742e972f.tar.bz2 protobuf-1711999078ca1d435de3958bf963e95a742e972f.zip |
Provide simple access to descriptor declarations in C#
This is primarily for access to comments, which would be expected to be available in a protoc plugin.
The implementation has two fiddly aspects:
- We use a Lazy<T> to avoid building the map before cross-linking. An alternative would be to crosslink at the end of the constructor, and remove the calls to CrossLink elsewhere. This would be generally better IMO, but deviate from the Java code.
- The casts to IReadOnlyList<DescriptorBase> are unfortunate. They'll always work, because these lists are always ReadOnlyCollection<T> for a descriptor type... but we can't use IList<DescriptorBase> as that's not covariant, and it's annoyingly fiddly to change the field to be of type ReadOnlyCollection<T>.
Diffstat (limited to 'csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs')
-rw-r--r-- | csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs b/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs index 194041a8..bfbebf17 100644 --- a/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs +++ b/csharp/src/Google.Protobuf/Reflection/DescriptorBase.cs @@ -30,6 +30,8 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using System.Collections.Generic; + namespace Google.Protobuf.Reflection { /// <summary> @@ -37,15 +39,11 @@ namespace Google.Protobuf.Reflection /// </summary> public abstract class DescriptorBase : IDescriptor { - private readonly FileDescriptor file; - private readonly string fullName; - private readonly int index; - internal DescriptorBase(FileDescriptor file, string fullName, int index) { - this.file = file; - this.fullName = fullName; - this.index = index; + File = file; + FullName = fullName; + Index = index; } /// <value> @@ -56,10 +54,7 @@ namespace Google.Protobuf.Reflection /// this descriptor's type. (There can be duplicate values for different /// types, e.g. one enum type with index 0 and one message type with index 0.) /// </remarks> - public int Index - { - get { return index; } - } + public int Index { get; } /// <summary> /// Returns the name of the entity (field, message etc) being described. @@ -69,17 +64,29 @@ namespace Google.Protobuf.Reflection /// <summary> /// The fully qualified name of the descriptor's target. /// </summary> - public string FullName - { - get { return fullName; } - } + public string FullName { get; } /// <value> /// The file this descriptor was declared in. /// </value> - public FileDescriptor File - { - get { return file; } - } + public FileDescriptor File { get; } + + /// <summary> + /// The declaration information about the descriptor, or null if no declaration information + /// is available for this descriptor. + /// </summary> + /// <remarks> + /// This information is typically only available for dynamically loaded descriptors, + /// for example within a protoc plugin where the full descriptors, including source info, + /// are passed to the code by protoc. + /// </remarks> + public DescriptorDeclaration Declaration => File.GetDeclaration(this); + + /// <summary> + /// Retrieves the list of nested descriptors corresponding to the given field number, if any. + /// If the field is unknown or not a nested descriptor list, return null to terminate the search. + /// The default implementation returns null. + /// </summary> + internal virtual IReadOnlyList<DescriptorBase> GetNestedDescriptorListForField(int fieldNumber) => null; } }
\ No newline at end of file |