Skip to content

Commit 5f0c45a

Browse files
Made ParseClient.ConvertTo() a public API.
ConvertTo's functionality is useful for external developers if they ever have to deal with CloudCode, or coercing other types return from the SDK. Fixes #67.
1 parent 0bec459 commit 5f0c45a

File tree

13 files changed

+169
-125
lines changed

13 files changed

+169
-125
lines changed

Parse/Internal/Cloud/Controller/ParseCloudCodeController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using Parse.Utilities;
78

89
namespace Parse.Internal {
910
internal class ParseCloudCodeController : IParseCloudCodeController {
@@ -27,7 +28,7 @@ public Task<T> CallFunctionAsync<T>(String name,
2728
if (!decoded.ContainsKey("result")) {
2829
return default(T);
2930
}
30-
return (T)ParseClient.ConvertTo<T>(decoded["result"]);
31+
return (T)Conversion.ConvertTo<T>(decoded["result"]);
3132
});
3233
}
3334
}

Parse/Internal/Encoding/ParseDecoder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Collections.Generic;
66
using System.Globalization;
7+
using Parse.Utilities;
78

89
namespace Parse.Internal {
910
internal class ParseDecoder {
@@ -59,8 +60,8 @@ public object Decode(object data) {
5960
}
6061

6162
if (typeString == "GeoPoint") {
62-
return new ParseGeoPoint((double)ParseClient.ConvertTo<double>(dict["latitude"]),
63-
(double)ParseClient.ConvertTo<double>(dict["longitude"]));
63+
return new ParseGeoPoint((double)Conversion.ConvertTo<double>(dict["latitude"]),
64+
(double)Conversion.ConvertTo<double>(dict["longitude"]));
6465
}
6566

6667
if (typeString == "Object") {

Parse/Internal/Encoding/ParseEncoder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Globalization;
66
using System.Linq;
7+
using Parse.Utilities;
78

89
namespace Parse.Internal {
910
/// <summary>
@@ -23,8 +24,8 @@ value is ParseGeoPoint ||
2324
value is ParseRelationBase ||
2425
value is DateTime ||
2526
value is byte[] ||
26-
ParseClient.ConvertTo<IDictionary<string, object>>(value) is IDictionary<string, object> ||
27-
ParseClient.ConvertTo<IList<object>>(value) is IList<object>;
27+
Conversion.ConvertTo<IDictionary<string, object>>(value) is IDictionary<string, object> ||
28+
Conversion.ConvertTo<IList<object>>(value) is IList<object>;
2829
}
2930

3031
public object Encode(object value) {
@@ -55,7 +56,7 @@ public object Encode(object value) {
5556
return jsonConvertible.ToJSON();
5657
}
5758

58-
var dict = ParseClient.ConvertTo<IDictionary<string, object>>(value) as IDictionary<string, object>;
59+
var dict = Conversion.ConvertTo<IDictionary<string, object>>(value) as IDictionary<string, object>;
5960
if (dict != null) {
6061
var json = new Dictionary<string, object>();
6162
foreach (var pair in dict) {
@@ -64,7 +65,7 @@ public object Encode(object value) {
6465
return json;
6566
}
6667

67-
var list = ParseClient.ConvertTo<IList<object>>(value) as IList<object>;
68+
var list = Conversion.ConvertTo<IList<object>>(value) as IList<object>;
6869
if (list != null) {
6970
return EncodeList(list);
7071
}

Parse/Internal/Object/Controller/ParseObjectController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Parse.Utilities;
89

910
namespace Parse.Internal {
1011
internal class ParseObjectController : IParseObjectController {
@@ -163,7 +164,7 @@ private IList<Task<IDictionary<string, object>>> ExecuteBatchRequest(IList<Parse
163164
return;
164165
}
165166

166-
var resultsArray = ParseClient.As<IList<object>>(t.Result.Item2["results"]);
167+
var resultsArray = Conversion.As<IList<object>>(t.Result.Item2["results"]);
167168
int resultLength = resultsArray.Count;
168169
if (resultLength != batchSize) {
169170
foreach (var tcs in tcss) {

Parse/Internal/Operation/ParseAddOperation.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using System.Linq;
7+
using Parse.Utilities;
78

89
namespace Parse.Internal {
910
class ParseAddOperation : IParseFieldOperation {
@@ -28,7 +29,7 @@ public IParseFieldOperation MergeWithPrevious(IParseFieldOperation previous) {
2829
}
2930
if (previous is ParseSetOperation) {
3031
var setOp = (ParseSetOperation)previous;
31-
var oldList = (IList<object>)ParseClient.ConvertTo<IList<object>>(setOp.Value);
32+
var oldList = (IList<object>)Conversion.ConvertTo<IList<object>>(setOp.Value);
3233
return new ParseSetOperation(oldList.Concat(objects).ToList());
3334
}
3435
if (previous is ParseAddOperation) {
@@ -41,7 +42,7 @@ public object Apply(object oldValue, string key) {
4142
if (oldValue == null) {
4243
return objects.ToList();
4344
}
44-
var oldList = (IList<object>)ParseClient.ConvertTo<IList<object>>(oldValue);
45+
var oldList = (IList<object>)Conversion.ConvertTo<IList<object>>(oldValue);
4546
return oldList.Concat(objects).ToList();
4647
}
4748

Parse/Internal/Operation/ParseAddUniqueOperation.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using System.Linq;
7+
using Parse.Utilities;
78

89
namespace Parse.Internal {
910
class ParseAddUniqueOperation : IParseFieldOperation {
@@ -28,7 +29,7 @@ public IParseFieldOperation MergeWithPrevious(IParseFieldOperation previous) {
2829
}
2930
if (previous is ParseSetOperation) {
3031
var setOp = (ParseSetOperation)previous;
31-
var oldList = (IList<object>)ParseClient.ConvertTo<IList<object>>(setOp.Value);
32+
var oldList = (IList<object>)Conversion.ConvertTo<IList<object>>(setOp.Value);
3233
var result = this.Apply(oldList, null);
3334
return new ParseSetOperation(result);
3435
}
@@ -43,7 +44,7 @@ public object Apply(object oldValue, string key) {
4344
if (oldValue == null) {
4445
return objects.ToList();
4546
}
46-
var newList = ((IList<object>)ParseClient.ConvertTo<IList<object>>(oldValue)).ToList();
47+
var newList = ((IList<object>)Conversion.ConvertTo<IList<object>>(oldValue)).ToList();
4748
var comparer = ParseFieldOperations.ParseObjectComparer;
4849
foreach (var objToAdd in objects) {
4950
if (objToAdd is ParseObject) {

Parse/Internal/Operation/ParseRemoveOperation.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Collections.ObjectModel;
66
using System.Linq;
77

8+
using Parse.Utilities;
9+
810
namespace Parse.Internal {
911
class ParseRemoveOperation : IParseFieldOperation {
1012
private ReadOnlyCollection<object> objects;
@@ -28,7 +30,7 @@ public IParseFieldOperation MergeWithPrevious(IParseFieldOperation previous) {
2830
}
2931
if (previous is ParseSetOperation) {
3032
var setOp = (ParseSetOperation)previous;
31-
var oldList = (IList<object>)ParseClient.ConvertTo<IList<object>>(setOp.Value);
33+
var oldList = Conversion.As<IList<object>>(setOp.Value);
3234
return new ParseSetOperation(this.Apply(oldList, null));
3335
}
3436
if (previous is ParseRemoveOperation) {
@@ -42,7 +44,7 @@ public object Apply(object oldValue,string key) {
4244
if (oldValue == null) {
4345
return new List<object>();
4446
}
45-
var oldList = (IList<object>)ParseClient.ConvertTo<IList<object>>(oldValue);
47+
var oldList = Conversion.As<IList<object>>(oldValue);
4648
return oldList.Except(objects, ParseFieldOperations.ParseObjectComparer).ToList();
4749
}
4850

Parse/Internal/Utilities/FlexibleDictionaryWrapper.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System.Collections.Generic;
44
using System.Linq;
5+
using Parse.Utilities;
56

67
#if IOS
78
using PreserveAttribute = Foundation.PreserveAttribute;
@@ -27,7 +28,7 @@ public FlexibleDictionaryWrapper(IDictionary<string, TIn> toWrap) {
2728
}
2829

2930
public void Add(string key, TOut value) {
30-
toWrap.Add(key, (TIn)ParseClient.ConvertTo<TIn>(value));
31+
toWrap.Add(key, (TIn)Conversion.ConvertTo<TIn>(value));
3132
}
3233

3334
public bool ContainsKey(string key) {
@@ -45,29 +46,29 @@ public bool Remove(string key) {
4546
public bool TryGetValue(string key, out TOut value) {
4647
TIn outValue;
4748
bool result = toWrap.TryGetValue(key, out outValue);
48-
value = (TOut)ParseClient.ConvertTo<TOut>(outValue);
49+
value = (TOut)Conversion.ConvertTo<TOut>(outValue);
4950
return result;
5051
}
5152

5253
public ICollection<TOut> Values {
5354
get {
5455
return toWrap.Values
55-
.Select(item => (TOut)ParseClient.ConvertTo<TOut>(item)).ToList();
56+
.Select(item => (TOut)Conversion.ConvertTo<TOut>(item)).ToList();
5657
}
5758
}
5859

5960
public TOut this[string key] {
6061
get {
61-
return (TOut)ParseClient.ConvertTo<TOut>(toWrap[key]);
62+
return (TOut)Conversion.ConvertTo<TOut>(toWrap[key]);
6263
}
6364
set {
64-
toWrap[key] = (TIn)ParseClient.ConvertTo<TIn>(value);
65+
toWrap[key] = (TIn)Conversion.ConvertTo<TIn>(value);
6566
}
6667
}
6768

6869
public void Add(KeyValuePair<string, TOut> item) {
6970
toWrap.Add(new KeyValuePair<string, TIn>(item.Key,
70-
(TIn)ParseClient.ConvertTo<TIn>(item.Value)));
71+
(TIn)Conversion.ConvertTo<TIn>(item.Value)));
7172
}
7273

7374
public void Clear() {
@@ -76,13 +77,13 @@ public void Clear() {
7677

7778
public bool Contains(KeyValuePair<string, TOut> item) {
7879
return toWrap.Contains(new KeyValuePair<string, TIn>(item.Key,
79-
(TIn)ParseClient.ConvertTo<TIn>(item.Value)));
80+
(TIn)Conversion.ConvertTo<TIn>(item.Value)));
8081
}
8182

8283
public void CopyTo(KeyValuePair<string, TOut>[] array, int arrayIndex) {
8384
var converted = from pair in toWrap
8485
select new KeyValuePair<string, TOut>(pair.Key,
85-
(TOut)ParseClient.ConvertTo<TOut>(pair.Value));
86+
(TOut)Conversion.ConvertTo<TOut>(pair.Value));
8687
converted.ToList().CopyTo(array, arrayIndex);
8788
}
8889

@@ -96,13 +97,13 @@ public bool IsReadOnly {
9697

9798
public bool Remove(KeyValuePair<string, TOut> item) {
9899
return toWrap.Remove(new KeyValuePair<string, TIn>(item.Key,
99-
(TIn)ParseClient.ConvertTo<TIn>(item.Value)));
100+
(TIn)Conversion.ConvertTo<TIn>(item.Value)));
100101
}
101102

102103
public IEnumerator<KeyValuePair<string, TOut>> GetEnumerator() {
103104
foreach (var pair in toWrap) {
104105
yield return new KeyValuePair<string, TOut>(pair.Key,
105-
(TOut)ParseClient.ConvertTo<TOut>(pair.Value));
106+
(TOut)Conversion.ConvertTo<TOut>(pair.Value));
106107
}
107108
}
108109

Parse/Internal/Utilities/FlexibleListWrapper.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using Parse.Utilities;
67

78
#if IOS
89
using PreserveAttribute = Foundation.PreserveAttribute;
@@ -28,11 +29,11 @@ public FlexibleListWrapper(IList<TIn> toWrap) {
2829
}
2930

3031
public int IndexOf(TOut item) {
31-
return toWrap.IndexOf((TIn)ParseClient.ConvertTo<TIn>(item));
32+
return toWrap.IndexOf((TIn)Conversion.ConvertTo<TIn>(item));
3233
}
3334

3435
public void Insert(int index, TOut item) {
35-
toWrap.Insert(index, (TIn)ParseClient.ConvertTo<TIn>(item));
36+
toWrap.Insert(index, (TIn)Conversion.ConvertTo<TIn>(item));
3637
}
3738

3839
public void RemoveAt(int index) {
@@ -41,27 +42,27 @@ public void RemoveAt(int index) {
4142

4243
public TOut this[int index] {
4344
get {
44-
return (TOut)ParseClient.ConvertTo<TOut>(toWrap[index]);
45+
return (TOut)Conversion.ConvertTo<TOut>(toWrap[index]);
4546
}
4647
set {
47-
toWrap[index] = (TIn)ParseClient.ConvertTo<TIn>(value);
48+
toWrap[index] = (TIn)Conversion.ConvertTo<TIn>(value);
4849
}
4950
}
5051

5152
public void Add(TOut item) {
52-
toWrap.Add((TIn)ParseClient.ConvertTo<TIn>(item));
53+
toWrap.Add((TIn)Conversion.ConvertTo<TIn>(item));
5354
}
5455

5556
public void Clear() {
5657
toWrap.Clear();
5758
}
5859

5960
public bool Contains(TOut item) {
60-
return toWrap.Contains((TIn)ParseClient.ConvertTo<TIn>(item));
61+
return toWrap.Contains((TIn)Conversion.ConvertTo<TIn>(item));
6162
}
6263

6364
public void CopyTo(TOut[] array, int arrayIndex) {
64-
toWrap.Select(item => (TOut)ParseClient.ConvertTo<TOut>(item))
65+
toWrap.Select(item => (TOut)Conversion.ConvertTo<TOut>(item))
6566
.ToList().CopyTo(array, arrayIndex);
6667
}
6768

@@ -74,12 +75,12 @@ public bool IsReadOnly {
7475
}
7576

7677
public bool Remove(TOut item) {
77-
return toWrap.Remove((TIn)ParseClient.ConvertTo<TIn>(item));
78+
return toWrap.Remove((TIn)Conversion.ConvertTo<TIn>(item));
7879
}
7980

8081
public IEnumerator<TOut> GetEnumerator() {
8182
foreach (var item in (IEnumerable)toWrap) {
82-
yield return (TOut)ParseClient.ConvertTo<TOut>(item);
83+
yield return (TOut)Conversion.ConvertTo<TOut>(item);
8384
}
8485
}
8586

0 commit comments

Comments
 (0)