Skip to content

Commit 3f129b9

Browse files
Merge pull request #73 from ParsePlatform/richardross.relation.query.redirect
Add support for redirecting class-names of ParseRelations.
2 parents 1b1cf36 + 1e1117b commit 3f129b9

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

Parse/ParseQuery.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ParseQuery<T> where T : ParseObject {
5050
private readonly ReadOnlyCollection<string> orderBy;
5151
private readonly ReadOnlyCollection<string> includes;
5252
private readonly ReadOnlyCollection<string> selectedKeys;
53+
private readonly String redirectClassNameForKey;
5354
private readonly int? skip;
5455
private readonly int? limit;
5556

@@ -73,7 +74,8 @@ private ParseQuery(ParseQuery<T> source,
7374
int? skip = null,
7475
int? limit = null,
7576
IEnumerable<string> includes = null,
76-
IEnumerable<string> selectedKeys = null) {
77+
IEnumerable<string> selectedKeys = null,
78+
String redirectClassNameForKey = null) {
7779
if (source == null) {
7880
throw new ArgumentNullException("source");
7981
}
@@ -85,6 +87,7 @@ private ParseQuery(ParseQuery<T> source,
8587
this.limit = source.limit;
8688
this.includes = source.includes;
8789
this.selectedKeys = source.selectedKeys;
90+
this.redirectClassNameForKey = source.redirectClassNameForKey;
8891

8992
if (where != null) {
9093
var newWhere = MergeWhereClauses(where);
@@ -127,6 +130,10 @@ private ParseQuery(ParseQuery<T> source,
127130
var newSelectedKeys = MergeSelectedKeys(selectedKeys);
128131
this.selectedKeys = new ReadOnlyCollection<string>(newSelectedKeys.ToList());
129132
}
133+
134+
if (redirectClassNameForKey != null) {
135+
this.redirectClassNameForKey = redirectClassNameForKey;
136+
}
130137
}
131138

132139
private HashSet<string> MergeIncludes(IEnumerable<string> includes) {
@@ -326,6 +333,10 @@ public ParseQuery<T> Limit(int count) {
326333
return new ParseQuery<T>(this, limit: count);
327334
}
328335

336+
internal ParseQuery<T> RedirectClassName(String key) {
337+
return new ParseQuery<T>(this, redirectClassNameForKey: key);
338+
}
339+
329340
#region Where
330341

331342
/// <summary>
@@ -848,6 +859,9 @@ internal IDictionary<string, object> BuildParameters(bool includeClassName = fal
848859
if (includeClassName) {
849860
result["className"] = className;
850861
}
862+
if (redirectClassNameForKey != null) {
863+
result["redirectClassNameForKey"] = redirectClassNameForKey;
864+
}
851865
return result;
852866
}
853867

Parse/ParseRelation.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ IDictionary<string, object> IJsonConvertible.ToJSON() {
5656
}
5757

5858
internal ParseQuery<T> GetQuery<T>() where T : ParseObject {
59-
return new ParseQuery<T>(targetClassName)
60-
.WhereRelatedTo(parent, key);
59+
if (targetClassName != null) {
60+
return new ParseQuery<T>(targetClassName)
61+
.WhereRelatedTo(parent, key);
62+
}
63+
64+
return new ParseQuery<T>(parent.ClassName)
65+
.RedirectClassName(key)
66+
.WhereRelatedTo(parent, key);
6167
}
6268

6369
internal string TargetClassName {

ParseTest.Unit/ParseTest.Unit.NetFx45.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<Compile Include="PushEncoderTests.cs" />
9797
<Compile Include="Properties\AssemblyInfo.cs" />
9898
<Compile Include="InstallationTests.cs" />
99+
<Compile Include="RelationTests.cs" />
99100
<Compile Include="SessionTests.cs" />
100101
<Compile Include="SessionControllerTests.cs" />
101102
<Compile Include="ObjectControllerTests.cs" />

ParseTest.Unit/RelationTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
using Parse;
4+
using Parse.Internal;
5+
6+
namespace ParseTest {
7+
[TestFixture]
8+
public class RelationTests {
9+
[Test]
10+
public void TestRelationQuery() {
11+
ParseObject parent = ParseObject.CreateWithoutData("Foo", "abcxyz");
12+
13+
ParseRelation<ParseObject> relation = new ParseRelation<ParseObject>(parent, "child");
14+
ParseQuery<ParseObject> query = relation.Query;
15+
16+
// Client side, the query will appear to be for the wrong class.
17+
// When the server recieves it, the class name will be redirected using the 'redirectClassNameForKey' option.
18+
Assert.AreEqual("Foo", query.ClassName);
19+
20+
IDictionary<string, object> encoded = query.BuildParameters();
21+
22+
Assert.AreEqual("child", encoded["redirectClassNameForKey"]);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)