diff options
author | Jon Skeet <skeet@pobox.com> | 2008-11-13 22:29:48 +0000 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-11-13 22:29:48 +0000 |
commit | 1d131c98f058dc0b5e0ebef73602e09dc88fe075 (patch) | |
tree | e94ff4c7383c557902ec5eb63b8889dda24e3bd7 /src/ProtocolBuffers/Descriptors/FieldDescriptor.cs | |
parent | d6343be707cb6a067fe2b5ccc2efd7848072d17c (diff) | |
download | protobuf-1d131c98f058dc0b5e0ebef73602e09dc88fe075.tar.gz protobuf-1d131c98f058dc0b5e0ebef73602e09dc88fe075.tar.bz2 protobuf-1d131c98f058dc0b5e0ebef73602e09dc88fe075.zip |
New options now work fine.
Diffstat (limited to 'src/ProtocolBuffers/Descriptors/FieldDescriptor.cs')
-rw-r--r-- | src/ProtocolBuffers/Descriptors/FieldDescriptor.cs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs b/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs index 5ff55197..0d0fad76 100644 --- a/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs +++ b/src/ProtocolBuffers/Descriptors/FieldDescriptor.cs @@ -50,6 +50,9 @@ namespace Google.ProtocolBuffers.Descriptors { private FieldType fieldType; private MappedType mappedType; + private CSharpFieldOptions csharpFieldOptions; + private readonly object optionsLock = new object(); + internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, bool isExtension) : base(proto, file, ComputeFullName(file, parent, proto.Name), index) { @@ -87,6 +90,31 @@ namespace Google.ProtocolBuffers.Descriptors { file.DescriptorPool.AddSymbol(this); } + private CSharpFieldOptions BuildOrFakeCSharpOptions() { + // TODO(jonskeet): Check if we could use FileDescriptorProto.Descriptor.Name - interesting bootstrap issues + if (File.Proto.Name == "google/protobuf/csharp_options.proto") { + if (Name=="csharp_field_options") { + return new CSharpFieldOptions.Builder { PropertyName = "CSharpFieldOptions" }.Build(); + } + if (Name=="csharp_file_options") { + return new CSharpFieldOptions.Builder { PropertyName = "CSharpFileOptions" }.Build(); + } + } + CSharpFieldOptions.Builder builder = CSharpFieldOptions.CreateBuilder(); + if (Proto.Options.HasExtension(DescriptorProtos.CSharpOptions.CSharpFieldOptions)) { + builder.MergeFrom(Proto.Options.GetExtension(DescriptorProtos.CSharpOptions.CSharpFieldOptions)); + } + if (!builder.HasPropertyName) { + string fieldName = FieldType == FieldType.Group ? MessageType.Name : Name; + string propertyName = NameHelpers.UnderscoresToPascalCase(fieldName); + if (propertyName == ContainingType.Name) { + propertyName += "_"; + } + builder.PropertyName = propertyName; + } + return builder.Build(); + } + /// <summary> /// Maps a field type as included in the .proto file to a FieldType. /// </summary> @@ -191,6 +219,21 @@ namespace Google.ProtocolBuffers.Descriptors { public MessageDescriptor ContainingType { get { return containingType; } } + + /// <summary> + /// Returns the C#-specific options for this file descriptor. This will always be + /// completely filled in. + /// </summary> + public CSharpFieldOptions CSharpOptions { + get { + lock (optionsLock) { + if (csharpFieldOptions == null) { + csharpFieldOptions = BuildOrFakeCSharpOptions(); + } + } + return csharpFieldOptions; + } + } /// <summary> /// For extensions defined nested within message types, gets |