-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyMaker.cs
130 lines (113 loc) · 3.03 KB
/
PropertyMaker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Collections.Generic;
public class PropertyMaker
{
private readonly List<string> modifiers = new List<string>();
private readonly ClassMaker cm;
private readonly string name;
private string type;
private bool isNullable;
private bool isList;
private bool isDbSet;
private bool explicitNullInitializer;
private bool noInitializer;
private bool defaultInitializer;
private string customInitializer;
public PropertyMaker(ClassMaker cm, string name)
{
this.cm = cm;
this.name = name;
}
public PropertyMaker IsType(string type)
{
this.type = type;
this.isList = false;
this.isDbSet = false;
return this;
}
public PropertyMaker IsListOfType(string type)
{
this.type = type;
this.isList = true;
this.isDbSet = false;
return this;
}
public PropertyMaker IsDbSetOfType(string type)
{
this.type = type;
this.isList = false;
this.isDbSet = true;
return this;
}
public PropertyMaker WithModifier(string modifier)
{
modifiers.Add(modifier);
return this;
}
public PropertyMaker IsNullable()
{
isNullable = true;
return this;
}
public PropertyMaker InitializeAsExplicitNull()
{
explicitNullInitializer = true;
return this;
}
public PropertyMaker NoInitializer()
{
noInitializer = true;
return this;
}
public PropertyMaker DefaultInitializer()
{
defaultInitializer = true;
return this;
}
public PropertyMaker WithCustomInitializer(string initializer)
{
customInitializer = initializer;
return this;
}
public void Build()
{
cm.AddLine("public " +
GetModifiers() + " " +
GetPropertyType() + " " +
name + Pluralize() + GetAccessor() +
GetInitializer());
}
private string GetModifiers()
{
return string.Join(" ", modifiers);
}
private string GetAccessor()
{
if (isDbSet) return " => ";
return " { get; set; }";
}
private string Pluralize()
{
if (isList || isDbSet) return "s";
return "";
}
private string GetPropertyType()
{
var t = type;
if (isList) t = "List<" + t + ">";
if (isDbSet) t = "DbSet<" + t + ">";
if (isNullable) return t + "?";
return t;
}
private string GetInitializer()
{
if (customInitializer != null) return customInitializer;
if (noInitializer) return "";
if (explicitNullInitializer) return " = null!;";
if (defaultInitializer) return " = default(" + type + ")!;";
if (isNullable) return "";
if (isList) return " = new List<" + type + ">();";
if (isDbSet) return "Set<" + type + ">();";
if (TypeUtils.IsNullableRequiredForType(type)) return TypeUtils.GetInitializerForType(type);
return " = new " + type + "();";
}
}