Skip to content

Commit 8d10016

Browse files
committed
chore(pointers): move classes into separate files
1 parent d64709e commit 8d10016

File tree

4 files changed

+86
-43
lines changed

4 files changed

+86
-43
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using Newtonsoft.Json.Schema;
4+
using JsonApiDotNetCore.Models.Pointers;
5+
6+
namespace JsonApiDotNetCore.Extensions
7+
{
8+
public static class JObjectExtensions
9+
{
10+
public static bool TryParse<TPointer, TPointerBase>(this JObject obj, JSchema schema, out Pointer<TPointerBase> pointer)
11+
where TPointer : Pointer<TPointerBase>, new()
12+
{
13+
if (obj.IsValid(schema))
14+
{
15+
pointer = obj.ToObject<TPointer>();
16+
return true;
17+
}
18+
19+
pointer = null;
20+
return false;
21+
}
22+
}
23+
}

src/JsonApiDotNetCore/Internal/Generics/IGenericProcessorFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace JsonApiDotNetCore.Internal.Generics
1010
/// </summary>
1111
public interface IGenericProcessorFactory
1212
{
13-
1413
TInterface GetProcessor<TInterface>(params Type[] types);
1514
}
1615
}
Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
using System;
12
using System.Collections.Generic;
23
using JsonApiDotNetCore.Models.Pointers;
34
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Linq;
5-
using Newtonsoft.Json.Schema;
65

76
namespace JsonApiDotNetCore.Models
87
{
@@ -20,44 +19,4 @@ public class DocumentData
2019
[JsonProperty("relationships")]
2120
public Dictionary<string, RelationshipData> Relationships { get; set; }
2221
}
23-
24-
public class DocumentDataPointerReplacement<TPointer, TPointerBase>
25-
where TPointer : Pointer<TPointerBase>, new()
26-
{
27-
private readonly DocumentData _data;
28-
29-
public DocumentDataPointerReplacement(DocumentData data)
30-
{
31-
_data = data;
32-
}
33-
34-
public void ReplacePointers(List<TPointerBase> parentDoc)
35-
{
36-
ReplacePointer(_data.Id, parentDoc);
37-
ReplacePointer(_data.Type, parentDoc);
38-
}
39-
40-
private void ReplacePointer(object reference, List<TPointerBase> parentDoc)
41-
{
42-
if (reference is JObject jObj)
43-
if (jObj.TryParse<TPointer, TPointerBase>(Pointer<TPointerBase>.JsonSchema, out Pointer<TPointerBase> pointer))
44-
reference = pointer.GetValue(parentDoc);
45-
}
46-
}
47-
}
48-
49-
public static class JObjectExtensions
50-
{
51-
public static bool TryParse<TPointer, TPointerBase>(this JObject obj, JSchema schema, out Pointer<TPointerBase> pointer)
52-
where TPointer : Pointer<TPointerBase>, new()
53-
{
54-
if (obj.IsValid(schema))
55-
{
56-
pointer = obj.ToObject<TPointer>();
57-
return true;
58-
}
59-
60-
pointer = null;
61-
return false;
62-
}
6322
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using JsonApiDotNetCore.Models.Pointers;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
6+
using Newtonsoft.Json.Schema;
7+
using JsonApiDotNetCore.Models;
8+
using JsonApiDotNetCore.Extensions;
9+
10+
namespace JsonApiDotNetCore.Services.Operations
11+
{
12+
public class DocumentDataPointerReplacement<TPointer, TPointerBase>
13+
where TPointer : Pointer<TPointerBase>, new()
14+
{
15+
private readonly DocumentData _data;
16+
17+
public DocumentDataPointerReplacement(DocumentData data)
18+
{
19+
_data = data;
20+
}
21+
22+
public void ReplacePointers(List<TPointerBase> parentDoc)
23+
{
24+
_data.Id = GetPointerValue(_data.Id, parentDoc);
25+
_data.Type = GetPointerValue(_data.Type, parentDoc);
26+
27+
if (_data.Relationships != null)
28+
{
29+
foreach (var relationshipDictionary in _data.Relationships)
30+
{
31+
if (relationshipDictionary.Value.IsHasMany)
32+
{
33+
foreach (var relationship in relationshipDictionary.Value.ManyData)
34+
ReplaceDictionaryPointers(relationship, parentDoc);
35+
}
36+
else
37+
{
38+
ReplaceDictionaryPointers(relationshipDictionary.Value.SingleData, parentDoc);
39+
}
40+
}
41+
}
42+
}
43+
44+
private void ReplaceDictionaryPointers(Dictionary<string, object> relationship, List<TPointerBase> parentDoc)
45+
{
46+
if (relationship.ContainsKey("id"))
47+
relationship["id"] = GetPointerValue(relationship["id"], parentDoc);
48+
49+
if (relationship.ContainsKey("type"))
50+
relationship["type"] = GetPointerValue(relationship["type"], parentDoc);
51+
}
52+
53+
private object GetPointerValue(object reference, List<TPointerBase> parentDoc)
54+
{
55+
if (reference is JObject jObj)
56+
if (jObj.TryParse<TPointer, TPointerBase>(Pointer<TPointerBase>.JsonSchema, out Pointer<TPointerBase> pointer))
57+
return pointer.GetValue(parentDoc);
58+
59+
return reference;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)