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

loresoft / EntityFrameworkCore.Generator / 15072048022

16 May 2025 03:31PM UTC coverage: 55.392% (-1.4%) from 56.772%
15072048022

push

github

pwelter34
enable nullable support

616 of 1271 branches covered (48.47%)

Branch coverage included in aggregate %.

233 of 397 new or added lines in 61 files covered. (58.69%)

17 existing lines in 11 files now uncovered.

1824 of 3134 relevant lines covered (58.2%)

88.56 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 Dictionary<string, string> _csharpTypeAlias = new(16)
1✔
50
    {
1✔
51
        {"System.Int16", "short"},
1✔
52
        {"System.Int32", "int"},
1✔
53
        {"System.Int64", "long"},
1✔
54
        {"System.String", "string"},
1✔
55
        {"System.Object", "object"},
1✔
56
        {"System.Boolean", "bool"},
1✔
57
        {"System.Void", "void"},
1✔
58
        {"System.Char", "char"},
1✔
59
        {"System.Byte", "byte"},
1✔
60
        {"System.UInt16", "ushort"},
1✔
61
        {"System.UInt32", "uint"},
1✔
62
        {"System.UInt64", "ulong"},
1✔
63
        {"System.SByte", "sbyte"},
1✔
64
        {"System.Single", "float"},
1✔
65
        {"System.Double", "double"},
1✔
66
        {"System.Decimal", "decimal"}
1✔
67
    };
1✔
68
    #endregion
69

70
    public static string ToFieldName(this string name)
71
    {
NEW
72
        ArgumentException.ThrowIfNullOrEmpty(name);
×
73

UNCOV
74
        return "_" + name.ToCamelCase();
×
75
    }
76

77
    public static string MakeUnique(this string name, Func<string, bool> exists)
78
    {
NEW
79
        ArgumentException.ThrowIfNullOrEmpty(name);
×
NEW
80
        ArgumentNullException.ThrowIfNull(exists);
×
81

82
        string uniqueName = name;
×
83
        int count = 1;
×
84

85
        while (exists(uniqueName))
×
86
            uniqueName = string.Concat(name, count++);
×
87

88
        return uniqueName;
×
89
    }
90

91
    public static bool IsKeyword(this string text, CodeLanguage language = CodeLanguage.CSharp)
92
    {
93
        ArgumentException.ThrowIfNullOrEmpty(text);
1,110✔
94

95
        return language == CodeLanguage.VisualBasic
1,110!
96
            ? _visualBasicKeywords.Contains(text)
1,110✔
97
            : _csharpKeywords.Contains(text);
1,110✔
98
    }
99

100
    [return: NotNullIfNotNull(nameof(name))]
101
    public static string? ToSafeName(this string? name, CodeLanguage language = CodeLanguage.CSharp)
102
    {
103
        if (string.IsNullOrEmpty(name))
1,110!
NEW
104
            return name;
×
105

106
        if (!name.IsKeyword(language))
1,110!
107
            return name;
1,110✔
108

109
        return language == CodeLanguage.VisualBasic
×
110
            ? string.Format("[{0}]", name)
×
111
            : "@" + name;
×
112
    }
113

114
    public static string ToType(this Type type, CodeLanguage language = CodeLanguage.CSharp)
115
    {
116
        ArgumentNullException.ThrowIfNull(type);
315✔
117

118
        return ToType(type.FullName ?? type.Name, language);
315!
119
    }
120

121
    public static string ToType(this string type, CodeLanguage language = CodeLanguage.CSharp)
122
    {
123
        ArgumentException.ThrowIfNullOrEmpty(type);
315✔
124

125
        if (type == "System.Xml.XmlDocument")
315!
126
            type = "System.String";
×
127

128
        if (language == CodeLanguage.CSharp && _csharpTypeAlias.TryGetValue(type, out var t))
315✔
129
            return t;
189✔
130

131
        // drop system from namespace
132
        var parts = type.Split('.');
126✔
133
        if (parts.Length == 2 && parts[0] == "System")
126✔
134
            return parts[1];
120✔
135

136
        return type;
6✔
137
    }
138

139
    public static string? ToNullableType(this Type type, bool isNullable = false, CodeLanguage language = CodeLanguage.CSharp)
140
    {
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
    {
NEW
146
        if (string.IsNullOrEmpty(type))
×
NEW
147
            return null;
×
148

UNCOV
149
        bool isValueType = type.IsValueType();
×
150

151
        type = type.ToType(language);
×
152

153
        if (!isValueType || !isNullable)
×
154
            return type;
×
155

156
        return language == CodeLanguage.VisualBasic
×
157
            ? $"Nullable(Of {type})"
×
158
            : type + "?";
×
159
    }
160

161
    public static bool IsValueType(this string? type)
162
    {
NEW
163
        if (string.IsNullOrEmpty(type))
×
NEW
164
            return false;
×
165

166
        if (!type.StartsWith("System."))
×
167
            return false;
×
168

169
        var t = Type.GetType(type, false);
×
170
        return t != null && t.IsValueType;
×
171
    }
172

173
    public static string ToLiteral(this string value)
174
    {
175
        ArgumentException.ThrowIfNullOrEmpty(value);
993✔
176

177
        return value.Contains('\n') || value.Contains('\r')
993!
178
            ? "@\"" + value.Replace("\"", "\"\"") + "\""
993✔
179
            : "\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
993✔
180
    }
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc