Skip to content

Commit

Permalink
Merge pull request #92 from thomasclaudiushuber/issue/76
Browse files Browse the repository at this point in the history
Add ModelPropertiesToIgnore prop to ViewModel attribute
  • Loading branch information
thomasclaudiushuber committed Dec 10, 2023
2 parents ba24c8e + 1f485bd commit c8026e2
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 2 deletions.
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 @@ internal static bool InspectGenerateConstructor(AttributeData viewModelAttribute
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 Down
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 @@ -92,7 +92,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
};

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

0 comments on commit c8026e2

Please sign in to comment.