aboutsummaryrefslogtreecommitdiff
path: root/src/ProtoGen/ExtensionGenerator.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ProtoGen/ExtensionGenerator.cs')
-rw-r--r--src/ProtoGen/ExtensionGenerator.cs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/ProtoGen/ExtensionGenerator.cs b/src/ProtoGen/ExtensionGenerator.cs
new file mode 100644
index 00000000..9c235456
--- /dev/null
+++ b/src/ProtoGen/ExtensionGenerator.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Google.ProtocolBuffers.Descriptors;
+
+namespace Google.ProtocolBuffers.ProtoGen {
+ internal class ExtensionGenerator : SourceGeneratorBase<FieldDescriptor>, ISourceGenerator {
+ internal ExtensionGenerator(FieldDescriptor descriptor) : base(descriptor) {
+ }
+
+ public void Generate(TextGenerator writer) {
+ string name = Helpers.UnderscoresToPascalCase(DescriptorUtil.GetFieldName(Descriptor));
+
+ string type;
+ switch (Descriptor.MappedType) {
+ case MappedType.Message:
+ type = DescriptorUtil.GetClassName(Descriptor.MessageType);
+ break;
+ case MappedType.Enum:
+ type = DescriptorUtil.GetClassName(Descriptor.EnumType);
+ break;
+ default:
+ type = DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
+ break;
+ }
+
+ if (Descriptor.IsRepeated) {
+ writer.WriteLine("{0} static readonly", ClassAccessLevel);
+ writer.WriteLine(" pb::GeneratedExtensionBase<scg::IList<{0}>> {1} =", type, name);
+ writer.WriteLine(" pb::GeneratedRepeatExtension<{0}>.CreateInstance(Descriptor.Extensions[{1}]);", type, Descriptor.Index);
+ } else {
+ writer.WriteLine("{0} static readonly pb::GeneratedExtensionBase<{1}> {2} =", ClassAccessLevel, type, name);
+ writer.WriteLine(" pb::GeneratedSingleExtension<{0}>.CreateInstance(Descriptor.Extensions[{1}]);", type, Descriptor.Index);
+ }
+ }
+ }
+}