Skip to content

Commit

Permalink
Fixed "empty parameter name" bug.
Browse files Browse the repository at this point in the history
- And improved PascalCase to snake_case converter
  • Loading branch information
Uralstech committed Nov 7, 2024
1 parent f4c5b2f commit 0584681
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public EzrSharpCompatibilityWrapper(TMemberInfo wrappedMember, object? instance,
{
SharpMember = wrappedMember;
AutoWrapperAttribute = wrappedMember.GetCustomAttribute<SharpAutoWrapperAttribute>();
SharpMemberName = !string.IsNullOrEmpty(AutoWrapperAttribute?.Name) ? AutoWrapperAttribute.Name : PascalToSnakeCase(wrappedMember.Name);
SharpMemberName = !string.IsNullOrEmpty(AutoWrapperAttribute?.Name) ? AutoWrapperAttribute.Name : PascalToSnakeCase(wrappedMember.Name)!;
Instance = instance;
}

Expand All @@ -85,18 +85,21 @@ public bool Validate()
/// </summary>
/// <param name="text">The text to convert in PascalCase.</param>
/// <returns>The converted text in snake_case.</returns>
internal protected static string PascalToSnakeCase(string text)
internal protected static string? PascalToSnakeCase(string? text)
{
if (string.IsNullOrEmpty(text))
return null;

StringBuilder result = new();
result.Append(char.ToLowerInvariant(text[0]));

for (int i = 1; i < text.Length; ++i)
{
char c = text[i];
if (char.IsUpper(c))
result.Append('_').Append(char.ToLowerInvariant(c));
else
result.Append(c);
if (c == '_')
continue;

result.Append(char.IsUpper(c) ? $"_{char.ToLowerInvariant(c)}" : c);
}

return result.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public EzrSharpCompatibilityConstructor(
ConstructingType = constructingType;

SharpAutoWrapperAttribute? autoWrapperAttribute = constructingType.GetCustomAttribute<SharpAutoWrapperAttribute>();
ConstructingTypeName = !string.IsNullOrEmpty(autoWrapperAttribute?.Name) ? autoWrapperAttribute.Name : PascalToSnakeCase(constructingType.Name);
ConstructingTypeName = !string.IsNullOrEmpty(autoWrapperAttribute?.Name) ? autoWrapperAttribute.Name : PascalToSnakeCase(constructingType.Name)!;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public EzrSharpCompatibilityExecutable(TMethodBase sharpMethodBase, object? inst
Tag = $"{Tag}.{SharpMemberName}.{UIDProvider.Get()}";

Parameters = SharpMember.GetParameters();
ParameterNames = new string[Parameters.Length];

ParameterNames = Array.ConvertAll(Parameters, p =>
for (int i = 0; i < Parameters.Length; i++)
{
string? definedName = p.GetCustomAttribute<SharpAutoWrapperAttribute>()?.Name;
return string.IsNullOrEmpty(definedName) ? PascalToSnakeCase(p.Name ?? string.Empty) : definedName;
});
ParameterInfo parameter = Parameters[i];
string? definedName = parameter.GetCustomAttribute<SharpAutoWrapperAttribute>()?.Name;

ParameterNames[i] = string.IsNullOrEmpty(definedName) ? PascalToSnakeCase(parameter.Name) ?? $"param_{i}" : definedName;
}

if (!skipValidation)
Validate();
Expand Down Expand Up @@ -125,7 +128,7 @@ protected internal Dictionary<string, IEzrObject> ArgumentsArrayToDictionary(Ref
for (int i = 0; i < Parameters.Length; i++)
{
ParameterInfo parameter = Parameters[i];
if (!string.IsNullOrEmpty(parameter.Name) && arguments.TryGetValue(ParameterNames[i], out IEzrObject? argument))
if (arguments.TryGetValue(ParameterNames[i], out IEzrObject? argument))
{
object? primitiveArgument = EzrObjectToCSharp(argument, parameter.ParameterType, result);
if (result.ShouldReturn)
Expand All @@ -151,7 +154,7 @@ protected internal Dictionary<string, IEzrObject> ArgumentsArrayToDictionary(Ref

if (arguments.Count > 0)
{
Dictionary<string, IEzrObject>.Enumerator argumentsEnumerator = arguments.GetEnumerator();
using Dictionary<string, IEzrObject>.Enumerator argumentsEnumerator = arguments.GetEnumerator();
argumentsEnumerator.MoveNext();

KeyValuePair<string, IEzrObject> first = argumentsEnumerator.Current;
Expand Down

0 comments on commit 0584681

Please sign in to comment.