Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for default keyword for non primitive structs #88

Merged
merged 1 commit into from Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/PublicApiGenerator/ApiGenerator.cs
Expand Up @@ -644,9 +644,15 @@ static bool IsExtensionMethod(ICustomAttributeProvider method)
}
}

static object FormatParameterConstant(IConstantProvider parameter)
static object FormatParameterConstant(ParameterDefinition parameter)
{
return parameter.Constant is string ? string.Format(CultureInfo.InvariantCulture, "\"{0}\"", parameter.Constant) : (parameter.Constant ?? "null");
if (parameter.Constant is string)
return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", parameter.Constant);

if (parameter.Constant != null)
return parameter.Constant;

return parameter.ParameterType.IsValueType ? "default" : "null";
}

static MemberAttributes GetMethodAttributes(MethodDefinition method)
Expand Down
5 changes: 3 additions & 2 deletions src/PublicApiGeneratorTests/Method_parameters.cs
@@ -1,4 +1,5 @@
using PublicApiGeneratorTests.Examples;
using System.Threading;
using Xunit;

namespace PublicApiGeneratorTests
Expand Down Expand Up @@ -168,7 +169,7 @@ public void Should_output_default_values()
public class MethodWithDefaultValues
{
public MethodWithDefaultValues() { }
public void Method(int value1 = 42, string value2 = ""hello world"") { }
public void Method(int value1 = 42, string value2 = ""hello world"", System.Threading.CancellationToken token = default, int value3 = 0, string value4 = null) { }
}
}");
}
Expand Down Expand Up @@ -351,7 +352,7 @@ public void Method(int value1, string value2, ComplexType value3)

public class MethodWithDefaultValues
{
public void Method(int value1 = 42, string value2 = "hello world")
public void Method(int value1 = 42, string value2 = "hello world", CancellationToken token = default, int value3 = default, string value4 = default)
{
}
}
Expand Down