• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / EntityFrameworkCore.Generator / 15282822856

27 May 2025 06:24PM UTC coverage: 54.881% (+0.02%) from 54.862%
15282822856

push

github

web-flow
Merge pull request #584 from DavidBoone/576-genericTypes

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

621 of 1293 branches covered (48.03%)

Branch coverage included in aggregate %.

9 of 20 new or added lines in 1 file covered. (45.0%)

1847 of 3204 relevant lines covered (57.65%)

61.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.54
/src/EntityFrameworkCore.Generator.Core/Extensions/GenerationExtensions.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics.CodeAnalysis;
4

5
using EntityFrameworkCore.Generator.Metadata.Generation;
6

7
using static System.Net.Mime.MediaTypeNames;
8

9
namespace EntityFrameworkCore.Generator.Extensions;
10

11
public static class GenerationExtensions
12
{
13
    #region Data
14
    private static readonly HashSet<string> _csharpKeywords = new(StringComparer.Ordinal)
1✔
15
    {
1✔
16
        "as", "do", "if", "in", "is",
1✔
17
        "for", "int", "new", "out", "ref", "try",
1✔
18
        "base", "bool", "byte", "case", "char", "else", "enum", "goto", "lock", "long", "null", "this", "true", "uint", "void",
1✔
19
        "break", "catch", "class", "const", "event", "false", "fixed", "float", "sbyte", "short", "throw", "ulong", "using", "while",
1✔
20
        "double", "extern", "object", "params", "public", "return", "sealed", "sizeof", "static", "string", "struct", "switch", "typeof", "unsafe", "ushort",
1✔
21
        "checked", "decimal", "default", "finally", "foreach", "private", "virtual",
1✔
22
        "abstract", "continue", "delegate", "explicit", "implicit", "internal", "operator", "override", "readonly", "volatile",
1✔
23
        "__arglist", "__makeref", "__reftype", "interface", "namespace", "protected", "unchecked",
1✔
24
        "__refvalue", "stackalloc"
1✔
25
    };
1✔
26

27
    private static readonly HashSet<string> _visualBasicKeywords = new(StringComparer.OrdinalIgnoreCase)
1✔
28
    {
1✔
29
        "as", "do", "if", "in", "is", "me", "of", "on", "or", "to",
1✔
30
        "and", "dim", "end", "for", "get", "let", "lib", "mod", "new", "not", "rem", "set", "sub", "try", "xor",
1✔
31
        "ansi", "auto", "byte", "call", "case", "cdbl", "cdec", "char", "cint", "clng", "cobj", "csng", "cstr", "date", "each", "else",
1✔
32
        "enum", "exit", "goto", "like", "long", "loop", "next", "step", "stop", "then", "true", "wend", "when", "with",
1✔
33
        "alias", "byref", "byval", "catch", "cbool", "cbyte", "cchar", "cdate", "class", "const", "ctype", "cuint", "culng", "endif", "erase", "error",
1✔
34
        "event", "false", "gosub", "isnot", "redim", "sbyte", "short", "throw", "ulong", "until", "using", "while",
1✔
35
        "csbyte", "cshort", "double", "elseif", "friend", "global", "module", "mybase", "object", "option", "orelse", "public", "resume", "return", "select", "shared",
1✔
36
        "single", "static", "string", "typeof", "ushort",
1✔
37
        "andalso", "boolean", "cushort", "decimal", "declare", "default", "finally", "gettype", "handles", "imports", "integer", "myclass", "nothing", "partial", "private", "shadows",
1✔
38
        "trycast", "unicode", "variant",
1✔
39
        "assembly", "continue", "delegate", "function", "inherits", "operator", "optional", "preserve", "property", "readonly", "synclock", "uinteger", "widening",
1✔
40
        "addressof", "interface", "namespace", "narrowing", "overloads", "overrides", "protected", "structure", "writeonly",
1✔
41
        "addhandler", "directcast", "implements", "paramarray", "raiseevent", "withevents",
1✔
42
        "mustinherit", "overridable",
1✔
43
        "mustoverride",
1✔
44
        "removehandler",
1✔
45
        "class_finalize", "notinheritable", "notoverridable",
1✔
46
        "class_initialize"
1✔
47
    };
1✔
48

49
    private static readonly List<string> _defaultUsings = new List<string>()
1✔
50
    {
1✔
51
        "System.Collections.Generic",
1✔
52
        "System"
1✔
53
    };
1✔
54

55
    private static readonly Dictionary<string, string> _csharpTypeAlias = new(16)
1✔
56
    {
1✔
57
        {"System.Int16", "short"},
1✔
58
        {"System.Int32", "int"},
1✔
59
        {"System.Int64", "long"},
1✔
60
        {"System.String", "string"},
1✔
61
        {"System.Object", "object"},
1✔
62
        {"System.Boolean", "bool"},
1✔
63
        {"System.Void", "void"},
1✔
64
        {"System.Char", "char"},
1✔
65
        {"System.Byte", "byte"},
1✔
66
        {"System.UInt16", "ushort"},
1✔
67
        {"System.UInt32", "uint"},
1✔
68
        {"System.UInt64", "ulong"},
1✔
69
        {"System.SByte", "sbyte"},
1✔
70
        {"System.Single", "float"},
1✔
71
        {"System.Double", "double"},
1✔
72
        {"System.Decimal", "decimal"}
1✔
73
    };
1✔
74
    #endregion
75

76
    public static string ToFieldName(this string name)
77
    {
78
        ArgumentException.ThrowIfNullOrEmpty(name);
×
79

80
        return "_" + name.ToCamelCase();
×
81
    }
82

83
    public static string MakeUnique(this string name, Func<string, bool> exists)
84
    {
85
        ArgumentException.ThrowIfNullOrEmpty(name);
×
86
        ArgumentNullException.ThrowIfNull(exists);
×
87

88
        string uniqueName = name;
×
89
        int count = 1;
×
90

91
        while (exists(uniqueName))
×
92
            uniqueName = string.Concat(name, count++);
×
93

94
        return uniqueName;
×
95
    }
96

97
    public static bool IsKeyword(this string text, CodeLanguage language = CodeLanguage.CSharp)
98
    {
99
        ArgumentException.ThrowIfNullOrEmpty(text);
740✔
100

101
        return language == CodeLanguage.VisualBasic
740!
102
            ? _visualBasicKeywords.Contains(text)
740✔
103
            : _csharpKeywords.Contains(text);
740✔
104
    }
105

106
    [return: NotNullIfNotNull(nameof(name))]
107
    public static string? ToSafeName(this string? name, CodeLanguage language = CodeLanguage.CSharp)
108
    {
109
        if (string.IsNullOrEmpty(name))
740!
110
            return name;
×
111

112
        if (!name.IsKeyword(language))
740!
113
            return name;
740✔
114

115
        return language == CodeLanguage.VisualBasic
×
116
            ? string.Format("[{0}]", name)
×
117
            : "@" + name;
×
118
    }
119

120
    public static string ToType(this Type type, CodeLanguage language = CodeLanguage.CSharp)
121
    {
122
        ArgumentNullException.ThrowIfNull(type);
210✔
123

124
        if (type.IsGenericType)
210!
125
        {
NEW
126
            var genericType = type.GetGenericTypeDefinition().FullName!
×
NEW
127
                .Split('`')[0];     // trim the `1 bit
×
128

NEW
129
            genericType = ToType(genericType, language);
×
130

NEW
131
            var elementType = ToType(type.GetGenericArguments()[0].FullName!, language);
×
NEW
132
            return language == CodeLanguage.VisualBasic
×
NEW
133
                ? $"{genericType}(Of {elementType})"
×
NEW
134
                : $"{genericType}<{elementType}>";
×
135
        }
136

137
        return ToType(type.FullName ?? type.Name, language);
210!
138
    }
139

140
    public static string ToType(this string type, CodeLanguage language = CodeLanguage.CSharp)
141
    {
142
        ArgumentException.ThrowIfNullOrEmpty(type);
210✔
143

144
        if (type == "System.Xml.XmlDocument")
210!
145
            type = "System.String";
×
146

147
        if (language == CodeLanguage.CSharp && _csharpTypeAlias.TryGetValue(type, out var t))
210✔
148
            return t;
126✔
149

150
        // drop common namespaces
151
        foreach (var defaultUsing in _defaultUsings)
424✔
152
            if (type.StartsWith(defaultUsing))
168✔
153
                return type.Remove(0, defaultUsing.Length + 1);
80✔
154

155
        return type;
4✔
156
    }
80✔
157

158
    public static string? ToNullableType(this Type type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
159
    {
NEW
160
        bool isValueType = type.IsValueType;
×
161

NEW
162
        var typeString = type.ToType(language);
×
163

164
        if (!isValueType || !isNullable)
×
NEW
165
            return typeString;
×
166

167
        return language == CodeLanguage.VisualBasic
×
168
            ? $"Nullable(Of {type})"
×
NEW
169
            : typeString + "?";
×
170
    }
171

172
    public static bool IsValueType(this string? type)
173
    {
174
        if (string.IsNullOrEmpty(type))
×
175
            return false;
×
176

177
        if (!type.StartsWith("System."))
×
178
            return false;
×
179

180
        var t = Type.GetType(type, false);
×
181
        return t != null && t.IsValueType;
×
182
    }
183

184
    public static string ToLiteral(this string value)
185
    {
186
        ArgumentException.ThrowIfNullOrEmpty(value);
662✔
187

188
        return value.Contains('\n') || value.Contains('\r')
662!
189
            ? "@\"" + value.Replace("\"", "\"\"") + "\""
662✔
190
            : "\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
662✔
191
    }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc