Ejemplo n.º 1
0
        protected override void Generate(
            CodeWriter writer,
            EnumDescriptor descriptor,
            out string fileName)
        {
            fileName = NamingConventions.EnumParserNameFromEnumName(descriptor.Name);

            ClassBuilder classBuilder = ClassBuilder
                                        .New(fileName)
                                        .AddImplements(TypeNames.IInputValueFormatter)
                                        .AddImplements(
                TypeNames.ILeafValueParser.WithGeneric(TypeNames.String, descriptor.Name));

            const string serializedValueParamName = "serializedValue";

            classBuilder
            .AddMethod("Parse")
            .AddParameter(ParameterBuilder.New()
                          .SetName(serializedValueParamName)
                          .SetType(TypeNames.String))
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType(descriptor.Name)
            .AddCode(CreateEnumParsingSwitch(serializedValueParamName, descriptor));

            const string runtimeValueParamName = "runtimeValue";

            classBuilder
            .AddMethod("Format")
            .SetAccessModifier(AccessModifier.Public)
            .SetReturnType("object")
            .AddParameter(ParameterBuilder.New()
                          .SetType(TypeNames.Object.MakeNullable())
                          .SetName(runtimeValueParamName))
            .AddCode(CreateEnumFormatingSwitch(runtimeValueParamName, descriptor));

            classBuilder
            .AddProperty("TypeName")
            .AsLambda(descriptor.Name.AsStringToken())
            .SetPublic()
            .SetType(TypeNames.String);

            CodeFileBuilder
            .New()
            .SetNamespace(descriptor.Namespace)
            .AddType(classBuilder)
            .Build(writer);
        }