blob: 5906c2e36d9cfcb9205410f3aaab139a0f4ff31d [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2015 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
33using System.Collections.Generic;
34using System.Collections.ObjectModel;
35using Google.Protobuf.Compatibility;
36
37namespace Google.Protobuf.Reflection
38{
39 /// <summary>
40 /// Describes a "oneof" field collection in a message type: a set of
41 /// fields of which at most one can be set in any particular message.
42 /// </summary>
43 public sealed class OneofDescriptor : DescriptorBase
44 {
45 private readonly OneofDescriptorProto proto;
46 private MessageDescriptor containingType;
47 private IList<FieldDescriptor> fields;
48 private readonly OneofAccessor accessor;
49
50 internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string clrName)
51 : base(file, file.ComputeFullName(parent, proto.Name), index)
52 {
53 this.proto = proto;
54 containingType = parent;
55
56 file.DescriptorPool.AddSymbol(this);
57 accessor = CreateAccessor(clrName);
58 }
59
60 /// <summary>
61 /// The brief name of the descriptor's target.
62 /// </summary>
63 public override string Name { get { return proto.Name; } }
64
65 /// <summary>
66 /// Gets the message type containing this oneof.
67 /// </summary>
68 /// <value>
69 /// The message type containing this oneof.
70 /// </value>
71 public MessageDescriptor ContainingType
72 {
73 get { return containingType; }
74 }
75
76 /// <summary>
77 /// Gets the fields within this oneof, in declaration order.
78 /// </summary>
79 /// <value>
80 /// The fields within this oneof, in declaration order.
81 /// </value>
82 public IList<FieldDescriptor> Fields { get { return fields; } }
83
84 /// <summary>
85 /// Gets an accessor for reflective access to the values associated with the oneof
86 /// in a particular message.
87 /// </summary>
88 /// <value>
89 /// The accessor used for reflective access.
90 /// </value>
91 public OneofAccessor Accessor { get { return accessor; } }
92
Austin Schuh40c16522018-10-28 20:27:54 -070093 /// <summary>
94 /// The (possibly empty) set of custom options for this oneof.
95 /// </summary>
96 public CustomOptions CustomOptions => proto.Options?.CustomOptions ?? CustomOptions.Empty;
97
Brian Silverman9c614bc2016-02-15 20:20:02 -050098 internal void CrossLink()
99 {
100 List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>();
101 foreach (var field in ContainingType.Fields.InDeclarationOrder())
102 {
103 if (field.ContainingOneof == this)
104 {
105 fieldCollection.Add(field);
106 }
107 }
108 fields = new ReadOnlyCollection<FieldDescriptor>(fieldCollection);
109 }
110
111 private OneofAccessor CreateAccessor(string clrName)
112 {
113 var caseProperty = containingType.ClrType.GetProperty(clrName + "Case");
114 if (caseProperty == null)
115 {
116 throw new DescriptorValidationException(this, $"Property {clrName}Case not found in {containingType.ClrType}");
117 }
118 var clearMethod = containingType.ClrType.GetMethod("Clear" + clrName);
119 if (clearMethod == null)
120 {
121 throw new DescriptorValidationException(this, $"Method Clear{clrName} not found in {containingType.ClrType}");
122 }
123
124 return new OneofAccessor(caseProperty, clearMethod, this);
125 }
126 }
127}