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 36 namespace Google.Protobuf.Reflection 37 { 38 /// <summary> 39 /// Describes a single method in a service. 40 /// </summary> 41 public sealed class MethodDescriptor : DescriptorBase 42 { 43 private readonly MethodDescriptorProto proto; 44 private readonly ServiceDescriptor service; 45 private MessageDescriptor inputType; 46 private MessageDescriptor outputType; 47 48 /// <value> 49 /// The service this method belongs to. 50 /// </value> 51 public ServiceDescriptor Service { get { return service; } } 52 53 /// <value> 54 /// The method's input type. 55 /// </value> 56 public MessageDescriptor InputType { get { return inputType; } } 57 58 /// <value> 59 /// The method's input type. 60 /// </value> 61 public MessageDescriptor OutputType { get { return outputType; } } 62 63 /// <value> 64 /// Indicates if client streams multiple requests. 65 /// </value> 66 public bool IsClientStreaming { get { return proto.ClientStreaming; } } 67 68 /// <value> 69 /// Indicates if server streams multiple responses. 70 /// </value> 71 public bool IsServerStreaming { get { return proto.ServerStreaming; } } 72 73 /// <summary> 74 /// The (possibly empty) set of custom options for this method. 75 /// </summary> 76 [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] 77 public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); 78 79 /// <summary> 80 /// The <c>MethodOptions</c>, defined in <c>descriptor.proto</c>. 81 /// If the options message is not present (i.e. there are no options), <c>null</c> is returned. 82 /// Custom options can be retrieved as extensions of the returned message. 83 /// NOTE: A defensive copy is created each time this property is retrieved. 84 /// </summary> GetOptions()85 public MethodOptions GetOptions() => Proto.Options?.Clone(); 86 87 /// <summary> 88 /// Gets a single value method option for this descriptor 89 /// </summary> 90 [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] GetOption(Extension<MethodOptions, T> extension)91 public T GetOption<T>(Extension<MethodOptions, T> extension) 92 { 93 var value = Proto.Options.GetExtension(extension); 94 return value is IDeepCloneable<T> ? (value as IDeepCloneable<T>).Clone() : value; 95 } 96 97 /// <summary> 98 /// Gets a repeated value method option for this descriptor 99 /// </summary> 100 [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] GetOption(RepeatedExtension<MethodOptions, T> extension)101 public RepeatedField<T> GetOption<T>(RepeatedExtension<MethodOptions, T> extension) 102 { 103 return Proto.Options.GetExtension(extension).Clone(); 104 } 105 MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file, ServiceDescriptor parent, int index)106 internal MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file, 107 ServiceDescriptor parent, int index) 108 : base(file, parent.FullName + "." + proto.Name, index) 109 { 110 this.proto = proto; 111 service = parent; 112 file.DescriptorPool.AddSymbol(this); 113 } 114 115 internal MethodDescriptorProto Proto { get { return proto; } } 116 117 /// <summary> 118 /// Returns a clone of the underlying <see cref="MethodDescriptorProto"/> describing this method. 119 /// Note that a copy is taken every time this method is called, so clients using it frequently 120 /// (and not modifying it) may want to cache the returned value. 121 /// </summary> 122 /// <returns>A protobuf representation of this method descriptor.</returns> ToProto()123 public MethodDescriptorProto ToProto() => Proto.Clone(); 124 125 /// <summary> 126 /// The brief name of the descriptor's target. 127 /// </summary> 128 public override string Name { get { return proto.Name; } } 129 CrossLink()130 internal void CrossLink() 131 { 132 IDescriptor lookup = File.DescriptorPool.LookupSymbol(Proto.InputType, this); 133 if (!(lookup is MessageDescriptor)) 134 { 135 throw new DescriptorValidationException(this, "\"" + Proto.InputType + "\" is not a message type."); 136 } 137 inputType = (MessageDescriptor) lookup; 138 139 lookup = File.DescriptorPool.LookupSymbol(Proto.OutputType, this); 140 if (!(lookup is MessageDescriptor)) 141 { 142 throw new DescriptorValidationException(this, "\"" + Proto.OutputType + "\" is not a message type."); 143 } 144 outputType = (MessageDescriptor) lookup; 145 } 146 } 147 } 148