summaryrefslogtreecommitdiff
path: root/FireBase/Streaming/FirebaseCache.cs
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-05-19 16:03:38 +0200
committerDennis Kobert <d-kobert@web.de>2019-05-19 16:03:38 +0200
commitf89f308c525e9deebc6d2cf6416e27dfe1a299dc (patch)
tree7097ef871ead0245efda696198443eab8e443d3a /FireBase/Streaming/FirebaseCache.cs
parentf3983341be939235c1a6cd522b3bb5cc318a6d1a (diff)
Cleanup DiscoBot Project
Diffstat (limited to 'FireBase/Streaming/FirebaseCache.cs')
-rw-r--r--FireBase/Streaming/FirebaseCache.cs58
1 files changed, 24 insertions, 34 deletions
diff --git a/FireBase/Streaming/FirebaseCache.cs b/FireBase/Streaming/FirebaseCache.cs
index ba7990b..77fc622 100644
--- a/FireBase/Streaming/FirebaseCache.cs
+++ b/FireBase/Streaming/FirebaseCache.cs
@@ -5,9 +5,7 @@ namespace Firebase.Database.Streaming
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
-
- using Firebase.Database.Http;
-
+ using Http;
using Newtonsoft.Json;
/// <summary>
@@ -18,6 +16,7 @@ namespace Firebase.Database.Streaming
{
private readonly IDictionary<string, T> dictionary;
private readonly bool isDictionaryType;
+
private readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings()
{
ObjectCreationHandling = ObjectCreationHandling.Replace
@@ -26,7 +25,7 @@ namespace Firebase.Database.Streaming
/// <summary>
/// Initializes a new instance of the <see cref="FirebaseCache{T}"/> class.
/// </summary>
- public FirebaseCache()
+ public FirebaseCache()
: this(new Dictionary<string, T>())
{
}
@@ -37,8 +36,8 @@ namespace Firebase.Database.Streaming
/// <param name="existingItems"> The existing items. </param>
public FirebaseCache(IDictionary<string, T> existingItems)
{
- this.dictionary = existingItems;
- this.isDictionaryType = typeof(IDictionary).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo());
+ dictionary = existingItems;
+ isDictionaryType = typeof(IDictionary).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo());
}
/// <summary>
@@ -53,11 +52,11 @@ namespace Firebase.Database.Streaming
Action<object> primitiveObjSetter = null;
Action objDeleter = null;
- var pathElements = path.Split(new[] { "/" }, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
+ var pathElements = path.Split(new[] {"/"},
+ removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
// first find where we should insert the data to
foreach (var element in pathElements)
- {
if (obj is IDictionary)
{
// if it's a dictionary, then it's just a matter of inserting into it / accessing existing object by key
@@ -73,7 +72,7 @@ namespace Firebase.Database.Streaming
}
else
{
- dictionary[element] = this.CreateInstance(valueType);
+ dictionary[element] = CreateInstance(valueType);
obj = dictionary[element];
}
}
@@ -84,24 +83,24 @@ namespace Firebase.Database.Streaming
var property = objParent
.GetType()
.GetRuntimeProperties()
- .First(p => p.Name.Equals(element, StringComparison.OrdinalIgnoreCase) || element == p.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName);
+ .First(p => p.Name.Equals(element, StringComparison.OrdinalIgnoreCase) ||
+ element == p.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName);
objDeleter = () => property.SetValue(objParent, null);
primitiveObjSetter = (d) => property.SetValue(objParent, d);
obj = property.GetValue(obj);
if (obj == null)
{
- obj = this.CreateInstance(property.PropertyType);
+ obj = CreateInstance(property.PropertyType);
property.SetValue(objParent, obj);
}
}
- }
// if data is null (=empty string) delete it
if (string.IsNullOrWhiteSpace(data) || data == "null")
{
var key = pathElements[0];
- var target = this.dictionary[key];
+ var target = dictionary[key];
objDeleter();
@@ -110,7 +109,7 @@ namespace Firebase.Database.Streaming
}
// now insert the data
- if (obj is IDictionary && !this.isDictionaryType)
+ if (obj is IDictionary && !isDictionaryType)
{
// insert data into dictionary and return it as a collection of FirebaseObject
var dictionary = obj as IDictionary;
@@ -122,10 +121,7 @@ namespace Firebase.Database.Streaming
dictionary[item.Key] = item.Object;
// top level dictionary changed
- if (!pathElements.Any())
- {
- yield return new FirebaseObject<T>(item.Key, (T)item.Object);
- }
+ if (!pathElements.Any()) yield return new FirebaseObject<T>(item.Key, (T) item.Object);
}
// nested dictionary changed
@@ -141,52 +137,46 @@ namespace Firebase.Database.Streaming
var valueType = obj.GetType();
// firebase sends strings without double quotes
- var targetObject = valueType == typeof(string) ? data.ToString() : JsonConvert.DeserializeObject(data, valueType);
+ var targetObject = valueType == typeof(string)
+ ? data.ToString()
+ : JsonConvert.DeserializeObject(data, valueType);
if ((valueType.GetTypeInfo().IsPrimitive || valueType == typeof(string)) && primitiveObjSetter != null)
- {
// handle primitive (value) types separately
primitiveObjSetter(targetObject);
- }
else
- {
- JsonConvert.PopulateObject(data, obj, this.serializerSettings);
- }
+ JsonConvert.PopulateObject(data, obj, serializerSettings);
- this.dictionary[pathElements[0]] = this.dictionary[pathElements[0]];
- yield return new FirebaseObject<T>(pathElements[0], this.dictionary[pathElements[0]]);
+ dictionary[pathElements[0]] = dictionary[pathElements[0]];
+ yield return new FirebaseObject<T>(pathElements[0], dictionary[pathElements[0]]);
}
}
public bool Contains(string key)
{
- return this.dictionary.Keys.Contains(key);
+ return dictionary.Keys.Contains(key);
}
private object CreateInstance(Type type)
{
if (type == typeof(string))
- {
return string.Empty;
- }
else
- {
return Activator.CreateInstance(type);
- }
}
#region IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
- return this.GetEnumerator();
+ return GetEnumerator();
}
public IEnumerator<FirebaseObject<T>> GetEnumerator()
{
- return this.dictionary.Select(p => new FirebaseObject<T>(p.Key, p.Value)).GetEnumerator();
+ return dictionary.Select(p => new FirebaseObject<T>(p.Key, p.Value)).GetEnumerator();
}
#endregion
}
-}
+} \ No newline at end of file