2020-02-16 11:55:29 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace NucuCar.Domain.Utilities
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// This class is used to translate C# dictionaries into firebase json format.
|
|
|
|
/// </summary>
|
|
|
|
public static class FirebaseRestTranslator
|
|
|
|
{
|
2020-03-07 10:40:25 +00:00
|
|
|
public class ReferenceValue
|
|
|
|
{
|
|
|
|
private readonly string _value;
|
|
|
|
|
|
|
|
public ReferenceValue(string value)
|
|
|
|
{
|
|
|
|
_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class GeoPointValue
|
|
|
|
{
|
|
|
|
private readonly double _latitude;
|
|
|
|
private readonly double _longitude;
|
|
|
|
|
|
|
|
public GeoPointValue(double latitude, double longitude)
|
|
|
|
{
|
|
|
|
_latitude = latitude;
|
|
|
|
_longitude = longitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Dictionary<string, object> ToValue()
|
|
|
|
{
|
|
|
|
return new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
["latitude"] = _latitude,
|
|
|
|
["longitude"] = _longitude
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
public static Dictionary<string, object> Translate(string name, Dictionary<string, object> dict)
|
|
|
|
{
|
|
|
|
return BuildRoot(name, dict);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
private static Dictionary<string, object> BuildRoot(string name, Dictionary<string, object> dict)
|
|
|
|
{
|
|
|
|
var root = new Dictionary<string, object>();
|
2020-02-16 13:45:41 +00:00
|
|
|
if (name != null)
|
|
|
|
{
|
|
|
|
root["name"] = name;
|
|
|
|
}
|
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
root["fields"] = new Dictionary<string, object>();
|
|
|
|
// iterate through fields and build leaf
|
|
|
|
foreach (var entry in dict)
|
|
|
|
{
|
|
|
|
var fields = (Dictionary<string, object>) root["fields"];
|
|
|
|
fields[entry.Key] = BuildNode(entry.Value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
return root;
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
private static Dictionary<string, object> BuildNode(object value)
|
|
|
|
{
|
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
case string v:
|
|
|
|
{
|
|
|
|
return BuildString(v);
|
|
|
|
}
|
|
|
|
case int v:
|
|
|
|
{
|
|
|
|
return BuildInteger(v);
|
|
|
|
}
|
|
|
|
case double v:
|
|
|
|
{
|
|
|
|
return BuildDouble(v);
|
|
|
|
}
|
2020-02-16 12:59:49 +00:00
|
|
|
case bool v:
|
|
|
|
{
|
|
|
|
return BuildBool(v);
|
|
|
|
}
|
2020-03-07 10:40:25 +00:00
|
|
|
case null:
|
|
|
|
{
|
|
|
|
return BuildNull(null);
|
|
|
|
}
|
|
|
|
case byte[] v:
|
|
|
|
{
|
|
|
|
return BuildBytes(v);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
case DateTime v:
|
|
|
|
{
|
|
|
|
return BuildTimestamp(v);
|
|
|
|
}
|
2020-03-07 10:40:25 +00:00
|
|
|
case ReferenceValue v:
|
|
|
|
{
|
|
|
|
return BuildReference(v);
|
|
|
|
}
|
|
|
|
case GeoPointValue v:
|
|
|
|
{
|
|
|
|
return BuildGeoPoint(v);
|
|
|
|
}
|
2020-02-16 11:55:29 +00:00
|
|
|
case List<Dictionary<string, object>> v:
|
|
|
|
{
|
|
|
|
return BuildArray(v);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
case Dictionary<string, object>[] v:
|
|
|
|
{
|
|
|
|
return BuildArray(new List<Dictionary<string, object>>(v));
|
|
|
|
}
|
2020-02-16 11:55:29 +00:00
|
|
|
case Dictionary<string, object> v:
|
|
|
|
{
|
|
|
|
return BuildMap(v);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (value.GetType().IsEnum)
|
|
|
|
{
|
|
|
|
return BuildInteger((int) value);
|
|
|
|
}
|
2020-03-07 10:40:25 +00:00
|
|
|
|
2020-02-16 13:45:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-02-16 11:55:29 +00:00
|
|
|
}
|
2020-03-07 10:40:25 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
throw new ArgumentException($"Can't build leaf! Unknown type for: {value}");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<string, object> BuildSimpleValue(string type, object value)
|
|
|
|
{
|
|
|
|
return new Dictionary<string, object>()
|
|
|
|
{
|
|
|
|
[type] = value
|
|
|
|
};
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
private static Dictionary<string, object> BuildString(string value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("stringValue", value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
private static Dictionary<string, object> BuildInteger(int value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("integerValue", value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
|
|
|
private static Dictionary<string, object> BuildTimestamp(DateTime value)
|
2020-02-16 11:55:29 +00:00
|
|
|
{
|
|
|
|
return BuildSimpleValue("timestampValue", value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
private static Dictionary<string, object> BuildDouble(double value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("doubleValue", value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 12:59:49 +00:00
|
|
|
private static Dictionary<string, object> BuildBool(bool value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("booleanValue", value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-03-07 10:40:25 +00:00
|
|
|
private static Dictionary<string, object> BuildNull(object value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("nullValue", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<string, object> BuildBytes(byte[] value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("bytesValue", Convert.ToBase64String(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<string, object> BuildReference(ReferenceValue value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("referenceValue", value.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<string, object> BuildGeoPoint(GeoPointValue value)
|
|
|
|
{
|
|
|
|
return BuildSimpleValue("geoPointValue", value.ToValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
private static Dictionary<string, object> BuildArray(List<Dictionary<string, object>> array)
|
|
|
|
{
|
|
|
|
var values = new List<Dictionary<string, object>>();
|
2020-02-16 12:58:02 +00:00
|
|
|
var root = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
["arrayValue"] = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
["values"] = values
|
|
|
|
}
|
|
|
|
};
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
foreach (var entry in array)
|
|
|
|
{
|
|
|
|
values.Add(BuildNode(entry));
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Dictionary<string, object> BuildMap(Dictionary<string, object> map)
|
|
|
|
{
|
|
|
|
var fields = new Dictionary<string, object>();
|
|
|
|
var root = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
["mapValue"] = new Dictionary<string, object>
|
|
|
|
{
|
|
|
|
["fields"] = fields
|
|
|
|
}
|
|
|
|
};
|
|
|
|
foreach (var entry in map)
|
|
|
|
{
|
|
|
|
fields[entry.Key] = BuildNode(entry.Value);
|
|
|
|
}
|
2020-02-16 13:45:41 +00:00
|
|
|
|
2020-02-16 11:55:29 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|