Skip to content

Commit

Permalink
ContentTypeFactory - also support ignoring things
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Oct 25, 2024
1 parent 1604100 commit e437883
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ private void AssertAttribute(IContentType ct, string name, ValueTypes type, bool
}

[TestMethod]
public void Attributes_NoSpec_Count()
{
var x = Factory().Create(typeof(TestTypeNoSpecs));
Assert.AreEqual(4, x.Attributes.Count());
}
public void Attributes_NoSpec_Count() => Assert.AreEqual(4, Factory().Create(typeof(TestTypeNoSpecs)).Attributes.Count());

[TestMethod]
[DataRow(nameof(TestTypeNoSpecs.Name), ValueTypes.String)]
Expand All @@ -33,6 +29,9 @@ public void Attributes_NoSpec_Count()
public void AssertAttributeNoSpec(string name, ValueTypes type)
=> AssertAttribute(Factory().Create(typeof(TestTypeNoSpecs)), name, type);

[TestMethod]
public void Attributes_WithSpec_Count() => Assert.AreEqual(5, Factory().Create(typeof(TestTypeWithSpecs)).Attributes.Count());

[TestMethod]
[DataRow(nameof(TestTypeWithSpecs.Name) + "Mod", ValueTypes.String, true)]
[DataRow(nameof(TestTypeWithSpecs.Url), ValueTypes.Hyperlink)]
Expand All @@ -42,4 +41,15 @@ public void AssertAttributeNoSpec(string name, ValueTypes type)
public void AssertAttributeWithSpec(string name, ValueTypes type, bool isTitle = false, string description = default)
=> AssertAttribute(Factory().Create(typeof(TestTypeWithSpecs)), name, type, isTitle, description);

/// <summary>
/// Don't use properties which are private, internal or have the Ignore attribute
/// </summary>
/// <param name="name"></param>
[TestMethod]
[DataRow(nameof(TestTypeWithSpecs.IgnoreThis))]
[DataRow(nameof(TestTypeWithSpecs.InternalProperty))]
[DataRow("PrivateProperty")]
public void Attributes_WithSpec_SkipIgnores(string name)
=> Assert.IsFalse(Factory().Create(typeof(TestTypeWithSpecs)).Attributes.Any(a => a.Name == name));

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ internal class TestTypeWithSpecs: TestTypeWithSpecsEmpty
[ContentTypeAttributeSpecs(Description = IsAliveDescription)]
public bool IsAlive { get; set; }


[ContentTypeAttributeIgnore]
public string IgnoreThis { get; set; }

private string PrivateProperty { get; set; }

internal string InternalProperty { get; set; }
}
8 changes: 7 additions & 1 deletion ToSic.Eav.Core/Data/Build/Builder/ContentTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ private List<IContentTypeAttribute> GenerateAttributes(Type type)
if (properties.Length == 0)
return [];

var propsFiltered = properties.Where(p => p.GetCustomAttribute<ContentTypeAttributeIgnoreAttribute>() == null)
.ToList();

if (propsFiltered.Count == 0)
return [];

// Generate list of attributes
var attributes = properties.Select(p =>
var attributes = propsFiltered.Select(p =>
{
var attrSpecs = p.GetCustomAttributes<ContentTypeAttributeSpecsAttribute>().FirstOrDefault();
var attrName = attrSpecs?.Name ?? p.Name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace ToSic.Eav.Data.ContentTypes.CodeAttributes;

[PrivateApi("WIP")]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public class ContentTypeAttributeIgnoreAttribute : Attribute;

0 comments on commit e437883

Please sign in to comment.