#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;
namespace Google.Protobuf.Reflection
{
///
/// Descriptor for a single enum value within an enum in a .proto file.
///
public sealed class EnumValueDescriptor : DescriptorBase
{
internal EnumValueDescriptor(EnumValueDescriptorProto proto, FileDescriptor file,
EnumDescriptor parent, int index)
: base(file, parent.FullName + "." + proto.Name, index, parent.Features.MergedWith(proto.Options?.Features))
{
Proto = proto;
EnumDescriptor = parent;
file.DescriptorPool.AddSymbol(this);
file.DescriptorPool.AddEnumValue(this);
}
internal EnumValueDescriptorProto Proto { get; }
///
/// Returns a clone of the underlying describing this enum value.
/// 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 enum value descriptor.
public EnumValueDescriptorProto ToProto() => Proto.Clone();
///
/// Returns the name of the enum value described by this object.
///
public override string Name => Proto.Name;
///
/// Returns the number associated with this enum value.
///
public int Number => Proto.Number;
///
/// Returns the enum descriptor that this value is part of.
///
public EnumDescriptor EnumDescriptor { get; }
///
/// The (possibly empty) set of custom options for this enum value.
///
[Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
///
/// The EnumValueOptions, 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 EnumValueOptions 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 enum value 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 enum value option for this descriptor
///
[Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
public RepeatedField GetOption(RepeatedExtension extension)
{
return Proto.Options.GetExtension(extension).Clone();
}
}
}