Skip to content

Commit 20be7a4

Browse files
committed
Use records for models
1 parent 09fd9f5 commit 20be7a4

File tree

2 files changed

+37
-73
lines changed

2 files changed

+37
-73
lines changed

src/EndpointHelpers/ControllerGeneratorBase.cs

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -143,36 +143,38 @@ private static ImmutableArray<ControllerModel> SelectControllers(
143143
.Select(static group => group.First())
144144
.ToArray();
145145

146-
return [
146+
return
147+
[
147148
..controllers
148149
.Where(controller => assemblyHasGenerate
149150
? controller.Name.EndsWith("Controller", StringComparison.Ordinal)
150-
: controller.ClassHasGenerateAttribute || controller.Methods.Any(static method => method.HasGenerateAttribute))
151-
.Select(controller => new ControllerModel(
152-
controller.MetadataName,
153-
controller.TypeName,
154-
controller.Name,
155-
controller.ClassHasGenerateAttribute,
151+
: controller.ClassHasGenerateAttribute ||
152+
controller.Methods.Any(static method => method.HasGenerateAttribute))
153+
.Select(controller => controller with
154+
{
155+
Methods =
156156
[
157157
..controller.Methods
158-
.Where(method => assemblyHasGenerate || controller.ClassHasGenerateAttribute || method.HasGenerateAttribute)
159-
]))
158+
.Where(method =>
159+
assemblyHasGenerate || controller.ClassHasGenerateAttribute ||
160+
method.HasGenerateAttribute)
161+
]
162+
})
160163
.Where(static controller => controller.Methods.Length > 0)
161164
];
162165
}
163166

164-
protected static bool HasAnyAttribute(ISymbol symbol, params string[] fullMetadataNames)
167+
private static bool HasAnyAttribute(ISymbol symbol, params string[] fullMetadataNames)
165168
{
166169
foreach (var attribute in symbol.GetAttributes())
167170
{
168171
var name = attribute.AttributeClass?.ToDisplayString();
169172
if (name is null)
170173
continue;
171174

172-
foreach (var metadataName in fullMetadataNames)
175+
if (fullMetadataNames.Any(metadataName => name == metadataName))
173176
{
174-
if (name == metadataName)
175-
return true;
177+
return true;
176178
}
177179
}
178180

@@ -215,63 +217,21 @@ private static string GetMetadataName(INamedTypeSymbol type)
215217
return string.IsNullOrEmpty(ns) ? name : $"{ns}.{name}";
216218
}
217219

218-
protected sealed class ControllerModel
219-
{
220-
public ControllerModel(
221-
string metadataName,
222-
string typeName,
223-
string name,
224-
bool classHasGenerateAttribute,
225-
ImmutableArray<ActionModel> methods)
226-
{
227-
MetadataName = metadataName;
228-
TypeName = typeName;
229-
Name = name;
230-
ClassHasGenerateAttribute = classHasGenerateAttribute;
231-
Methods = methods;
232-
}
233-
234-
public string MetadataName { get; }
235-
public string TypeName { get; }
236-
public string Name { get; }
237-
public bool ClassHasGenerateAttribute { get; }
238-
public ImmutableArray<ActionModel> Methods { get; }
239-
}
240-
241-
protected sealed class ActionModel
242-
{
243-
public ActionModel(
244-
string name,
245-
bool hasGenerateAttribute,
246-
ImmutableArray<ParameterModel> parameters)
247-
{
248-
Name = name;
249-
HasGenerateAttribute = hasGenerateAttribute;
250-
Parameters = parameters;
251-
}
252-
253-
public string Name { get; }
254-
public bool HasGenerateAttribute { get; }
255-
public ImmutableArray<ParameterModel> Parameters { get; }
256-
}
257-
258-
protected sealed class ParameterModel
259-
{
260-
public ParameterModel(
261-
string typeName,
262-
string name,
263-
bool isOptional,
264-
string defaultValueLiteral)
265-
{
266-
TypeName = typeName;
267-
Name = name;
268-
IsOptional = isOptional;
269-
DefaultValueLiteral = defaultValueLiteral;
270-
}
271-
272-
public string TypeName { get; }
273-
public string Name { get; }
274-
public bool IsOptional { get; }
275-
public string DefaultValueLiteral { get; }
276-
}
277-
}
220+
protected sealed record ControllerModel(
221+
string MetadataName,
222+
string TypeName,
223+
string Name,
224+
bool ClassHasGenerateAttribute,
225+
ImmutableArray<ActionModel> Methods);
226+
227+
protected sealed record ActionModel(
228+
string Name,
229+
bool HasGenerateAttribute,
230+
ImmutableArray<ParameterModel> Parameters);
231+
232+
protected sealed record ParameterModel(
233+
string TypeName,
234+
string Name,
235+
bool IsOptional,
236+
string DefaultValueLiteral);
237+
}

src/EndpointHelpers/EndpointHelpers.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4747
</PackageReference>
4848
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
49+
<PackageReference Include="PolySharp" Version="1.15.0">
50+
<PrivateAssets>all</PrivateAssets>
51+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
52+
</PackageReference>
4953
</ItemGroup>
5054

5155
</Project>

0 commit comments

Comments
 (0)