diff --git a/src/Dhgms.Nucleotide.GenerationTests/Dhgms.Nucleotide.SampleApp.csproj b/src/Dhgms.Nucleotide.GenerationTests/Dhgms.Nucleotide.SampleApp.csproj index ac04a9f3..f1711e35 100644 --- a/src/Dhgms.Nucleotide.GenerationTests/Dhgms.Nucleotide.SampleApp.csproj +++ b/src/Dhgms.Nucleotide.GenerationTests/Dhgms.Nucleotide.SampleApp.csproj @@ -2,8 +2,9 @@ net8.0-windows10.0.19041 - 9 + latest false + enable diff --git a/src/Dhgms.Nucleotide.Generators/Features/Model/KeyedModelClassGeneratorProcessor.cs b/src/Dhgms.Nucleotide.Generators/Features/Model/KeyedModelClassGeneratorProcessor.cs index fcbe3e7a..65e52a45 100644 --- a/src/Dhgms.Nucleotide.Generators/Features/Model/KeyedModelClassGeneratorProcessor.cs +++ b/src/Dhgms.Nucleotide.Generators/Features/Model/KeyedModelClassGeneratorProcessor.cs @@ -67,7 +67,7 @@ protected override IEnumerable GetPropertyDeclaration return new[] { - GetReadOnlyPropertyDeclaration(idColumn) + GetPropertyDeclaration(idColumn) }; } diff --git a/src/Dhgms.Nucleotide.Generators/Features/Model/UnkeyedModelClassGeneratorProcessor.cs b/src/Dhgms.Nucleotide.Generators/Features/Model/UnkeyedModelClassGeneratorProcessor.cs index d73fbd18..1c1c521e 100644 --- a/src/Dhgms.Nucleotide.Generators/Features/Model/UnkeyedModelClassGeneratorProcessor.cs +++ b/src/Dhgms.Nucleotide.Generators/Features/Model/UnkeyedModelClassGeneratorProcessor.cs @@ -77,7 +77,12 @@ private void DoPropertyDeclarations(InterfaceGenerationModel interfaceGeneration private PropertyDeclarationSyntax GetPropertyDeclaration(PropertyGenerationModel propertyGenerationModel) { - var type = SyntaxFactory.ParseName(propertyGenerationModel.TypeName); + var type = SyntaxFactory.ParseTypeName(propertyGenerationModel.TypeName); + if (propertyGenerationModel.Optional) + { + type = SyntaxFactory.NullableType(type); + } + var identifier = propertyGenerationModel.Name; var summary = new[] @@ -94,6 +99,11 @@ private PropertyDeclarationSyntax GetPropertyDeclaration(PropertyGenerationModel SyntaxFactory.List(accessorList) )) .WithLeadingTrivia(summary); + // HACK: will rewrite this once I replace the type with ISymbol + if (!propertyGenerationModel.Optional && propertyGenerationModel.TypeName.Equals("string")) + { + result = result.AddModifiers(SyntaxFactory.Token(SyntaxKind.RequiredKeyword)); + } return result; } @@ -111,9 +121,14 @@ private static AccessorDeclarationSyntax[] GetPropertyAccessorDeclarationSyntaxC protected override PropertyDeclarationSyntax GetPropertyDeclaration(PropertyInfoBase propertyInfo, AccessorDeclarationSyntax[] accessorList, IEnumerable summary) { - var type = SyntaxFactory.ParseName(propertyInfo.NetDataType); + var type = SyntaxFactory.ParseTypeName(propertyInfo.NetDataType); var identifier = propertyInfo.Name; + if (propertyInfo.Optional) + { + type = SyntaxFactory.NullableType(type); + } + var attributes = GetAttributesForProperty(propertyInfo); var result = SyntaxFactory.PropertyDeclaration(type, identifier) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) @@ -123,6 +138,12 @@ protected override PropertyDeclarationSyntax GetPropertyDeclaration(PropertyInfo )) .WithLeadingTrivia(summary); + // HACK: will rewrite this once I replace the type with ISymbol + if (!propertyInfo.Optional && propertyInfo.NetDataType.Equals("string")) + { + result = result.AddModifiers(SyntaxFactory.Token(SyntaxKind.RequiredKeyword)); + } + if (attributes.Count > 0) { var attributeLists = SyntaxFactory.AttributeList(attributes); diff --git a/src/Dhgms.Nucleotide.Generators/GeneratorProcessors/BaseClassLevelCodeGeneratorProcessor.cs b/src/Dhgms.Nucleotide.Generators/GeneratorProcessors/BaseClassLevelCodeGeneratorProcessor.cs index ce85b850..608a76ed 100644 --- a/src/Dhgms.Nucleotide.Generators/GeneratorProcessors/BaseClassLevelCodeGeneratorProcessor.cs +++ b/src/Dhgms.Nucleotide.Generators/GeneratorProcessors/BaseClassLevelCodeGeneratorProcessor.cs @@ -424,6 +424,11 @@ protected override PropertyDeclarationSyntax GetPropertyDeclaration(PropertyInfo var type = SyntaxFactory.ParseTypeName(propertyInfo.NetDataType); var identifier = propertyInfo.Name; + if (propertyInfo.Optional) + { + type = SyntaxFactory.NullableType(type); + } + var attributes = GetAttributesForProperty(propertyInfo); var result = SyntaxFactory.PropertyDeclaration(type, identifier) .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) @@ -433,6 +438,12 @@ protected override PropertyDeclarationSyntax GetPropertyDeclaration(PropertyInfo )) .WithLeadingTrivia(summary); + // HACK: will rewrite this once I replace the type with ISymbol + if (!propertyInfo.Optional && propertyInfo.NetDataType.Equals("string")) + { + result = result.AddModifiers(SyntaxFactory.Token(SyntaxKind.RequiredKeyword)); + } + if (attributes.Count > 0) { var attributeLists = SyntaxFactory.AttributeList(attributes); diff --git a/src/Dhgms.Nucleotide.Generators/Models/IPropertyGenerationModel.cs b/src/Dhgms.Nucleotide.Generators/Models/IPropertyGenerationModel.cs index 5f4a5c9f..92128c39 100644 --- a/src/Dhgms.Nucleotide.Generators/Models/IPropertyGenerationModel.cs +++ b/src/Dhgms.Nucleotide.Generators/Models/IPropertyGenerationModel.cs @@ -12,6 +12,7 @@ namespace Dhgms.Nucleotide.Generators.Models public record PropertyGenerationModel( string TypeName, string Name, - PropertyAccessorFlags PropertyAccessorFlags) + PropertyAccessorFlags PropertyAccessorFlags, + bool Optional) : INameable; } diff --git a/src/Dhgms.Nucleotide.ModelTests/InterfaceGenerationModels/Whipstaff/Entities/NameableInterfaceGenerationModel.cs b/src/Dhgms.Nucleotide.ModelTests/InterfaceGenerationModels/Whipstaff/Entities/NameableInterfaceGenerationModel.cs index da9eb094..54b77ef7 100644 --- a/src/Dhgms.Nucleotide.ModelTests/InterfaceGenerationModels/Whipstaff/Entities/NameableInterfaceGenerationModel.cs +++ b/src/Dhgms.Nucleotide.ModelTests/InterfaceGenerationModels/Whipstaff/Entities/NameableInterfaceGenerationModel.cs @@ -7,7 +7,7 @@ public sealed record NameableInterfaceGenerationModel() : InterfaceGenerationMod "Whipstaff.Core.Entities.INameable", new List { - new ("string", "Name", PropertyAccessorFlags.Get) + new ("string", "Name", PropertyAccessorFlags.Get, false) }, new List(), new List()); diff --git a/src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs b/src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs index 78aec872..e79673f3 100644 --- a/src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs +++ b/src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs @@ -161,7 +161,7 @@ public static class SampleEntityFrameworkModelGenerationModel ClassRemarks = "Represents a User", Properties = [ new ClrStringPropertyInfo(CollectionType.None, "Username", "Username for the user", false, 3, 255, false, false, null), - new ClrStringPropertyInfo(CollectionType.None, "PasswordHash", "Hash of the user password", false, 0, 1024, false, false, null) + new ClrStringPropertyInfo(CollectionType.None, "PasswordHash", "Hash of the user password", true, 0, 1024, false, false, null) ] };