From dde5e92c76529b25a6b6859e384479483846d5a1 Mon Sep 17 00:00:00 2001 From: Tom Vervoort Date: Tue, 24 Oct 2017 13:16:40 +0200 Subject: [PATCH] Allow attributes with boolean or enum properties. --- .../CSharp/AttributeGeneratorTests.cs | 11 +++++---- .../CSharp/AttributeGenerator.cs | 23 +++++++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/RequestHandlers.Mvc.Tests/CSharp/AttributeGeneratorTests.cs b/src/RequestHandlers.Mvc.Tests/CSharp/AttributeGeneratorTests.cs index 5a9af76..53960ef 100644 --- a/src/RequestHandlers.Mvc.Tests/CSharp/AttributeGeneratorTests.cs +++ b/src/RequestHandlers.Mvc.Tests/CSharp/AttributeGeneratorTests.cs @@ -32,13 +32,13 @@ public void Generate_GivenCustomAttributeData_WithNamedArgument_GenerateAttribut Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestNamedArgumentsAttribute(StringProperty = \"Yenthe\")]", stringAttribute); } - [TestNamedArguments(StringProperty = "Yenthe", IntProperty = 5)] class AttributeWithNamedArgumentsHost { } + [TestNamedArguments(StringProperty = "Yenthe", IntProperty = 5, BoolProperty = true)] class AttributeWithNamedArgumentsHost { } [Fact] public void Generate_GivenCustomAttributeData_WithMultipleNamedArguments_GenerateAttributeAsString() { var stringAttribute = _sut.Generate(typeof(AttributeWithNamedArgumentsHost).GetCustomAttributesData().First()); - Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestNamedArgumentsAttribute(StringProperty = \"Yenthe\", IntProperty = 5)]", stringAttribute); + Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestNamedArgumentsAttribute(StringProperty = \"Yenthe\", IntProperty = 5, BoolProperty = true)]", stringAttribute); } [TestNamedArguments(EnumProperty = TestEnum.SecondValue)] class AttributeWithEnumNamedArgumentHost { } @@ -47,7 +47,7 @@ public void Generate_GivenCustomAttributeData_WithMultipleNamedArguments_Generat public void Generate_GivenCustomAttributeData_WithEnumNamedArgument_GenerateAttributeAsString() { var stringAttribute = _sut.Generate(typeof(AttributeWithEnumNamedArgumentHost).GetCustomAttributesData().First()); - Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestNamedArgumentsAttribute(EnumProperty = 1)]", stringAttribute); + Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestNamedArgumentsAttribute(EnumProperty = RequestHandlers.Mvc.Tests.CSharp.TestEnum.SecondValue)]", stringAttribute); } [TestConstructorArguments("first", 2, TestEnum.ThirdValue)] class AttributeWithConstructorArgumentsHost { } @@ -56,7 +56,7 @@ public void Generate_GivenCustomAttributeData_WithEnumNamedArgument_GenerateAttr public void Generate_GivenCustomAttributeData_WithConstructorArguments_GenerateAttributeAsString() { var stringAttribute = _sut.Generate(typeof(AttributeWithConstructorArgumentsHost).GetCustomAttributesData().First()); - Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestConstructorArgumentsAttribute(\"first\", 2, 2)]", stringAttribute); + Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestConstructorArgumentsAttribute(\"first\", 2, RequestHandlers.Mvc.Tests.CSharp.TestEnum.ThirdValue)]", stringAttribute); } [TestConstructorArguments("first", 2, TestEnum.ThirdValue, Property = "Yenthe")] class AttributeWithNamedAndConstructorArgumentsHost { } @@ -65,7 +65,7 @@ public void Generate_GivenCustomAttributeData_WithConstructorArguments_GenerateA public void Generate_GivenCustomAttributeData_WithNamedAndConstructorArguments_GenerateAttributeAsString() { var stringAttribute = _sut.Generate(typeof(AttributeWithNamedAndConstructorArgumentsHost).GetCustomAttributesData().First()); - Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestConstructorArgumentsAttribute(\"first\", 2, 2, Property = \"Yenthe\")]", stringAttribute); + Assert.Equal("[RequestHandlers.Mvc.Tests.CSharp.TestConstructorArgumentsAttribute(\"first\", 2, RequestHandlers.Mvc.Tests.CSharp.TestEnum.ThirdValue, Property = \"Yenthe\")]", stringAttribute); } } @@ -81,6 +81,7 @@ public class TestNamedArgumentsAttribute : Attribute public string StringProperty { get; set; } public int IntProperty { get; set; } public TestEnum EnumProperty { get; set; } + public bool BoolProperty { get; set; } } public class TestConstructorArgumentsAttribute : Attribute diff --git a/src/RequestHandlers.Mvc/CSharp/AttributeGenerator.cs b/src/RequestHandlers.Mvc/CSharp/AttributeGenerator.cs index 4aa919a..6eafac3 100644 --- a/src/RequestHandlers.Mvc/CSharp/AttributeGenerator.cs +++ b/src/RequestHandlers.Mvc/CSharp/AttributeGenerator.cs @@ -33,17 +33,32 @@ private void AppendConstructor(IEnumerable constru private string GenerateConstructorArgument(CustomAttributeTypedArgument constructorArgument) { - return ObjectToCodeString(constructorArgument.Value); + return ObjectToCodeString(constructorArgument.ArgumentType, constructorArgument.Value); } private string GenerateNamedArgument(CustomAttributeNamedArgument namedArgument) { - return $"{namedArgument.MemberName} = {ObjectToCodeString(namedArgument.TypedValue.Value)}"; + return $"{namedArgument.MemberName} = {ObjectToCodeString(namedArgument.TypedValue.ArgumentType, namedArgument.TypedValue.Value)}"; } - private static string ObjectToCodeString(object value) + private static string ObjectToCodeString(Type type, object value) { - return value is string ? $"\"{value}\"" : value.ToString(); + if (type == typeof(string)) + { + return $"\"{value}\""; + } + + if (type == typeof(bool)) + { + return (bool)value ? "true" : "false"; + } + + if (type.GetTypeInfo().IsEnum) + { + return type.FullName + "." + Enum.GetName(type, value); + } + + return value.ToString(); } } } \ No newline at end of file