| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | using NexusLabs.Needlr.Generators.Models; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Generators.CodeGen; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Generates IValidateOptions implementations for options classes with validation. |
| | | 13 | | /// </summary> |
| | | 14 | | internal static class OptionsCodeGenerator |
| | | 15 | | { |
| | | 16 | | internal static string GenerateOptionsValidatorsSource(IReadOnlyList<DiscoveredOptions> optionsWithValidators, strin |
| | | 17 | | { |
| | 18 | 18 | | var builder = new StringBuilder(); |
| | 18 | 19 | | var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier(assemblyName); |
| | | 20 | | |
| | 18 | 21 | | breadcrumbs.WriteFileHeader(builder, assemblyName, "Needlr Generated Options Validators"); |
| | 18 | 22 | | builder.AppendLine("#nullable enable"); |
| | 18 | 23 | | builder.AppendLine(); |
| | 18 | 24 | | builder.AppendLine("using System.Collections.Generic;"); |
| | 18 | 25 | | builder.AppendLine("using System.Linq;"); |
| | 18 | 26 | | builder.AppendLine(); |
| | 18 | 27 | | builder.AppendLine("using Microsoft.Extensions.Options;"); |
| | 18 | 28 | | builder.AppendLine(); |
| | 18 | 29 | | builder.AppendLine("using NexusLabs.Needlr.Generators;"); |
| | 18 | 30 | | builder.AppendLine(); |
| | 18 | 31 | | builder.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 18 | 32 | | builder.AppendLine(); |
| | | 33 | | |
| | | 34 | | // Generate validator class for each options type with a validator method |
| | 72 | 35 | | foreach (var opt in optionsWithValidators) |
| | | 36 | | { |
| | 18 | 37 | | if (!opt.HasValidatorMethod || opt.ValidatorMethod == null) |
| | | 38 | | continue; |
| | | 39 | | |
| | 18 | 40 | | var shortTypeName = GeneratorHelpers.GetShortTypeName(opt.TypeName); |
| | 18 | 41 | | var validatorClassName = shortTypeName + "Validator"; |
| | | 42 | | |
| | | 43 | | // Determine which type has the validator method |
| | 18 | 44 | | var validatorTargetType = opt.HasExternalValidator ? opt.ValidatorTypeName! : opt.TypeName; |
| | | 45 | | |
| | 18 | 46 | | builder.AppendLine("/// <summary>"); |
| | 18 | 47 | | builder.AppendLine($"/// Generated validator for <see cref=\"{opt.TypeName}\"/>."); |
| | 18 | 48 | | if (opt.HasExternalValidator) |
| | | 49 | | { |
| | 6 | 50 | | builder.AppendLine($"/// Uses external validator <see cref=\"{validatorTargetType}\"/>."); |
| | | 51 | | } |
| | | 52 | | else |
| | | 53 | | { |
| | 12 | 54 | | builder.AppendLine("/// Calls the validation method on the options instance."); |
| | | 55 | | } |
| | 18 | 56 | | builder.AppendLine("/// </summary>"); |
| | 18 | 57 | | builder.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.Generators\", |
| | 18 | 58 | | builder.AppendLine($"public sealed class {validatorClassName} : IValidateOptions<{opt.TypeName}>"); |
| | 18 | 59 | | builder.AppendLine("{"); |
| | | 60 | | |
| | 18 | 61 | | if (opt.HasExternalValidator && !opt.ValidatorMethod.Value.IsStatic) |
| | | 62 | | { |
| | | 63 | | // External validator needs to be injected for instance methods |
| | 4 | 64 | | builder.AppendLine($" private readonly {validatorTargetType} _validator;"); |
| | 4 | 65 | | builder.AppendLine(); |
| | 4 | 66 | | builder.AppendLine($" public {validatorClassName}({validatorTargetType} validator)"); |
| | 4 | 67 | | builder.AppendLine(" {"); |
| | 4 | 68 | | builder.AppendLine(" _validator = validator;"); |
| | 4 | 69 | | builder.AppendLine(" }"); |
| | 4 | 70 | | builder.AppendLine(); |
| | | 71 | | } |
| | | 72 | | |
| | 18 | 73 | | builder.AppendLine($" public ValidateOptionsResult Validate(string? name, {opt.TypeName} options)"); |
| | 18 | 74 | | builder.AppendLine(" {"); |
| | 18 | 75 | | builder.AppendLine(" var errors = new List<string>();"); |
| | | 76 | | |
| | | 77 | | // Generate the foreach to iterate errors |
| | | 78 | | string validationCall; |
| | 18 | 79 | | if (opt.HasExternalValidator) |
| | | 80 | | { |
| | 6 | 81 | | if (opt.ValidatorMethod.Value.IsStatic) |
| | | 82 | | { |
| | | 83 | | // Static method on external type: ExternalValidator.ValidateMethod(options) |
| | 2 | 84 | | validationCall = $"{validatorTargetType}.{opt.ValidatorMethod.Value.MethodName}(options)"; |
| | | 85 | | } |
| | | 86 | | else |
| | | 87 | | { |
| | | 88 | | // Instance method on external type: _validator.ValidateMethod(options) |
| | 4 | 89 | | validationCall = $"_validator.{opt.ValidatorMethod.Value.MethodName}(options)"; |
| | | 90 | | } |
| | | 91 | | } |
| | 12 | 92 | | else if (opt.ValidatorMethod.Value.IsStatic) |
| | | 93 | | { |
| | | 94 | | // Static method on options type: OptionsType.ValidateMethod(options) |
| | 1 | 95 | | validationCall = $"{opt.TypeName}.{opt.ValidatorMethod.Value.MethodName}(options)"; |
| | | 96 | | } |
| | | 97 | | else |
| | | 98 | | { |
| | | 99 | | // Instance method on options type: options.ValidateMethod() |
| | 11 | 100 | | validationCall = $"options.{opt.ValidatorMethod.Value.MethodName}()"; |
| | | 101 | | } |
| | | 102 | | |
| | 18 | 103 | | builder.AppendLine($" foreach (var error in {validationCall})"); |
| | 18 | 104 | | builder.AppendLine(" {"); |
| | 18 | 105 | | builder.AppendLine(" // Support both string and ValidationError (ValidationError.ToString() retur |
| | 18 | 106 | | builder.AppendLine(" var errorMessage = error?.ToString() ?? string.Empty;"); |
| | 18 | 107 | | builder.AppendLine(" if (!string.IsNullOrEmpty(errorMessage))"); |
| | 18 | 108 | | builder.AppendLine(" {"); |
| | 18 | 109 | | builder.AppendLine(" errors.Add(errorMessage);"); |
| | 18 | 110 | | builder.AppendLine(" }"); |
| | 18 | 111 | | builder.AppendLine(" }"); |
| | 18 | 112 | | builder.AppendLine(); |
| | 18 | 113 | | builder.AppendLine(" if (errors.Count > 0)"); |
| | 18 | 114 | | builder.AppendLine(" {"); |
| | 18 | 115 | | builder.AppendLine($" return ValidateOptionsResult.Fail(errors);"); |
| | 18 | 116 | | builder.AppendLine(" }"); |
| | 18 | 117 | | builder.AppendLine(); |
| | 18 | 118 | | builder.AppendLine(" return ValidateOptionsResult.Success;"); |
| | 18 | 119 | | builder.AppendLine(" }"); |
| | 18 | 120 | | builder.AppendLine("}"); |
| | 18 | 121 | | builder.AppendLine(); |
| | | 122 | | } |
| | | 123 | | |
| | 18 | 124 | | return builder.ToString(); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Generates IValidateOptions implementations for options classes with DataAnnotation attributes. |
| | | 129 | | /// This enables AOT-compatible validation without reflection. |
| | | 130 | | /// </summary> |
| | | 131 | | internal static string GenerateDataAnnotationsValidatorsSource( |
| | | 132 | | IReadOnlyList<DiscoveredOptions> optionsWithDataAnnotations, |
| | | 133 | | string assemblyName, |
| | | 134 | | BreadcrumbWriter breadcrumbs, |
| | | 135 | | string? projectDirectory) |
| | | 136 | | { |
| | 18 | 137 | | var builder = new StringBuilder(); |
| | 18 | 138 | | var safeAssemblyName = GeneratorHelpers.SanitizeIdentifier(assemblyName); |
| | | 139 | | |
| | 18 | 140 | | breadcrumbs.WriteFileHeader(builder, assemblyName, "Needlr Generated DataAnnotations Validators"); |
| | 18 | 141 | | builder.AppendLine("#nullable enable"); |
| | 18 | 142 | | builder.AppendLine(); |
| | 18 | 143 | | builder.AppendLine("using System.Collections.Generic;"); |
| | 18 | 144 | | builder.AppendLine("using System.Text.RegularExpressions;"); |
| | 18 | 145 | | builder.AppendLine(); |
| | 18 | 146 | | builder.AppendLine("using Microsoft.Extensions.Options;"); |
| | 18 | 147 | | builder.AppendLine(); |
| | 18 | 148 | | builder.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 18 | 149 | | builder.AppendLine(); |
| | | 150 | | |
| | 74 | 151 | | foreach (var opt in optionsWithDataAnnotations) |
| | | 152 | | { |
| | 19 | 153 | | if (!opt.HasDataAnnotations) |
| | | 154 | | continue; |
| | | 155 | | |
| | 19 | 156 | | var shortTypeName = GeneratorHelpers.GetShortTypeName(opt.TypeName); |
| | 19 | 157 | | var validatorClassName = shortTypeName + "DataAnnotationsValidator"; |
| | | 158 | | |
| | 19 | 159 | | builder.AppendLine("/// <summary>"); |
| | 19 | 160 | | builder.AppendLine($"/// Generated DataAnnotations validator for <see cref=\"{opt.TypeName}\"/>."); |
| | 19 | 161 | | builder.AppendLine("/// Validates DataAnnotation attributes without reflection (AOT-compatible)."); |
| | 19 | 162 | | builder.AppendLine("/// </summary>"); |
| | 19 | 163 | | builder.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.Generators\", |
| | 19 | 164 | | builder.AppendLine($"public sealed class {validatorClassName} : IValidateOptions<{opt.TypeName}>"); |
| | 19 | 165 | | builder.AppendLine("{"); |
| | 19 | 166 | | builder.AppendLine($" public ValidateOptionsResult Validate(string? name, {opt.TypeName} options)"); |
| | 19 | 167 | | builder.AppendLine(" {"); |
| | 19 | 168 | | builder.AppendLine(" var errors = new List<string>();"); |
| | 19 | 169 | | builder.AppendLine(); |
| | | 170 | | |
| | | 171 | | // Generate validation for each property with DataAnnotations |
| | 82 | 172 | | foreach (var prop in opt.Properties) |
| | | 173 | | { |
| | 22 | 174 | | if (!prop.HasDataAnnotations) |
| | | 175 | | continue; |
| | | 176 | | |
| | 90 | 177 | | foreach (var annotation in prop.DataAnnotations) |
| | | 178 | | { |
| | 23 | 179 | | GenerateDataAnnotationValidation(builder, prop, annotation); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | 19 | 183 | | builder.AppendLine(" if (errors.Count > 0)"); |
| | 19 | 184 | | builder.AppendLine(" {"); |
| | 19 | 185 | | builder.AppendLine(" return ValidateOptionsResult.Fail(errors);"); |
| | 19 | 186 | | builder.AppendLine(" }"); |
| | 19 | 187 | | builder.AppendLine(); |
| | 19 | 188 | | builder.AppendLine(" return ValidateOptionsResult.Success;"); |
| | 19 | 189 | | builder.AppendLine(" }"); |
| | 19 | 190 | | builder.AppendLine("}"); |
| | 19 | 191 | | builder.AppendLine(); |
| | | 192 | | } |
| | | 193 | | |
| | 18 | 194 | | return builder.ToString(); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | private static void GenerateDataAnnotationValidation(StringBuilder builder, OptionsPropertyInfo prop, DataAnnotation |
| | | 198 | | { |
| | 23 | 199 | | var propName = prop.Name; |
| | 23 | 200 | | var errorMsg = annotation.ErrorMessage; |
| | | 201 | | |
| | 23 | 202 | | switch (annotation.Kind) |
| | | 203 | | { |
| | | 204 | | case DataAnnotationKind.Required: |
| | 12 | 205 | | var requiredError = errorMsg ?? $"The {propName} field is required."; |
| | 12 | 206 | | if (prop.TypeName.Contains("string")) |
| | | 207 | | { |
| | 12 | 208 | | builder.AppendLine($" if (string.IsNullOrEmpty(options.{propName}))"); |
| | | 209 | | } |
| | | 210 | | else |
| | | 211 | | { |
| | 0 | 212 | | builder.AppendLine($" if (options.{propName} == null)"); |
| | | 213 | | } |
| | 12 | 214 | | builder.AppendLine(" {"); |
| | 12 | 215 | | builder.AppendLine($" errors.Add(\"{EscapeString(requiredError)}\");"); |
| | 12 | 216 | | builder.AppendLine(" }"); |
| | 12 | 217 | | builder.AppendLine(); |
| | 12 | 218 | | break; |
| | | 219 | | |
| | | 220 | | case DataAnnotationKind.Range: |
| | 6 | 221 | | var min = annotation.Minimum; |
| | 6 | 222 | | var max = annotation.Maximum; |
| | 6 | 223 | | var rangeError = errorMsg ?? $"The field {propName} must be between {min} and {max}."; |
| | 6 | 224 | | builder.AppendLine($" if (options.{propName} < {min} || options.{propName} > {max})"); |
| | 6 | 225 | | builder.AppendLine(" {"); |
| | 6 | 226 | | builder.AppendLine($" errors.Add(\"{EscapeString(rangeError)}\");"); |
| | 6 | 227 | | builder.AppendLine(" }"); |
| | 6 | 228 | | builder.AppendLine(); |
| | 6 | 229 | | break; |
| | | 230 | | |
| | | 231 | | case DataAnnotationKind.StringLength: |
| | 2 | 232 | | var maxLen = annotation.Maximum; |
| | 2 | 233 | | var minLen = annotation.MinimumLength ?? 0; |
| | 2 | 234 | | var strLenError = errorMsg ?? $"The field {propName} must be a string with a maximum length of {maxLen}. |
| | 2 | 235 | | if (minLen > 0) |
| | | 236 | | { |
| | 2 | 237 | | builder.AppendLine($" if (options.{propName}?.Length < {minLen} || options.{propName}?.Length |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 0 | 241 | | builder.AppendLine($" if (options.{propName}?.Length > {maxLen})"); |
| | | 242 | | } |
| | 2 | 243 | | builder.AppendLine(" {"); |
| | 2 | 244 | | builder.AppendLine($" errors.Add(\"{EscapeString(strLenError)}\");"); |
| | 2 | 245 | | builder.AppendLine(" }"); |
| | 2 | 246 | | builder.AppendLine(); |
| | 2 | 247 | | break; |
| | | 248 | | |
| | | 249 | | case DataAnnotationKind.MinLength: |
| | 1 | 250 | | var minLenVal = annotation.MinimumLength ?? 0; |
| | 1 | 251 | | var minLenError = errorMsg ?? $"The field {propName} must have a minimum length of {minLenVal}."; |
| | 1 | 252 | | builder.AppendLine($" if (options.{propName}?.Length < {minLenVal})"); |
| | 1 | 253 | | builder.AppendLine(" {"); |
| | 1 | 254 | | builder.AppendLine($" errors.Add(\"{EscapeString(minLenError)}\");"); |
| | 1 | 255 | | builder.AppendLine(" }"); |
| | 1 | 256 | | builder.AppendLine(); |
| | 1 | 257 | | break; |
| | | 258 | | |
| | | 259 | | case DataAnnotationKind.MaxLength: |
| | 1 | 260 | | var maxLenVal = annotation.Maximum; |
| | 1 | 261 | | var maxLenError = errorMsg ?? $"The field {propName} must have a maximum length of {maxLenVal}."; |
| | 1 | 262 | | builder.AppendLine($" if (options.{propName}?.Length > {maxLenVal})"); |
| | 1 | 263 | | builder.AppendLine(" {"); |
| | 1 | 264 | | builder.AppendLine($" errors.Add(\"{EscapeString(maxLenError)}\");"); |
| | 1 | 265 | | builder.AppendLine(" }"); |
| | 1 | 266 | | builder.AppendLine(); |
| | 1 | 267 | | break; |
| | | 268 | | |
| | | 269 | | case DataAnnotationKind.RegularExpression: |
| | 1 | 270 | | var pattern = annotation.Pattern ?? ""; |
| | 1 | 271 | | var regexError = errorMsg ?? $"The field {propName} must match the regular expression '{pattern}'."; |
| | | 272 | | // Use verbatim string for pattern |
| | 1 | 273 | | builder.AppendLine($" if (options.{propName} != null && !Regex.IsMatch(options.{propName}, @\"{Es |
| | 1 | 274 | | builder.AppendLine(" {"); |
| | 1 | 275 | | builder.AppendLine($" errors.Add(\"{EscapeString(regexError)}\");"); |
| | 1 | 276 | | builder.AppendLine(" }"); |
| | 1 | 277 | | builder.AppendLine(); |
| | 1 | 278 | | break; |
| | | 279 | | |
| | | 280 | | case DataAnnotationKind.EmailAddress: |
| | 0 | 281 | | var emailError = errorMsg ?? $"The {propName} field is not a valid e-mail address."; |
| | 0 | 282 | | builder.AppendLine($" if (!string.IsNullOrEmpty(options.{propName}) && !Regex.IsMatch(options.{pr |
| | 0 | 283 | | builder.AppendLine(" {"); |
| | 0 | 284 | | builder.AppendLine($" errors.Add(\"{EscapeString(emailError)}\");"); |
| | 0 | 285 | | builder.AppendLine(" }"); |
| | 0 | 286 | | builder.AppendLine(); |
| | 0 | 287 | | break; |
| | | 288 | | |
| | | 289 | | case DataAnnotationKind.Url: |
| | 0 | 290 | | var urlError = errorMsg ?? $"The {propName} field is not a valid fully-qualified http, https, or ftp URL |
| | 0 | 291 | | builder.AppendLine($" if (!string.IsNullOrEmpty(options.{propName}) && !global::System.Uri.TryCre |
| | 0 | 292 | | builder.AppendLine(" {"); |
| | 0 | 293 | | builder.AppendLine($" errors.Add(\"{EscapeString(urlError)}\");"); |
| | 0 | 294 | | builder.AppendLine(" }"); |
| | 0 | 295 | | builder.AppendLine(); |
| | 0 | 296 | | break; |
| | | 297 | | |
| | | 298 | | case DataAnnotationKind.Phone: |
| | 0 | 299 | | var phoneError = errorMsg ?? $"The {propName} field is not a valid phone number."; |
| | 0 | 300 | | builder.AppendLine($" if (!string.IsNullOrEmpty(options.{propName}) && !Regex.IsMatch(options.{pr |
| | 0 | 301 | | builder.AppendLine(" {"); |
| | 0 | 302 | | builder.AppendLine($" errors.Add(\"{EscapeString(phoneError)}\");"); |
| | 0 | 303 | | builder.AppendLine(" }"); |
| | 0 | 304 | | builder.AppendLine(); |
| | | 305 | | break; |
| | | 306 | | |
| | | 307 | | case DataAnnotationKind.Unsupported: |
| | | 308 | | // Skip unsupported - will be handled by analyzer |
| | | 309 | | break; |
| | | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | private static string EscapeString(string s) |
| | | 314 | | { |
| | 23 | 315 | | return s.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | private static string EscapeVerbatimString(string s) |
| | | 319 | | { |
| | 1 | 320 | | return s.Replace("\"", "\"\""); |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Generates parameterless constructors for partial positional records with [Options]. |
| | | 325 | | /// This enables configuration binding which requires a parameterless constructor. |
| | | 326 | | /// </summary> |
| | | 327 | | internal static string GeneratePositionalRecordConstructorsSource( |
| | | 328 | | IReadOnlyList<DiscoveredOptions> optionsNeedingConstructors, |
| | | 329 | | string assemblyName, |
| | | 330 | | BreadcrumbWriter breadcrumbs, |
| | | 331 | | string? projectDirectory) |
| | | 332 | | { |
| | 7 | 333 | | var builder = new StringBuilder(); |
| | | 334 | | |
| | 7 | 335 | | breadcrumbs.WriteFileHeader(builder, assemblyName, "Needlr Generated Options Constructors"); |
| | 7 | 336 | | builder.AppendLine("#nullable enable"); |
| | 7 | 337 | | builder.AppendLine(); |
| | | 338 | | |
| | | 339 | | // Group by namespace for cleaner output |
| | 7 | 340 | | var byNamespace = optionsNeedingConstructors |
| | 7 | 341 | | .Where(o => o.PositionalRecordInfo != null) |
| | 7 | 342 | | .GroupBy(o => o.PositionalRecordInfo!.Value.ContainingNamespace) |
| | 14 | 343 | | .OrderBy(g => g.Key); |
| | | 344 | | |
| | 28 | 345 | | foreach (var namespaceGroup in byNamespace) |
| | | 346 | | { |
| | 7 | 347 | | var namespaceName = namespaceGroup.Key; |
| | | 348 | | |
| | 7 | 349 | | if (!string.IsNullOrEmpty(namespaceName)) |
| | | 350 | | { |
| | 7 | 351 | | builder.AppendLine($"namespace {namespaceName};"); |
| | 7 | 352 | | builder.AppendLine(); |
| | | 353 | | } |
| | | 354 | | |
| | 28 | 355 | | foreach (var opt in namespaceGroup) |
| | | 356 | | { |
| | 7 | 357 | | var info = opt.PositionalRecordInfo!.Value; |
| | | 358 | | |
| | 7 | 359 | | builder.AppendLine("/// <summary>"); |
| | 7 | 360 | | builder.AppendLine($"/// Generated parameterless constructor for configuration binding."); |
| | 7 | 361 | | builder.AppendLine($"/// Chains to primary constructor with default values."); |
| | 7 | 362 | | builder.AppendLine("/// </summary>"); |
| | 7 | 363 | | builder.AppendLine($"[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.Generato |
| | 7 | 364 | | builder.AppendLine($"public partial record {info.ShortTypeName}"); |
| | 7 | 365 | | builder.AppendLine("{"); |
| | | 366 | | |
| | | 367 | | // Build the constructor call with default values for each parameter |
| | 7 | 368 | | var defaultArgs = new List<string>(); |
| | 52 | 369 | | foreach (var param in info.Parameters) |
| | | 370 | | { |
| | 19 | 371 | | var defaultValue = GetDefaultValueForType(param.TypeName); |
| | 19 | 372 | | defaultArgs.Add(defaultValue); |
| | | 373 | | } |
| | | 374 | | |
| | 7 | 375 | | var argsString = string.Join(", ", defaultArgs); |
| | 7 | 376 | | builder.AppendLine($" public {info.ShortTypeName}() : this({argsString}) {{ }}"); |
| | 7 | 377 | | builder.AppendLine("}"); |
| | 7 | 378 | | builder.AppendLine(); |
| | | 379 | | } |
| | | 380 | | } |
| | | 381 | | |
| | 7 | 382 | | return builder.ToString(); |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Gets the default value expression for a given type. |
| | | 387 | | /// </summary> |
| | | 388 | | private static string GetDefaultValueForType(string fullyQualifiedTypeName) |
| | | 389 | | { |
| | | 390 | | // Handle common types with user-friendly defaults |
| | 19 | 391 | | return fullyQualifiedTypeName switch |
| | 19 | 392 | | { |
| | 7 | 393 | | "global::System.String" or "string" => "string.Empty", |
| | 4 | 394 | | "global::System.Boolean" or "bool" => "default", |
| | 6 | 395 | | "global::System.Int32" or "int" => "default", |
| | 0 | 396 | | "global::System.Int64" or "long" => "default", |
| | 0 | 397 | | "global::System.Int16" or "short" => "default", |
| | 0 | 398 | | "global::System.Byte" or "byte" => "default", |
| | 0 | 399 | | "global::System.SByte" or "sbyte" => "default", |
| | 0 | 400 | | "global::System.UInt32" or "uint" => "default", |
| | 0 | 401 | | "global::System.UInt64" or "ulong" => "default", |
| | 0 | 402 | | "global::System.UInt16" or "ushort" => "default", |
| | 0 | 403 | | "global::System.Single" or "float" => "default", |
| | 1 | 404 | | "global::System.Double" or "double" => "default", |
| | 0 | 405 | | "global::System.Decimal" or "decimal" => "default", |
| | 0 | 406 | | "global::System.Char" or "char" => "default", |
| | 0 | 407 | | "global::System.DateTime" => "default", |
| | 0 | 408 | | "global::System.DateTimeOffset" => "default", |
| | 0 | 409 | | "global::System.TimeSpan" => "default", |
| | 0 | 410 | | "global::System.Guid" => "default", |
| | 19 | 411 | | // For nullable types and reference types, use default (which gives null for reference types) |
| | 19 | 412 | | // For other value types, use default |
| | 1 | 413 | | _ when fullyQualifiedTypeName.EndsWith("?") => "default", |
| | 1 | 414 | | _ => "default!" // Reference types need null-forgiving operator |
| | 19 | 415 | | }; |
| | | 416 | | } |
| | | 417 | | } |
| | | 418 | | |