xref: /aosp_15_r20/external/protobuf/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 // https://developers.google.com/protocol-buffers/
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 //     * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #endregion
32 
33 using Google.Protobuf.Collections;
34 using System;
35 using System.Collections.Generic;
36 
37 namespace Google.Protobuf.Reflection
38 {
39     /// <summary>
40     /// Descriptor for an enum type in a .proto file.
41     /// </summary>
42     public sealed class EnumDescriptor : DescriptorBase
43     {
44         private readonly EnumDescriptorProto proto;
45         private readonly MessageDescriptor containingType;
46         private readonly IList<EnumValueDescriptor> values;
47         private readonly Type clrType;
48 
EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type clrType)49         internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type clrType)
50             : base(file, file.ComputeFullName(parent, proto.Name), index)
51         {
52             this.proto = proto;
53             this.clrType = clrType;
54             containingType = parent;
55 
56             if (proto.Value.Count == 0)
57             {
58                 // We cannot allow enums with no values because this would mean there
59                 // would be no valid default value for fields of this type.
60                 throw new DescriptorValidationException(this, "Enums must contain at least one value.");
61             }
62 
63             values = DescriptorUtil.ConvertAndMakeReadOnly(proto.Value,
64                                                            (value, i) => new EnumValueDescriptor(value, file, this, i));
65 
66             File.DescriptorPool.AddSymbol(this);
67         }
68 
69         internal EnumDescriptorProto Proto { get { return proto; } }
70 
71         /// <summary>
72         /// Returns a clone of the underlying <see cref="EnumDescriptorProto"/> describing this enum.
73         /// Note that a copy is taken every time this method is called, so clients using it frequently
74         /// (and not modifying it) may want to cache the returned value.
75         /// </summary>
76         /// <returns>A protobuf representation of this enum descriptor.</returns>
ToProto()77         public EnumDescriptorProto ToProto() => Proto.Clone();
78 
79         /// <summary>
80         /// The brief name of the descriptor's target.
81         /// </summary>
82         public override string Name { get { return proto.Name; } }
83 
GetNestedDescriptorListForField(int fieldNumber)84         internal override IReadOnlyList<DescriptorBase> GetNestedDescriptorListForField(int fieldNumber)
85         {
86             switch (fieldNumber)
87             {
88                 case EnumDescriptorProto.ValueFieldNumber:
89                     return (IReadOnlyList<DescriptorBase>) Values;
90                 default:
91                     return null;
92             }
93         }
94 
95         /// <summary>
96         /// The CLR type for this enum. For generated code, this will be a CLR enum type.
97         /// </summary>
98         public Type ClrType { get { return clrType; } }
99 
100         /// <value>
101         /// If this is a nested type, get the outer descriptor, otherwise null.
102         /// </value>
103         public MessageDescriptor ContainingType
104         {
105             get { return containingType; }
106         }
107 
108         /// <value>
109         /// An unmodifiable list of defined value descriptors for this enum.
110         /// </value>
111         public IList<EnumValueDescriptor> Values
112         {
113             get { return values; }
114         }
115 
116         /// <summary>
117         /// Finds an enum value by number. If multiple enum values have the
118         /// same number, this returns the first defined value with that number.
119         /// If there is no value for the given number, this returns <c>null</c>.
120         /// </summary>
FindValueByNumber(int number)121         public EnumValueDescriptor FindValueByNumber(int number)
122         {
123             return File.DescriptorPool.FindEnumValueByNumber(this, number);
124         }
125 
126         /// <summary>
127         /// Finds an enum value by name.
128         /// </summary>
129         /// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
130         /// <returns>The value's descriptor, or null if not found.</returns>
FindValueByName(string name)131         public EnumValueDescriptor FindValueByName(string name)
132         {
133             return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
134         }
135 
136         /// <summary>
137         /// The (possibly empty) set of custom options for this enum.
138         /// </summary>
139         [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")]
140         public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
141 
142         /// <summary>
143         /// The <c>EnumOptions</c>, defined in <c>descriptor.proto</c>.
144         /// If the options message is not present (i.e. there are no options), <c>null</c> is returned.
145         /// Custom options can be retrieved as extensions of the returned message.
146         /// NOTE: A defensive copy is created each time this property is retrieved.
147         /// </summary>
GetOptions()148         public EnumOptions GetOptions() => Proto.Options?.Clone();
149 
150         /// <summary>
151         /// Gets a single value enum option for this descriptor
152         /// </summary>
153         [Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
GetOption(Extension<EnumOptions, T> extension)154         public T GetOption<T>(Extension<EnumOptions, T> extension)
155         {
156             var value = Proto.Options.GetExtension(extension);
157             return value is IDeepCloneable<T> ? (value as IDeepCloneable<T>).Clone() : value;
158         }
159 
160         /// <summary>
161         /// Gets a repeated value enum option for this descriptor
162         /// </summary>
163         [Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
GetOption(RepeatedExtension<EnumOptions, T> extension)164         public RepeatedField<T> GetOption<T>(RepeatedExtension<EnumOptions, T> extension)
165         {
166             return Proto.Options.GetExtension(extension).Clone();
167         }
168     }
169 }