Skip to content

Commit

Permalink
Fix: nullable \ required string in models (#480)
Browse files Browse the repository at this point in the history
* add required keyword to models

* fix issue with read only key on model

* more required keyword hacks for optional

* Update UnkeyedModelClassGeneratorProcessor.cs

* Update BaseClassLevelCodeGeneratorProcessor.cs
  • Loading branch information
dpvreony authored Jul 19, 2024
1 parent e449463 commit fca3e97
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041</TargetFramework>
<LangVersion>9</LangVersion>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected override IEnumerable<PropertyDeclarationSyntax> GetPropertyDeclaration

return new[]
{
GetReadOnlyPropertyDeclaration(idColumn)
GetPropertyDeclaration(idColumn)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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;
}

Expand All @@ -111,9 +121,14 @@ private static AccessorDeclarationSyntax[] GetPropertyAccessorDeclarationSyntaxC

protected override PropertyDeclarationSyntax GetPropertyDeclaration(PropertyInfoBase propertyInfo, AccessorDeclarationSyntax[] accessorList, IEnumerable<SyntaxTrivia> 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))
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Dhgms.Nucleotide.Generators.Models
public record PropertyGenerationModel(
string TypeName,
string Name,
PropertyAccessorFlags PropertyAccessorFlags)
PropertyAccessorFlags PropertyAccessorFlags,
bool Optional)
: INameable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public sealed record NameableInterfaceGenerationModel() : InterfaceGenerationMod
"Whipstaff.Core.Entities.INameable",
new List<PropertyGenerationModel>
{
new ("string", "Name", PropertyAccessorFlags.Get)
new ("string", "Name", PropertyAccessorFlags.Get, false)
},
new List<IInterfaceMethodGenerationModel>(),
new List<InterfaceGenerationModel>());
Expand Down
2 changes: 1 addition & 1 deletion src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
};

Expand Down

0 comments on commit fca3e97

Please sign in to comment.