com.wallstop-studios.dxmessaging 2.0.0-rc26.3 → 2.0.0-rc26.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ namespace DxMessaging.Core.Attributes
2
+ {
3
+ using System;
4
+
5
+ [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
6
+ public sealed class DxOptionalParameterAttribute : Attribute
7
+ {
8
+ public DxOptionalParameterAttribute() { }
9
+ }
10
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 87d96cdf2fc74f40ba77ae0d157be964
3
+ timeCreated: 1749850608
@@ -18,6 +18,9 @@
18
18
  private const string AutoGenConstructorAttrFullName =
19
19
  "DxMessaging.Core.Attributes.DxAutoConstructorAttribute";
20
20
 
21
+ private const string OptionalParameterAttrFullName =
22
+ "DxMessaging.Core.Attributes.DxOptionalParameterAttribute";
23
+
21
24
  // Info needed during generation for a valid type
22
25
  private record struct TypeToGenerateInfo(
23
26
  INamedTypeSymbol TypeSymbol,
@@ -143,7 +146,7 @@
143
146
 
144
147
  foreach (TypeToGenerateInfo typeInfo in typesToGenerate)
145
148
  {
146
- if (!processedTypes.Add(typeInfo.TypeSymbol))
149
+ if (!processedTypes.Add(typeInfo.TypeSymbol) || typeInfo.FieldsToInject.Length == 0)
147
150
  {
148
151
  continue; // Already processed this type (e.g., from another partial definition)
149
152
  }
@@ -175,9 +178,9 @@
175
178
  ? string.Empty
176
179
  : typeSymbol.ContainingNamespace.ToDisplayString();
177
180
  string namespaceBlockOpen = string.IsNullOrEmpty(namespaceName)
178
- ? "namespace\n{"
181
+ ? string.Empty
179
182
  : $"namespace {namespaceName}\n{{";
180
- const string namespaceBlockClose = "}";
183
+ string namespaceBlockClose = string.IsNullOrEmpty(namespaceName) ? string.Empty : "}";
181
184
  const string indent = " ";
182
185
 
183
186
  string typeName = typeSymbol.ToDisplayString(
@@ -212,18 +215,39 @@
212
215
  SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers
213
216
  );
214
217
 
215
- for (int i = 0; i < fieldsToInject.Length; i++)
218
+ List<(string Type, string Name, bool IsOptional)> parameterDetails = [];
219
+
220
+ foreach (IFieldSymbol field in fieldsToInject)
216
221
  {
217
- IFieldSymbol field = fieldsToInject[i];
218
222
  string fieldType = field.Type.ToDisplayString(fieldTypeFormat);
219
223
  string fieldName = field.Name;
224
+ bool isOptional = field
225
+ .GetAttributes()
226
+ .Any(attr =>
227
+ string.Equals(
228
+ attr.AttributeClass?.ToDisplayString(),
229
+ OptionalParameterAttrFullName,
230
+ StringComparison.Ordinal
231
+ )
232
+ );
233
+
234
+ parameterDetails.Add((fieldType, fieldName, isOptional));
235
+ constructorBody.AppendLine($"{indent}{indent} this.{fieldName} = {fieldName};");
236
+ }
237
+
238
+ for (int i = 0; i < parameterDetails.Count; i++)
239
+ {
240
+ (string Type, string Name, bool IsOptional) p = parameterDetails[i];
241
+ constructorParams.Append($"{p.Type} {p.Name}");
242
+ if (p.IsOptional)
243
+ {
244
+ constructorParams.Append(" = default");
245
+ }
220
246
 
221
- constructorParams.Append($"{fieldType} {fieldName}");
222
- if (i < fieldsToInject.Length - 1)
247
+ if (i < parameterDetails.Count - 1)
223
248
  {
224
249
  constructorParams.Append(", ");
225
250
  }
226
- constructorBody.AppendLine($"{indent}{indent} this.{fieldName} = {fieldName};");
227
251
  }
228
252
 
229
253
  return $$"""
@@ -258,9 +258,9 @@
258
258
  ? string.Empty
259
259
  : typeSymbol.ContainingNamespace.ToDisplayString();
260
260
  string namespaceBlockOpen = string.IsNullOrEmpty(namespaceName)
261
- ? "namespace\n{"
261
+ ? string.Empty
262
262
  : $"namespace {namespaceName}\n{{";
263
- const string namespaceBlockClose = "}";
263
+ string namespaceBlockClose = string.IsNullOrEmpty(namespaceName) ? string.Empty : "}";
264
264
  const string indent = " ";
265
265
 
266
266
  string typeNameWithGenerics = typeSymbol.ToDisplayString(
@@ -279,10 +279,12 @@
279
279
 
280
280
  string accessibility = typeSymbol.DeclaredAccessibility switch
281
281
  {
282
- Accessibility.Public => "public ",
283
- Accessibility.Internal => "internal ",
282
+ Accessibility.Public => "public",
283
+ Accessibility.Protected => "protected",
284
+ Accessibility.Private => "private",
285
+ Accessibility.Internal => "internal",
284
286
  // Add others if necessary, default to internal if restrictive
285
- _ => "internal ",
287
+ _ => "internal",
286
288
  };
287
289
 
288
290
  string interfaceDeclaration = $", global::{targetInterfaceFullName}";
@@ -294,7 +296,7 @@
294
296
 
295
297
  {{namespaceBlockOpen}}
296
298
  {{indent}}// Partial implementation for {{typeNameWithGenerics}} to implement {{BaseInterfaceFullName}}
297
- {{indent}}{{accessibility}}partial {{typeKind}} {{typeNameWithGenerics}} : global::{{BaseInterfaceFullName}} {{interfaceDeclaration}}
299
+ {{indent}}{{accessibility}} partial {{typeKind}} {{typeNameWithGenerics}} : global::{{BaseInterfaceFullName}} {{interfaceDeclaration}}
298
300
  {{indent}}{
299
301
  {{indent}} /// <inheritdoc/>
300
302
  {{indent}} public global::System.Type MessageType => typeof({{fullyQualifiedName}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.dxmessaging",
3
- "version": "2.0.0-rc26.3",
3
+ "version": "2.0.0-rc26.5",
4
4
  "displayName": "DxMessaging",
5
5
  "description": "Synchronous Event Bus for Unity",
6
6
  "unity": "2021.3",
@@ -35,3 +35,5 @@
35
35
 
36
36
 
37
37
 
38
+
39
+