Skip to content

Commit 4fc8fa9

Browse files
author
Bart Koelman
committed
Resource inheritance: derived includes
1 parent 971bf33 commit 4fc8fa9

File tree

16 files changed

+1139
-137
lines changed

16 files changed

+1139
-137
lines changed

benchmarks/Serialization/ResourceSerializationBenchmarks.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,19 @@ protected override IEvaluatedIncludeCache CreateEvaluatedIncludeCache(IResourceG
127127
RelationshipAttribute multi4 = resourceAType.GetRelationshipByPropertyName(nameof(OutgoingResource.Multi4));
128128
RelationshipAttribute multi5 = resourceAType.GetRelationshipByPropertyName(nameof(OutgoingResource.Multi5));
129129

130-
ImmutableArray<ResourceFieldAttribute> chain = ImmutableArray.Create<ResourceFieldAttribute>(single2, single3, multi4, multi5);
131-
IEnumerable<ResourceFieldChainExpression> chains = new ResourceFieldChainExpression(chain).AsEnumerable();
132-
133-
var converter = new IncludeChainConverter();
134-
IncludeExpression include = converter.FromRelationshipChains(chains);
130+
var include = new IncludeExpression(new HashSet<IncludeElementExpression>
131+
{
132+
new(single2, new HashSet<IncludeElementExpression>
133+
{
134+
new(single3, new HashSet<IncludeElementExpression>
135+
{
136+
new(multi4, new HashSet<IncludeElementExpression>
137+
{
138+
new(multi5)
139+
}.ToImmutableHashSet())
140+
}.ToImmutableHashSet())
141+
}.ToImmutableHashSet())
142+
}.ToImmutableHashSet());
135143

136144
var cache = new EvaluatedIncludeCache();
137145
cache.Set(include);

src/JsonApiDotNetCore/Queries/Expressions/IncludeChainConverter.cs

-72
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,6 @@ public IReadOnlyCollection<ResourceFieldChainExpression> GetRelationshipChains(I
4242
return converter.Chains;
4343
}
4444

45-
/// <summary>
46-
/// Converts a set of relationship chains into a tree of inclusions.
47-
/// </summary>
48-
/// <example>
49-
/// Input chains: <code><![CDATA[
50-
/// Article -> Blog,
51-
/// Article -> Revisions -> Author
52-
/// ]]></code> Output tree:
53-
/// <code><![CDATA[
54-
/// Article
55-
/// {
56-
/// Blog,
57-
/// Revisions
58-
/// {
59-
/// Author
60-
/// }
61-
/// }
62-
/// ]]></code>
63-
/// </example>
64-
public IncludeExpression FromRelationshipChains(IEnumerable<ResourceFieldChainExpression> chains)
65-
{
66-
ArgumentGuard.NotNull(chains, nameof(chains));
67-
68-
IImmutableSet<IncludeElementExpression> elements = ConvertChainsToElements(chains);
69-
return elements.Any() ? new IncludeExpression(elements) : IncludeExpression.Empty;
70-
}
71-
72-
private static IImmutableSet<IncludeElementExpression> ConvertChainsToElements(IEnumerable<ResourceFieldChainExpression> chains)
73-
{
74-
var rootNode = new MutableIncludeNode(null!);
75-
76-
foreach (ResourceFieldChainExpression chain in chains)
77-
{
78-
ConvertChainToElement(chain, rootNode);
79-
}
80-
81-
return rootNode.Children.Values.Select(child => child.ToExpression()).ToImmutableHashSet();
82-
}
83-
84-
private static void ConvertChainToElement(ResourceFieldChainExpression chain, MutableIncludeNode rootNode)
85-
{
86-
MutableIncludeNode currentNode = rootNode;
87-
88-
foreach (RelationshipAttribute relationship in chain.Fields.OfType<RelationshipAttribute>())
89-
{
90-
if (!currentNode.Children.ContainsKey(relationship))
91-
{
92-
currentNode.Children[relationship] = new MutableIncludeNode(relationship);
93-
}
94-
95-
currentNode = currentNode.Children[relationship];
96-
}
97-
}
98-
9945
private sealed class IncludeToChainsConverter : QueryExpressionVisitor<object?, object?>
10046
{
10147
private readonly Stack<RelationshipAttribute> _parentRelationshipStack = new();
@@ -144,22 +90,4 @@ private void FlushChain(IncludeElementExpression expression)
14490
Chains.Add(new ResourceFieldChainExpression(chainBuilder.ToImmutable()));
14591
}
14692
}
147-
148-
private sealed class MutableIncludeNode
149-
{
150-
private readonly RelationshipAttribute _relationship;
151-
152-
public IDictionary<RelationshipAttribute, MutableIncludeNode> Children { get; } = new Dictionary<RelationshipAttribute, MutableIncludeNode>();
153-
154-
public MutableIncludeNode(RelationshipAttribute relationship)
155-
{
156-
_relationship = relationship;
157-
}
158-
159-
public IncludeElementExpression ToExpression()
160-
{
161-
IImmutableSet<IncludeElementExpression> elementChildren = Children.Values.Select(child => child.ToExpression()).ToImmutableHashSet();
162-
return new IncludeElementExpression(_relationship, elementChildren);
163-
}
164-
}
16593
}

0 commit comments

Comments
 (0)