-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParameterBuilder.cs
More file actions
218 lines (190 loc) · 6.69 KB
/
ParameterBuilder.cs
File metadata and controls
218 lines (190 loc) · 6.69 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
218
using System;
using System.Collections.Generic;
using Ultz.Extensions.Commands.Attributes.Checks;
using Ultz.Extensions.Commands.Built;
using Ultz.Extensions.Commands.Parsing.TypeParsers;
namespace Ultz.Extensions.Commands.Builders
{
/// <summary>
/// Allows for building <see cref="Parameter" />s using the <see cref="CommandService" />.
/// </summary>
public sealed class ParameterBuilder
{
internal ParameterBuilder(Type type, CommandBuilder command)
{
Type = type;
Command = command;
Checks = new List<ParameterCheckAttribute>();
Attributes = new List<Attribute>();
}
/// <summary>
/// Gets or sets the name of the <see cref="Parameter" />.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the description of the <see cref="Parameter" />.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the remarks of the <see cref="Parameter" />.
/// </summary>
public string Remarks { get; set; }
/// <summary>
/// Gets or sets whether the <see cref="Parameter" /> is multiple or not.
/// </summary>
public bool IsMultiple { get; set; }
/// <summary>
/// Gets or sets whether the <see cref="Parameter" /> is optional or not.
/// </summary>
public bool IsOptional { get; set; }
/// <summary>
/// Gets or sets whether the <see cref="Parameter" /> is a remainder parameter or not.
/// </summary>
public bool IsRemainder { get; set; }
/// <summary>
/// Gets or sets the default value of the <see cref="Parameter" />.
/// </summary>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets the <see cref="System.Type" /> of a custom <see cref="TypeParser{T}" />.
/// </summary>
public Type CustomTypeParserType { get; set; }
/// <summary>
/// Gets the checks of the <see cref="Parameter" />.
/// </summary>
public List<ParameterCheckAttribute> Checks { get; }
/// <summary>
/// Gets the attributes of the <see cref="Parameter" />.
/// </summary>
public List<Attribute> Attributes { get; }
/// <summary>
/// Gets the <see cref="System.Type" /> of the <see cref="Parameter" />.
/// </summary>
public Type Type { get; internal set; }
/// <summary>
/// Gets the command of the <see cref="Parameter" />.
/// </summary>
public CommandBuilder Command { get; }
/// <summary>
/// Sets the <see cref="Name" />.
/// </summary>
public ParameterBuilder WithName(string name)
{
Name = name;
return this;
}
/// <summary>
/// Sets the <see cref="Description" />.
/// </summary>
public ParameterBuilder WithDescription(string description)
{
Description = description;
return this;
}
/// <summary>
/// Sets the <see cref="Remarks" />.
/// </summary>
public ParameterBuilder WithRemarks(string remarks)
{
Remarks = remarks;
return this;
}
/// <summary>
/// Sets the <see cref="IsMultiple" />.
/// </summary>
public ParameterBuilder WithIsMultiple(bool isMultiple)
{
IsMultiple = isMultiple;
return this;
}
/// <summary>
/// Sets the <see cref="IsOptional" />.
/// </summary>
public ParameterBuilder WithIsOptional(bool isOptional)
{
IsOptional = isOptional;
return this;
}
/// <summary>
/// Sets the <see cref="IsRemainder" />.
/// </summary>
public ParameterBuilder WithIsRemainder(bool isRemainder)
{
IsRemainder = isRemainder;
return this;
}
/// <summary>
/// Sets the <see cref="DefaultValue" />.
/// </summary>
public ParameterBuilder WithDefaultValue(object defaultValue)
{
DefaultValue = defaultValue;
return this;
}
/// <summary>
/// Sets the <see cref="CustomTypeParserType" />.
/// </summary>
public ParameterBuilder WithCustomTypeParserType(Type customTypeParserType)
{
CustomTypeParserType = customTypeParserType;
return this;
}
/// <summary>
/// Adds a check to <see cref="Checks" />.
/// </summary>
public ParameterBuilder AddCheck(ParameterCheckAttribute check)
{
Checks.Add(check);
return this;
}
/// <summary>
/// Adds checks to <see cref="Checks" />.
/// </summary>
public ParameterBuilder AddChecks(params ParameterCheckAttribute[] checks)
{
Checks.AddRange(checks);
return this;
}
/// <summary>
/// Adds an attribute to <see cref="Attributes" />.
/// </summary>
public ParameterBuilder AddAttribute(Attribute attribute)
{
Attributes.Add(attribute);
return this;
}
/// <summary>
/// Adds attributes to <see cref="Attributes" />.
/// </summary>
public ParameterBuilder AddAttributes(params Attribute[] attributes)
{
Attributes.AddRange(attributes);
return this;
}
internal Parameter Build(Command command)
{
if (CustomTypeParserType != null && !Utilities.IsValidTypeParserDefinition(CustomTypeParserType, Type))
{
throw new ParameterBuildingException(this,
$"{CustomTypeParserType} is not a valid type parser for parameter of type {Type}.");
}
if (IsOptional)
{
if (DefaultValue == null)
{
if (Type.IsValueType && !Utilities.IsNullable(Type))
{
throw new ParameterBuildingException(this,
"Value type parameter's default value must not be null.");
}
}
else if (DefaultValue.GetType() != (IsMultiple ? Type.MakeArrayType() : Type))
{
throw new ParameterBuildingException(this,
$"Parameter type and default value mismatch. Expected {Type}, got {DefaultValue.GetType()}.");
}
}
return new Parameter(this, command);
}
}
}