FirebaseRestTranslator: Add support for: Reference, GeoPoint, Null and Bytes values
This commit is contained in:
parent
7c07a2a57c
commit
ac8f58df9a
2 changed files with 126 additions and 0 deletions
|
@ -8,6 +8,42 @@ namespace NucuCar.Domain.Utilities
|
|||
/// </summary>
|
||||
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<string, object> ToValue()
|
||||
{
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
["latitude"] = _latitude,
|
||||
["longitude"] = _longitude
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, object> Translate(string name, Dictionary<string, object> dict)
|
||||
{
|
||||
return BuildRoot(name, dict);
|
||||
|
@ -52,10 +88,26 @@ namespace NucuCar.Domain.Utilities
|
|||
{
|
||||
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<Dictionary<string, object>> v:
|
||||
{
|
||||
return BuildArray(v);
|
||||
|
@ -74,9 +126,11 @@ namespace NucuCar.Domain.Utilities
|
|||
{
|
||||
return BuildInteger((int) value);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Can't build leaf! Unknown type for: {value}");
|
||||
}
|
||||
|
||||
|
@ -113,6 +167,27 @@ namespace NucuCar.Domain.Utilities
|
|||
return BuildSimpleValue("booleanValue", value);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
private static Dictionary<string, object> BuildArray(List<Dictionary<string, object>> array)
|
||||
{
|
||||
var values = new List<Dictionary<string, object>>();
|
||||
|
|
|
@ -202,5 +202,56 @@ namespace NucuCar.UnitTests.NucuCar.Domain.Tests.Utilities
|
|||
Assert.Equal(expectedJson, actualJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_FirebaseTranslator_NullValue()
|
||||
{
|
||||
var data = new Dictionary<string, object>()
|
||||
{
|
||||
["myKey"] = null
|
||||
};
|
||||
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"nullValue\":null}}}";
|
||||
var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data);
|
||||
var actualJson = JsonConvert.SerializeObject(result);
|
||||
Assert.Equal(expectedJson, actualJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_FirebaseTranslator_BytesValue()
|
||||
{
|
||||
var data = new Dictionary<string, object>()
|
||||
{
|
||||
["myKey"] = new byte[] {97, 98, 99, 100, 101, 102, 103, 104, 105 }
|
||||
};
|
||||
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"bytesValue\":\"YWJjZGVmZ2hp\"}}}";
|
||||
var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data);
|
||||
var actualJson = JsonConvert.SerializeObject(result);
|
||||
Assert.Equal(expectedJson, actualJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_FirebaseTranslator_ReferenceValue()
|
||||
{
|
||||
var data = new Dictionary<string, object>()
|
||||
{
|
||||
["myKey"] = new FirebaseRestTranslator.ReferenceValue("test")
|
||||
};
|
||||
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"referenceValue\":\"test\"}}}";
|
||||
var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data);
|
||||
var actualJson = JsonConvert.SerializeObject(result);
|
||||
Assert.Equal(expectedJson, actualJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_FirebaseTranslator_GeoPointvalue()
|
||||
{
|
||||
var data = new Dictionary<string, object>()
|
||||
{
|
||||
["myKey"] = new FirebaseRestTranslator.GeoPointValue(10, 22)
|
||||
};
|
||||
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"geoPointValue\":{\"latitude\":10.0,\"longitude\":22.0}}}}";
|
||||
var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data);
|
||||
var actualJson = JsonConvert.SerializeObject(result);
|
||||
Assert.Equal(expectedJson, actualJson);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue