Skip to content

Handle Generic types such as List<> and NpgsqlRange<> #584

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public static class GenerationExtensions
"class_initialize"
};

private static readonly List<string> _defaultUsings = new List<string>()
{
"System.Collections.Generic",
"System"
};

private static readonly Dictionary<string, string> _csharpTypeAlias = new Dictionary<string, string>(16)
{
{"System.Int16", "short"},
Expand Down Expand Up @@ -99,6 +105,19 @@ public static string ToSafeName(this string name, CodeLanguage language = CodeLa

public static string ToType(this Type type, CodeLanguage language = CodeLanguage.CSharp)
{
if (type.IsGenericType)
{
var genericType = type.GetGenericTypeDefinition().FullName
.Split('`')[0]; // trim the `1 bit

genericType = ToType(genericType, language);

var elementType = ToType(type.GetGenericArguments()[0].FullName, language);
return language == CodeLanguage.VisualBasic
? $"{genericType}(Of {elementType})"
: $"{genericType}<{elementType}>";
}

return ToType(type.FullName, language);
}

Expand All @@ -110,40 +129,26 @@ public static string ToType(this string type, CodeLanguage language = CodeLangua
if (language == CodeLanguage.CSharp && _csharpTypeAlias.TryGetValue(type, out string t))
return t;

// drop system from namespace
string[] parts = type.Split('.');
if (parts.Length == 2 && parts[0] == "System")
return parts[1];
// drop common namespaces
foreach (var defaultUsing in _defaultUsings)
if (type.StartsWith(defaultUsing))
return type.Remove(0, defaultUsing.Length + 1);

return type;
}

public static string ToNullableType(this Type type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
{
return ToNullableType(type.FullName, isNullable, language);
}
bool isValueType = type.IsValueType;

public static string ToNullableType(this string type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
{
bool isValueType = type.IsValueType();

type = type.ToType(language);
var typeString = type.ToType(language);

if (!isValueType || !isNullable)
return type;
return typeString;

return language == CodeLanguage.VisualBasic
? $"Nullable(Of {type})"
: type + "?";
}

public static bool IsValueType(this string type)
{
if (!type.StartsWith("System."))
return false;

var t = Type.GetType(type, false);
return t != null && t.IsValueType;
? $"Nullable(Of {typeString})"
: typeString + "?";
}

public static string ToLiteral(this string value)
Expand Down