Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

42 lines
1.9 KiB

using System;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.ProtoGen {
internal static class SourceGenerators {
private static readonly Dictionary<Type, Func<IDescriptor, ISourceGenerator>> GeneratorFactories = new Dictionary<Type, Func<IDescriptor, ISourceGenerator>> {
{ typeof(FileDescriptor), descriptor => new UmbrellaClassGenerator((FileDescriptor) descriptor) },
{ typeof(EnumDescriptor), descriptor => new EnumGenerator((EnumDescriptor) descriptor) },
{ typeof(ServiceDescriptor), descriptor => new ServiceGenerator((ServiceDescriptor) descriptor) },
{ typeof(MessageDescriptor), descriptor => new MessageGenerator((MessageDescriptor) descriptor) },
// For other fields, we have IFieldSourceGenerators.
{ typeof(FieldDescriptor), descriptor => new ExtensionGenerator((FieldDescriptor) descriptor) }
};
public static IFieldSourceGenerator CreateFieldGenerator(FieldDescriptor field) {
switch (field.MappedType) {
case MappedType.Message :
return field.IsRepeated
? (IFieldSourceGenerator) new RepeatedMessageFieldGenerator(field)
: new MessageFieldGenerator(field);
case MappedType.Enum:
return field.IsRepeated
? (IFieldSourceGenerator)new RepeatedEnumFieldGenerator(field)
: new EnumFieldGenerator(field);
default:
return field.IsRepeated
? (IFieldSourceGenerator)new RepeatedPrimitiveFieldGenerator(field)
: new PrimitiveFieldGenerator(field);
}
}
public static ISourceGenerator CreateGenerator<T>(T descriptor) where T : IDescriptor {
Func<IDescriptor, ISourceGenerator> factory;
if (!GeneratorFactories.TryGetValue(typeof(T), out factory)) {
throw new ArgumentException("No generator registered for " + typeof(T).Name);
}
return factory(descriptor);
}
}
}