Skip to content

Commit

Permalink
ITyped .Children now also handle the type param #3141
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Aug 10, 2023
1 parent 1e871d2 commit d19650f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class TestPerson
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string Type { get; set; } = "Person";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ private class TestData
new TestPerson
{
Id = 202
}
},
new TestPerson
{
Id = 301,
Type = "Company"
},
};
}
private static readonly TestData Data = new TestData();

private ITypedItem Author() => ItemFromObject(Data).Child("author");
private ITypedItem Reader() => ItemFromObject(Data).Child("readers");
private IEnumerable<ITypedItem> Readers() => ItemFromObject(Data).Children("readers");
private ITypedItem Item => ItemFromObject(Data);
private ITypedItem Author() => Item.Child("author");
private ITypedItem Reader() => Item.Child("readers");
private IEnumerable<ITypedItem> Readers() => Item.Children("readers");

[TestMethod] public void ChildSingleExists() => IsNotNull(Author());
[TestMethod] public void ChildDummyNotExists() => IsNull(ItemFromObject(Data).Child("dummy"));
[TestMethod] public void ChildDummyNotExists() => IsNull(Item.Child("dummy"));
[TestMethod] public void ChildSingleId() => AreEqual(Data.Author.Id, Author().Id);
[TestMethod] public void ChildSingleTitle() => AreEqual(Data.Author.Title, Author().Title);
[TestMethod] public void ChildSingleFirstName() => AreEqual(Data.Author.FirstName, Author().String("firstname"));
Expand All @@ -48,7 +53,9 @@ private class TestData
[TestMethod] public void ReadersListExists() => IsNotNull(Readers());

[TestMethod] public void ReadersListCount() => AreEqual(Data.Readers.Length, Readers().Count());
[TestMethod] public void FakeListNotExists() => IsNotNull(ItemFromObject(Data).Children("dummy"));
[TestMethod] public void ReadersPersonCount() => AreEqual(Data.Readers.Length - 1, Item.Children("readers", type: "Person").Count());
[TestMethod] public void ReadersCompanyCount() => AreEqual(1, Item.Children("readers", type: "Company").Count());
[TestMethod] public void FakeListNotExists() => IsNotNull(Item.Children("dummy"));
[TestMethod] public void ReadersList1Exists() => IsNotNull(Readers().First());
[TestMethod] public void ReadersList1Id() => AreEqual(Data.Readers[0].Id, Readers().First().Id);
[TestMethod] public void ReadersList2Exists() => IsNotNull(Readers().Skip(1).First());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class DynamicEntity


/// <inheritdoc />
public IField Field(string name) => (this as ITypedItem).Field(name, required: null);
public IField Field(string name) => (this as ITypedItem).Field(name);

IField ITypedItem.Field(string name, string noParamOrder, bool? required)
{
Expand Down
13 changes: 4 additions & 9 deletions Src/Sxc/ToSic.Sxc/Data/DynamicEntity/IDynamicEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public partial interface IDynamicEntity:
/// <returns>
/// An Entity object.
/// </returns>
[PrivateApi("This should not be used publicly, use AsTyped instead")]
[PrivateApi("This should not be used publicly, use AsTyped instead. It's necessary so that code can find the Entity without ambiguity")]
new IEntity Entity { get; }



/// <summary>
/// Get a Field-object of a property of this entity, to use with services like the <see cref="Services.IImageService"/> which also need more information like the metadata.
/// </summary>
Expand Down Expand Up @@ -128,14 +129,8 @@ public partial interface IDynamicEntity:
dynamic Presentation { get; }

[PrivateApi]
// ReSharper disable once InconsistentNaming
CodeDataFactory _Cdf {get; }

/* IMPORTANT: KEEP THIS DEFINITION AND DOCS IN SYNC BETWEEN IDynamicEntity, IDynamicEntityBase and IDynamicStack */
///// <summary>
///// Activate debugging, so that you'll see details in [Insights](xref:NetCode.Debug.Insights.Index) how the value was retrieved.
///// </summary>
///// <param name="debug"></param>
//void SetDebug(bool debug);

}
}
14 changes: 8 additions & 6 deletions Src/Sxc/ToSic.Sxc/Data/Typed/WrapObjectTypedItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ public IEnumerable<ITypedItem> Children(string field, string noParamOrder, strin
.Where(o => o != null && !o.GetType().IsValueType)
.ToList();

return list.Select(l => Wrapper.TypedItemFromObject(l, PreWrap.Settings));
var items = list.Select(l => Wrapper.TypedItemFromObject(l, PreWrap.Settings));

if (type.HasValue())
items = items.Where(i => i.String(nameof(ITypedItem.Type), required: false).EqualsInsensitive(type)).ToList();

return items;
}

/// <summary>
Expand All @@ -85,14 +90,11 @@ public IEnumerable<ITypedItem> Parents(string noParamOrder, string type, string
{
var blank = Enumerable.Empty<ITypedItem>();
var typed = this as ITypedItem;
var items = typed.Children(nameof(ITypedItem.Parents), noParamOrder)?.ToList();
var items = typed.Children(nameof(ITypedItem.Parents), type: type)?.ToList();

if (items == null || !items.Any() || !type.HasValue() && !field.HasValue())
if (items == null || !items.Any() && !field.HasValue())
return items ?? blank;

if (type.HasValue())
items = items.Where(i => i.String(nameof(ITypedItem.Type), required: false).EqualsInsensitive(type)).ToList();

if (field.HasValue())
items = items.Where(i => i.String("Field", required: false).EqualsInsensitive(field)).ToList();
return items;
Expand Down

0 comments on commit d19650f

Please sign in to comment.