-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFieldBuilder.cs
More file actions
217 lines (189 loc) · 7.78 KB
/
FieldBuilder.cs
File metadata and controls
217 lines (189 loc) · 7.78 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Linq;
using Optional;
using Optional.Unsafe;
namespace SharpCode
{
/// <summary>
/// Provides functionality for building fields. <see cref="FieldBuilder"/> instances are <b>not</b> immutable.
/// </summary>
public class FieldBuilder
{
private readonly List<TypeParameterBuilder> _typeParameters = new List<TypeParameterBuilder>();
internal FieldBuilder()
{
}
internal Field Field { get; private set; } = new Field(AccessModifier.Private);
/// <summary>
/// Sets accessibilty modifier of the field being built.
/// </summary>
public FieldBuilder WithAccessModifier(AccessModifier accessModifier)
{
Field = Field.With(accessModifier: Option.Some(accessModifier));
return this;
}
/// <summary>
/// Sets the type of the field being built.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="type"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// The specified <paramref name="type"/> is empty or invalid.
/// </exception>
public FieldBuilder WithType(string type)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
if (string.IsNullOrWhiteSpace(type))
throw new ArgumentException("Field type must be a valid, non-empty string.", nameof(type));
Field = Field.With(type: Option.Some(type));
return this;
}
/// <summary>
/// Sets the type of the field being built.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="type"/> is <c>null</c>.
/// </exception>
public FieldBuilder WithType(Type type)
{
if (type is null)
throw new ArgumentNullException(nameof(type));
Field = Field.With(type: Option.Some(type.Name));
return this;
}
/// <summary>
/// Sets the name of the field being built.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="name"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// The specified <paramref name="name"/> is empty or invalid.
/// </exception>
public FieldBuilder WithName(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Field name must be a valid, non-empty string.", nameof(name));
Field = Field.With(name: Option.Some(name));
return this;
}
/// <summary>
/// Sets the readonly preference for the field being built.
/// </summary>
/// <param name="makeReadonly">
/// Indicates whether the field will be made readonly (<c>true</c>) or not (<c>false</c>).
/// </param>
public FieldBuilder MakeReadonly(bool makeReadonly = true)
{
Field = Field.With(isReadonly: Option.Some(makeReadonly));
return this;
}
/// <summary>
/// Adds XML summary documentation to the field.
/// </summary>
/// <param name="summary">
/// The content of the summary documentation.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="summary"/> is <c>null</c>.
/// </exception>
public FieldBuilder WithSummary(string summary)
{
if (summary is null)
throw new ArgumentNullException(nameof(summary));
Field = Field.With(summary: Option.Some(summary));
return this;
}
/// <summary>
/// Adds a type parameter to the field being built.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="builder"/> is <c>null</c>.
/// </exception>
public FieldBuilder WithTypeParameter(TypeParameterBuilder builder)
{
if (builder is null)
throw new ArgumentNullException(nameof(builder));
_typeParameters.Add(builder);
return this;
}
/// <summary>
/// Adds a bunch of type parameters to the field being built.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="builders"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// One of the specified <paramref name="builders"/> is <c>null</c>.
/// </exception>
public FieldBuilder WithTypeParameters(params TypeParameterBuilder[] builders)
{
if (builders is null)
throw new ArgumentNullException(nameof(builders));
if (builders.Any(x => x is null))
throw new ArgumentException("One of the type parameter builders is null.");
_typeParameters.AddRange(builders);
return this;
}
/// <summary>
/// Adds a bunch of type parameters to the field being built.
/// </summary>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="builders"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// One of the specified <paramref name="builders"/> is <c>null</c>.
/// </exception>
public FieldBuilder WithTypeParameters(IEnumerable<TypeParameterBuilder> builders)
{
if (builders is null)
throw new ArgumentNullException(nameof(builders));
if (builders.Any(x => x is null))
throw new ArgumentException("One of the type parameter builders is null.");
_typeParameters.AddRange(builders);
return this;
}
/// <summary>
/// Returns the source code of the built field.
/// </summary>
/// <exception cref="MissingBuilderSettingException">
/// A setting that is required to build a valid field structure is missing.
/// </exception>
/// <exception cref="SyntaxException">
/// The field builder is configured in such a way that the resulting code would be invalid.
/// </exception>
public string ToSourceCode() =>
Ast.Stringify(Ast.FromDefinition(Build()));
/// <summary>
/// Returns the source code of the built field.
/// </summary>
/// <exception cref="MissingBuilderSettingException">
/// A setting that is required to build a valid field structure is missing.
/// </exception>
/// <exception cref="SyntaxException">
/// The field builder is configured in such a way that the resulting code would be invalid.
/// </exception>
public override string ToString() =>
ToSourceCode();
internal Field Build()
{
if (string.IsNullOrWhiteSpace(Field.Type.ValueOrDefault()))
{
throw new MissingBuilderSettingException(
"Providing the type of the field is required when building a field.");
}
else if (string.IsNullOrWhiteSpace(Field.Name.ValueOrDefault()))
{
throw new MissingBuilderSettingException(
"Providing the name of the field is required when building a field.");
}
Field.TypeParameters.AddRange(_typeParameters.Select(builder => builder.Build()));
return Field;
}
}
}