Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/RequestHandlers.Mvc.Tests/CSharp/AttributeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand All @@ -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 { }
Expand All @@ -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 { }
Expand All @@ -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);
}
}

Expand All @@ -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
Expand Down
23 changes: 19 additions & 4 deletions src/RequestHandlers.Mvc/CSharp/AttributeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,32 @@ private void AppendConstructor(IEnumerable<CustomAttributeTypedArgument> 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();
}
}
}