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

loresoft / EntityFrameworkCore.Generator / 14749418583

30 Apr 2025 07:44AM CUT coverage: 56.772%. First build
14749418583

Pull #666

github

web-flow
Merge d89b6a9a9 into 5ddfdd103
Pull Request #666: Bump Injectio from 4.1.0 to 5.0.0

566 of 1125 branches covered (50.31%)

Branch coverage included in aggregate %.

1815 of 3069 relevant lines covered (59.14%)

83.9 hits per line

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

0.0
/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel.DataAnnotations;
4

5
using EntityFrameworkCore.Generator.Options;
6
using EntityFrameworkCore.Generator.Serialization;
7

8
using Microsoft.CodeAnalysis.Options;
9

10
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
11

12
namespace EntityFrameworkCore.Generator;
13

14
public static class OptionMapper
15
{
16
    public static GeneratorOptions Map(GeneratorModel generator)
17
    {
18
        var options = new GeneratorOptions();
×
19
        options.Variables.ShouldEvaluate = false;
×
20

21
        MapProject(options.Project, generator.Project);
×
22
        MapDatabase(options.Database, generator.Database);
×
23
        MapData(options.Data, generator.Data);
×
24
        MapModel(options.Model, generator.Model);
×
25
        MapScript(options.Script, generator.Script);
×
26

27
        options.Variables.ShouldEvaluate = true;
×
28

29
        return options;
×
30
    }
31

32
    private static void MapScript(ScriptOptions option, ScriptModel script)
33
    {
34
        if (script == null)
×
35
            return;
×
36

37
        MapList(option.Context, script.Context, (template) =>
×
38
        {
×
39
            var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Context{option.Context.Count:0000}");
×
40
            return MapTemplate(option.Variables, template, prefix);
×
41
        });
×
42

43
        MapList(option.Entity, script.Entity, (template) =>
×
44
        {
×
45
            var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Entity{option.Entity.Count:0000}");
×
46
            return MapTemplate(option.Variables, template, prefix);
×
47
        });
×
48

49
        MapList(option.Model, script.Model, (template) =>
×
50
        {
×
51
            var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Model{option.Entity.Count:0000}");
×
52
            return MapTemplate(option.Variables, template, prefix);
×
53
        });
×
54
    }
×
55

56
    private static void MapModel(ModelOptions option, ViewModel model)
57
    {
58
        MapShared(option.Shared, model.Shared);
×
59
        MapRead(option.Read, model.Read);
×
60
        MapCreate(option.Create, model.Create);
×
61
        MapUpdate(option.Update, model.Update);
×
62
        MapMapper(option.Mapper, model.Mapper);
×
63
        MapValidator(option.Validator, model.Validator);
×
64
    }
×
65

66
    private static void MapClassBase(ClassOptionsBase option, ClassBase classBase)
67
    {
68
        option.Namespace = classBase.Namespace;
×
69
        option.Directory = classBase.Directory;
×
70
        option.Document = classBase.Document;
×
71
        option.Name = classBase.Name;
×
72
        option.BaseClass = classBase.BaseClass;
×
73
        option.Attributes = classBase.Attributes;
×
74
    }
×
75

76
    private static void MapModelBase(ModelOptionsBase option, ModelBase modelBase)
77
    {
78
        MapClassBase(option, modelBase);
×
79

80
        option.Generate = modelBase.Generate;
×
81

82
        MapSelection(option.Include, modelBase.Include);
×
83
        MapSelection(option.Exclude, modelBase.Exclude);
×
84

85
    }
×
86

87
    private static void MapValidator(ValidatorClassOptions option, ValidatorClass validator)
88
    {
89
        MapClassBase(option, validator);
×
90

91
        option.Generate = validator.Generate;
×
92
    }
×
93

94
    private static void MapMapper(MapperClassOptions option, MapperClass mapper)
95
    {
96
        MapClassBase(option, mapper);
×
97

98
        option.Generate = mapper.Generate;
×
99
    }
×
100

101
    private static void MapUpdate(UpdateModelOptions option, UpdateModel update)
102
    {
103
        MapModelBase(option, update);
×
104
    }
×
105

106
    private static void MapCreate(CreateModelOptions option, CreateModel create)
107
    {
108
        MapModelBase(option, create);
×
109
    }
×
110

111
    private static void MapRead(ReadModelOptions option, ReadModel read)
112
    {
113
        MapModelBase(option, read);
×
114
    }
×
115

116
    private static void MapShared(SharedModelOptions option, SharedModel shared)
117
    {
118
        option.Namespace = shared.Namespace;
×
119
        option.Directory = shared.Directory;
×
120

121
        MapSelection(option.Include, shared.Include);
×
122
        MapSelection(option.Exclude, shared.Exclude);
×
123
    }
×
124

125
    private static void MapData(DataOptions option, DataModel data)
126
    {
127
        MapContext(option.Context, data.Context);
×
128
        MapEntity(option.Entity, data.Entity);
×
129
        MapMapping(option.Mapping, data.Mapping);
×
130
        MapQuery(option.Query, data.Query);
×
131
    }
×
132

133
    private static void MapQuery(QueryExtensionOptions option, QueryExtension query)
134
    {
135
        MapClassBase(option, query);
×
136

137
        option.Generate = query.Generate;
×
138
        option.IndexPrefix = query.IndexPrefix;
×
139
        option.UniquePrefix = query.UniquePrefix;
×
140
    }
×
141

142
    private static void MapMapping(MappingClassOptions option, MappingClass mapping)
143
    {
144
        MapClassBase(option, mapping);
×
145

146
        option.Temporal = mapping.Temporal;
×
147
        option.RowVersion = mapping.RowVersion;
×
148
    }
×
149

150
    private static void MapEntity(EntityClassOptions option, EntityClass entity)
151
    {
152
        MapClassBase(option, entity);
×
153

154
        option.EntityNaming = entity.EntityNaming;
×
155
        option.RelationshipNaming = entity.RelationshipNaming;
×
156
        option.PrefixWithSchemaName = entity.PrefixWithSchemaName;
×
157
        option.MappingAttributes = entity.MappingAttributes;
×
158

159
        MapSelection(option.Renaming, entity.Renaming);
×
160
    }
×
161

162
    private static void MapSelection(SelectionOptions option, SelectionModel selection)
163
    {
164
        if (selection == null)
×
165
            return;
×
166

167
        MapList(option.Entities, selection.Entities, (match) =>
×
168
        {
×
169
            var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Entity{option.Entities.Count:0000}");
×
170
            return MapMatch(option.Variables, match, prefix);
×
171
        });
×
172

173
        MapList(option.Properties, selection.Properties, (match) =>
×
174
        {
×
175
            var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Property{option.Properties.Count:0000}");
×
176
            return MapMatch(option.Variables, match, prefix);
×
177
        });
×
178
    }
×
179

180
    private static void MapContext(ContextClassOptions option, ContextClass context)
181
    {
182
        MapClassBase(option, context);
×
183

184
        option.PropertyNaming = context.PropertyNaming;
×
185
    }
×
186

187
    private static void MapDatabase(DatabaseOptions option, DatabaseModel database)
188
    {
189
        option.Provider = database.Provider;
×
190
        option.ConnectionString = database.ConnectionString;
×
191
        option.ConnectionName = database.ConnectionName;
×
192
        option.UserSecretsId = database.UserSecretsId;
×
193
        option.TableNaming = database.TableNaming;
×
194

195
        MapList(option.Tables, database.Tables);
×
196
        MapList(option.Schemas, database.Schemas);
×
197
        MapList(option.Exclude, database.Exclude, (match) =>
×
198
        {
×
199
            var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Exclude{option.Exclude.Count:0000}");
×
200
            return MapMatch(option.Variables, match, prefix);
×
201
        });
×
202
    }
×
203

204
    private static void MapProject(ProjectOptions option, ProjectModel project)
205
    {
206
        option.Namespace = project.Namespace;
×
207
        option.Directory = project.Directory;
×
208
        option.Nullable = project.Nullable;
×
209
        option.FileScopedNamespace = project.FileScopedNamespace;
×
210
    }
×
211

212

213
    private static void MapList<T>(IList<T> targetList, IList<T> sourceList)
214
    {
215
        if (!(sourceList?.Count > 0))
×
216
            return;
×
217

218
        foreach (var source in sourceList)
×
219
            targetList.Add(source);
×
220
    }
×
221

222
    private static void MapList<TTarget, TSource>(IList<TTarget> targetList, IList<TSource> sourceList, Func<TSource, TTarget> factory)
223
    {
224
        if (sourceList == null || sourceList.Count == 0)
×
225
            return;
×
226

227
        foreach (var source in sourceList)
×
228
        {
229
            var target = factory(source);
×
230
            targetList.Add(target);
×
231
        }
232
    }
×
233

234
    private static MatchOptions MapMatch(VariableDictionary variables, MatchModel match, string prefix)
235
    {
236
        return new MatchOptions(variables, prefix)
×
237
        {
×
238
            Exact = match.Exact,
×
239
            Expression = match.Expression
×
240
        };
×
241
    }
242

243
    private static TemplateOptions MapTemplate(VariableDictionary variables, TemplateModel template, string prefix)
244
    {
245
        var option = new TemplateOptions(variables, prefix)
×
246
        {
×
247
            TemplatePath = template.TemplatePath,
×
248
            FileName = template.FileName,
×
249
            Namespace = template.Namespace,
×
250
            BaseClass = template.BaseClass,
×
251
            Directory = template.Directory,
×
252
            Overwrite = template.Overwrite,
×
253
            Merge = template.Merge,
×
254
        };
×
255

256
        if (template.Parameters == null || template.Parameters.Count == 0)
×
257
            return option;
×
258

259
        foreach (var paremeter in template.Parameters)
×
260
            option.Parameters[paremeter.Key] = paremeter.Value;
×
261

262
        return option;
×
263
    }
264

265
}
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