• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using Google.Protobuf.Collections;
11 using System;
12 
13 namespace Google.Protobuf.Reflection
14 {
15     /// <summary>
16     /// Descriptor for a single enum value within an enum in a .proto file.
17     /// </summary>
18     public sealed class EnumValueDescriptor : DescriptorBase
19     {
EnumValueDescriptor(EnumValueDescriptorProto proto, FileDescriptor file, EnumDescriptor parent, int index)20         internal EnumValueDescriptor(EnumValueDescriptorProto proto, FileDescriptor file,
21                                      EnumDescriptor parent, int index)
22             : base(file, parent.FullName + "." + proto.Name, index, parent.Features.MergedWith(proto.Options?.Features))
23         {
24             Proto = proto;
25             EnumDescriptor = parent;
26             file.DescriptorPool.AddSymbol(this);
27             file.DescriptorPool.AddEnumValue(this);
28         }
29 
30         internal EnumValueDescriptorProto Proto { get; }
31 
32         /// <summary>
33         /// Returns a clone of the underlying <see cref="EnumValueDescriptorProto"/> describing this enum value.
34         /// Note that a copy is taken every time this method is called, so clients using it frequently
35         /// (and not modifying it) may want to cache the returned value.
36         /// </summary>
37         /// <returns>A protobuf representation of this enum value descriptor.</returns>
ToProto()38         public EnumValueDescriptorProto ToProto() => Proto.Clone();
39 
40         /// <summary>
41         /// Returns the name of the enum value described by this object.
42         /// </summary>
43         public override string Name => Proto.Name;
44 
45         /// <summary>
46         /// Returns the number associated with this enum value.
47         /// </summary>
48         public int Number => Proto.Number;
49 
50         /// <summary>
51         /// Returns the enum descriptor that this value is part of.
52         /// </summary>
53         public EnumDescriptor EnumDescriptor { get; }
54 
55         /// <summary>
56         /// The (possibly empty) set of custom options for this enum value.
57         /// </summary>
58         [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")]
59         public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
60 
61         /// <summary>
62         /// The <c>EnumValueOptions</c>, defined in <c>descriptor.proto</c>.
63         /// If the options message is not present (i.e. there are no options), <c>null</c> is returned.
64         /// Custom options can be retrieved as extensions of the returned message.
65         /// NOTE: A defensive copy is created each time this property is retrieved.
66         /// </summary>
GetOptions()67         public EnumValueOptions GetOptions()
68         {
69             var clone = Proto.Options?.Clone();
70             if (clone is null)
71             {
72                 return null;
73             }
74             // Clients should be using feature accessor methods, not accessing features on the
75             // options proto.
76             clone.Features = null;
77             return clone;
78         }
79 
80         /// <summary>
81         /// Gets a single value enum value option for this descriptor
82         /// </summary>
83         [Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
GetOption(Extension<EnumValueOptions, T> extension)84         public T GetOption<T>(Extension<EnumValueOptions, T> extension)
85         {
86             var value = Proto.Options.GetExtension(extension);
87             return value is IDeepCloneable<T> ? (value as IDeepCloneable<T>).Clone() : value;
88         }
89 
90         /// <summary>
91         /// Gets a repeated value enum value option for this descriptor
92         /// </summary>
93         [Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
GetOption(RepeatedExtension<EnumValueOptions, T> extension)94         public RepeatedField<T> GetOption<T>(RepeatedExtension<EnumValueOptions, T> extension)
95         {
96             return Proto.Options.GetExtension(extension).Clone();
97         }
98     }
99 }
100