Skip to content

Commit 9791037

Browse files
committed
Handle Generic types such as List<> and NpgsqlRange<>
1 parent 81f7480 commit 9791037

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
44

@@ -46,6 +46,12 @@ public static class GenerationExtensions
4646
"class_initialize"
4747
};
4848

49+
private static readonly List<string> _defaultUsings = new List<string>()
50+
{
51+
"System.Collections.Generic",
52+
"System"
53+
};
54+
4955
private static readonly Dictionary<string, string> _csharpTypeAlias = new(16)
5056
{
5157
{"System.Int16", "short"},
@@ -115,6 +121,19 @@ public static string ToType(this Type type, CodeLanguage language = CodeLanguage
115121
{
116122
ArgumentNullException.ThrowIfNull(type);
117123

124+
if (type.IsGenericType)
125+
{
126+
var genericType = type.GetGenericTypeDefinition().FullName!
127+
.Split('`')[0]; // trim the `1 bit
128+
129+
genericType = ToType(genericType, language);
130+
131+
var elementType = ToType(type.GetGenericArguments()[0].FullName!, language);
132+
return language == CodeLanguage.VisualBasic
133+
? $"{genericType}(Of {elementType})"
134+
: $"{genericType}<{elementType}>";
135+
}
136+
118137
return ToType(type.FullName ?? type.Name, language);
119138
}
120139

@@ -128,34 +147,26 @@ public static string ToType(this string type, CodeLanguage language = CodeLangua
128147
if (language == CodeLanguage.CSharp && _csharpTypeAlias.TryGetValue(type, out var t))
129148
return t;
130149

131-
// drop system from namespace
132-
var parts = type.Split('.');
133-
if (parts.Length == 2 && parts[0] == "System")
134-
return parts[1];
150+
// drop common namespaces
151+
foreach (var defaultUsing in _defaultUsings)
152+
if (type.StartsWith(defaultUsing))
153+
return type.Remove(0, defaultUsing.Length + 1);
135154

136155
return type;
137156
}
138157

139158
public static string? ToNullableType(this Type type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
140159
{
141-
return ToNullableType(type.FullName, isNullable, language);
142-
}
143-
144-
public static string? ToNullableType(this string? type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
145-
{
146-
if (string.IsNullOrEmpty(type))
147-
return null;
148-
149-
bool isValueType = type.IsValueType();
160+
bool isValueType = type.IsValueType;
150161

151-
type = type.ToType(language);
162+
var typeString = type.ToType(language);
152163

153164
if (!isValueType || !isNullable)
154-
return type;
165+
return typeString;
155166

156167
return language == CodeLanguage.VisualBasic
157168
? $"Nullable(Of {type})"
158-
: type + "?";
169+
: typeString + "?";
159170
}
160171

161172
public static bool IsValueType(this string? type)

0 commit comments

Comments
 (0)