#region Copyright notice and license // Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd #endregion using Google.Protobuf.Collections; using System; using System.Collections.Generic; namespace Google.Protobuf.Reflection { /// /// Describes a service type. /// public sealed class ServiceDescriptor : DescriptorBase { internal ServiceDescriptor(ServiceDescriptorProto proto, FileDescriptor file, int index) : base(file, file.ComputeFullName(null, proto.Name), index, file.Features.MergedWith(proto.Options?.Features)) { Proto = proto; Methods = DescriptorUtil.ConvertAndMakeReadOnly(proto.Method, (method, i) => new MethodDescriptor(method, file, this, i)); file.DescriptorPool.AddSymbol(this); } /// /// The brief name of the descriptor's target. /// public override string Name => Proto.Name; internal override IReadOnlyList GetNestedDescriptorListForField(int fieldNumber) => fieldNumber switch { ServiceDescriptorProto.MethodFieldNumber => (IReadOnlyList)Methods, _ => null, }; internal ServiceDescriptorProto Proto { get; } /// /// Returns a clone of the underlying describing this service. /// Note that a copy is taken every time this method is called, so clients using it frequently /// (and not modifying it) may want to cache the returned value. /// /// A protobuf representation of this service descriptor. public ServiceDescriptorProto ToProto() => Proto.Clone(); /// /// An unmodifiable list of methods in this service. /// public IList Methods { get; } /// /// Finds a method by name. /// /// The unqualified name of the method (e.g. "Foo"). /// The method's descriptor, or null if not found. public MethodDescriptor FindMethodByName(string name) => File.DescriptorPool.FindSymbol(FullName + "." + name); /// /// The (possibly empty) set of custom options for this service. /// [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// The ServiceOptions, defined in descriptor.proto. /// If the options message is not present (i.e. there are no options), null is returned. /// Custom options can be retrieved as extensions of the returned message. /// NOTE: A defensive copy is created each time this property is retrieved. /// public ServiceOptions GetOptions() { var clone = Proto.Options?.Clone(); if (clone is null) { return null; } // Clients should be using feature accessor methods, not accessing features on the // options proto. clone.Features = null; return clone; } /// /// Gets a single value service option for this descriptor /// [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); return value is IDeepCloneable ? (value as IDeepCloneable).Clone() : value; } /// /// Gets a repeated value service option for this descriptor /// [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); } internal void CrossLink() { foreach (MethodDescriptor method in Methods) { method.CrossLink(); } } } }