Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ModelPropertiesToIgnore prop to ViewModel attribute #92

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,31 @@ public void ShouldBeEqual9()
Assert.Equal(_viewModelToGenerate1, _viewModelToGenerate2);
}

[Fact]
public void ShouldNotBeEqualDifferentModelPropertiesToIgnore1()
{
_viewModelToGenerate2.WrappedModelPropertiesToIgnore = null;

Assert.NotEqual(_viewModelToGenerate1, _viewModelToGenerate2);
}

[Fact]
public void ShouldNotBeEqualDifferentModelPropertiesToIgnore2()
{
_viewModelToGenerate2.WrappedModelPropertiesToIgnore = "FirstName";

Assert.NotEqual(_viewModelToGenerate1, _viewModelToGenerate2);
}

[Fact]
public void ShouldBeEqual10()
{
_viewModelToGenerate1.WrappedModelPropertiesToIgnore = null;
_viewModelToGenerate2.WrappedModelPropertiesToIgnore = null;

Assert.Equal(_viewModelToGenerate1, _viewModelToGenerate2);
}

private static void FillAllProperties(ViewModelToGenerate viewModelToGenerate)
{
viewModelToGenerate.ClassAccessModifier = "public";
Expand All @@ -399,6 +424,7 @@ private static void FillAllProperties(ViewModelToGenerate viewModelToGenerate)
viewModelToGenerate.IsEventSubscriber = true;
viewModelToGenerate.WrappedModelType = "Employee";
viewModelToGenerate.WrappedModelPropertyName = "EmployeeModel";
viewModelToGenerate.WrappedModelPropertiesToIgnore = "Address";

viewModelToGenerate.PropertiesToGenerate = new List<PropertyToGenerate>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,60 @@ public bool IsDeveloper
protected MyCode.Employee { expectedModelPropertyName } {{ get; set; }}
}}
}}
");
}

[InlineData("ModelType=typeof(Employee)")]
[InlineData("typeof(Employee)")]
[Theory]
public void IgnoreModelProperties(string attributeArgument)
{
ShouldGenerateExpectedCode(
$@"using MvvmGen;

namespace MyCode
{{
public class Employee
{{
public string FirstName {{ get; set; }}
public string LastName {{ get; set; }}
public bool IsDeveloper {{ get; set; }}
}}

[ViewModel({attributeArgument},ModelPropertiesToIgnore=""LastName, IsDeveloper"")]
public partial class EmployeeViewModel
{{
}}
}}",
$@"{AutoGeneratedTopContent}

namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
public EmployeeViewModel()
{{
this.OnInitialize();
}}

partial void OnInitialize();

public string FirstName
{{
get => Model.FirstName;
set
{{
if (Model.FirstName != value)
{{
Model.FirstName = value;
OnPropertyChanged(""FirstName"");
}}
}}
}}

protected MyCode.Employee Model {{ get; set; }}
}}
}}
");
}

Expand Down
13 changes: 12 additions & 1 deletion src/MvvmGen.SourceGenerators/Inspectors/ModelMemberInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ namespace MvvmGen.Inspectors
{
internal static class ModelMemberInspector
{
internal static string? Inspect(AttributeData viewModelAttributeData, IList<PropertyToGenerate> propertiesToGenerate, string? wrappedModelPropertyName)
internal static string? Inspect(AttributeData viewModelAttributeData,
IList<PropertyToGenerate> propertiesToGenerate,
string? wrappedModelPropertyName,
string? wrappedModelPropertiesToIgnore)
{
string? wrappedModelType = null;
var wrappedModelPropsToIgnore = wrappedModelPropertiesToIgnore?
.Split(',').Select(x => x.Trim()).ToList();

var modelTypedConstant = (TypedConstant?)viewModelAttributeData.ConstructorArguments.FirstOrDefault();

Expand All @@ -41,6 +46,12 @@ internal static class ModelMemberInspector
var propertySymbol = (IPropertySymbol?)methodSymbol.AssociatedSymbol;
if (propertySymbol is not null)
{
if (wrappedModelPropsToIgnore is not null
&& wrappedModelPropsToIgnore.Contains(propertySymbol.Name))
{
continue;
}

propertiesToGenerate.Add(new PropertyToGenerate(
propertySymbol.Name, propertySymbol.Type.ToString(), $"{wrappedModelPropertyName}.{propertySymbol.Name}", propertySymbol.IsReadOnly));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@
return modelPropertyName;
}

internal static string? InspectModelPropertiesToIgnore(AttributeData viewModelAttributeData)
{
string? propertiesToIgnore = null;

foreach (var arg in viewModelAttributeData.NamedArguments)
{
if (arg.Key == "ModelPropertiesToIgnore")
{
propertiesToIgnore = arg.Value.Value?.ToString();
}
}

return propertiesToIgnore;
}

internal static string InspectCommandType(AttributeData viewModelAttributeData)
{
string commandType = "DelegateCommand";
Expand All @@ -48,11 +63,11 @@
{
if (arg.Key == "CommandType")
{
commandType = arg.Value.Value?.ToString();

Check warning on line 66 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

Converting null literal or possible null value to non-nullable type.

Check warning on line 66 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

Converting null literal or possible null value to non-nullable type.

Check warning on line 66 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

Converting null literal or possible null value to non-nullable type.

Check warning on line 66 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

Converting null literal or possible null value to non-nullable type.
}
}

return commandType;

Check warning on line 70 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

Possible null reference return.

Check warning on line 70 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

Possible null reference return.

Check warning on line 70 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

Possible null reference return.

Check warning on line 70 in src/MvvmGen.SourceGenerators/Inspectors/ViewModelAttributeInspector.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

Possible null reference return.
}
}
}
3 changes: 3 additions & 0 deletions src/MvvmGen.SourceGenerators/Model/ViewModelToGenerate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public ViewModelToGenerate(string className, string namespaceName)

public string? WrappedModelPropertyName { get; set; }

public string? WrappedModelPropertiesToIgnore { get; set; }

public bool IsEventSubscriber { get; set; }

public bool GenerateConstructor { get; set; }
Expand Down Expand Up @@ -66,6 +68,7 @@ public bool Equals(ViewModelToGenerate? other)
NamespaceName == other.NamespaceName &&
WrappedModelType == other.WrappedModelType &&
WrappedModelPropertyName == other.WrappedModelPropertyName &&
WrappedModelPropertiesToIgnore == other.WrappedModelPropertiesToIgnore &&
IsEventSubscriber == other.IsEventSubscriber &&
GenerateConstructor == other.GenerateConstructor &&
CommandType== other.CommandType &&
Expand Down
7 changes: 6 additions & 1 deletion src/MvvmGen.SourceGenerators/ViewModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// Generates ViewModels for classes that are decorated with the MvvmGen.ViewModelAttribute.
/// </summary>
[Generator]
public class ViewModelGenerator : IIncrementalGenerator

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'
{
private static readonly string _versionString;

Expand Down Expand Up @@ -92,7 +92,12 @@
};

viewModelToGenerate.WrappedModelPropertyName = ViewModelAttributeInspector.InspectModelPropertyName(viewModelAttributeData);
viewModelToGenerate.WrappedModelType = ModelMemberInspector.Inspect(viewModelAttributeData, viewModelToGenerate.PropertiesToGenerate, viewModelToGenerate.WrappedModelPropertyName);
viewModelToGenerate.WrappedModelPropertiesToIgnore = ViewModelAttributeInspector.InspectModelPropertiesToIgnore(viewModelAttributeData);
viewModelToGenerate.WrappedModelType =
ModelMemberInspector.Inspect(viewModelAttributeData,
viewModelToGenerate.PropertiesToGenerate,
viewModelToGenerate.WrappedModelPropertyName,
viewModelToGenerate.WrappedModelPropertiesToIgnore);

viewModelToGenerate.ViewModelInterfaceToGenerate = ViewModelGenerateInterfaceAttributeInspector.Inspect(viewModelClassSymbol,
viewModelToGenerate.PropertiesToGenerate, viewModelToGenerate.CommandsToGenerate, context.Node.SyntaxTree);
Expand Down
6 changes: 6 additions & 0 deletions src/MvvmGen/Attributes/ViewModelAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public ViewModelAttribute(Type modelType)
/// </summary>
public string? ModelPropertyName { get; set; }

/// <summary>
/// Gets or sets a comma separated list of model properties that should not be generated
/// in the ViewModel for the model that you specified with the <see cref="ModelType"/> property.
/// </summary>
public string? ModelPropertiesToIgnore { get; set; }

/// <summary>
/// Gets or sets if a constructor is generated. Default value is true.
/// </summary>
Expand Down