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