Initial commit
This commit is contained in:
commit
fe5841a2f6
9 changed files with 548 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
/packages/
|
||||||
|
.idea
|
|
@ -0,0 +1,20 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.0" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||||
|
<PackageReference Include="coverlet.collector" Version="1.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FirebaseRestTranslator\FirebaseRestTranslator.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
235
FirebaseRestTranslator.UnitTests/FirebaseRestTranslatorTests.cs
Normal file
235
FirebaseRestTranslator.UnitTests/FirebaseRestTranslatorTests.cs
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace FirebaseRestTranslator.UnitTests
|
||||||
|
{
|
||||||
|
enum Season
|
||||||
|
{
|
||||||
|
Spring,
|
||||||
|
Summer,
|
||||||
|
Autumn,
|
||||||
|
Winter
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class FirebaseRestTranslatorTests
|
||||||
|
{
|
||||||
|
public FirebaseRestTranslatorTests(ITestOutputHelper testOutputHelper)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, object> getBasicTelemetryData()
|
||||||
|
{
|
||||||
|
var basicTelemetryDict = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["source"] = "NucuCar.Sensors",
|
||||||
|
["timestamp"] = "2019-12-01T23:26:13.5537227+02:00"
|
||||||
|
};
|
||||||
|
var data = new List<Dictionary<string, object>>
|
||||||
|
{
|
||||||
|
new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["sensor_state"] = 2,
|
||||||
|
["cpu_temperature"] = 48.849998474121094,
|
||||||
|
["_id"] = "CpuTemperature",
|
||||||
|
},
|
||||||
|
new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["sensor_state"] = 2,
|
||||||
|
["temperature"] = 32.65,
|
||||||
|
["humidity"] = 100.0,
|
||||||
|
["pressure"] = 62228.49,
|
||||||
|
["voc"] = 0.0,
|
||||||
|
["_id"] = "Bme680-Sensor"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
basicTelemetryDict["data"] = data;
|
||||||
|
return basicTelemetryDict;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_Parse()
|
||||||
|
{
|
||||||
|
var expectedJson =
|
||||||
|
"{\"name\":\"Test\",\"fields\":{\"source\":{\"stringValue\":\"NucuCar.Sensors\"},\"timestamp\":{\"stringValue\":\"2019-12-01T23:26:13.5537227+02:00\"},\"data\":{\"arrayValue\":{\"values\":[{\"mapValue\":{\"fields\":{\"sensor_state\":{\"integerValue\":2},\"cpu_temperature\":{\"doubleValue\":48.849998474121094},\"_id\":{\"stringValue\":\"CpuTemperature\"}}}},{\"mapValue\":{\"fields\":{\"sensor_state\":{\"integerValue\":2},\"temperature\":{\"doubleValue\":32.65},\"humidity\":{\"doubleValue\":100.0},\"pressure\":{\"doubleValue\":62228.49},\"voc\":{\"doubleValue\":0.0},\"_id\":{\"stringValue\":\"Bme680-Sensor\"}}}}]}}}}";
|
||||||
|
var basicTelemetryData = getBasicTelemetryData();
|
||||||
|
var result = Translator.Translate("Test", basicTelemetryData);
|
||||||
|
var json = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_StringValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = "myValue"
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator_StringValue\",\"fields\":{\"myKey\":{\"stringValue\":\"myValue\"}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator_StringValue", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_IntegerValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = 23
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"integerValue\":23}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_DoubleValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = 11.20
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"doubleValue\":11.2}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_BoolValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = false
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"booleanValue\":false}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_TimestampValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = new DateTime(2020, 2, 29, 0, 0, 0, 0)
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"timestampValue\":\"2020-02-29T00:00:00\"}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_EnumValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = Season.Winter
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"integerValue\":3}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_ArrayValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = new List<Dictionary<string, object>>
|
||||||
|
{
|
||||||
|
new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["arrayIndex0"] = 11.20
|
||||||
|
},
|
||||||
|
new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["arrayIndex1"] = "test"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"arrayValue\":{\"values\":[{\"mapValue\":{\"fields\":{\"arrayIndex0\":{\"doubleValue\":11.2}}}},{\"mapValue\":{\"fields\":{\"arrayIndex1\":{\"stringValue\":\"test\"}}}}]}}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_MapValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["stringKey"] = "test",
|
||||||
|
["boolKey"] = true,
|
||||||
|
["intKey"] = 99
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"mapValue\":{\"fields\":{\"stringKey\":{\"stringValue\":\"test\"},\"boolKey\":{\"booleanValue\":true},\"intKey\":{\"integerValue\":99}}}}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
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 = Translator.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 = Translator.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 ReferenceValue("test")
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"referenceValue\":\"test\"}}}";
|
||||||
|
var result = Translator.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 GeoPointValue(10, 22)
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"geoPointValue\":{\"latitude\":10.0,\"longitude\":22.0}}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
FirebaseRestTranslator.sln
Normal file
22
FirebaseRestTranslator.sln
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseRestTranslator", "FirebaseRestTranslator\FirebaseRestTranslator.csproj", "{B1444209-4E08-441E-BE6B-0B2D41145D2C}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseRestTranslator.UnitTests", "FirebaseRestTranslator.UnitTests\FirebaseRestTranslator.UnitTests.csproj", "{2862F03A-8A57-45F4-80D5-797D8E326850}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{B1444209-4E08-441E-BE6B-0B2D41145D2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B1444209-4E08-441E-BE6B-0B2D41145D2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B1444209-4E08-441E-BE6B-0B2D41145D2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B1444209-4E08-441E-BE6B-0B2D41145D2C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{2862F03A-8A57-45F4-80D5-797D8E326850}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{2862F03A-8A57-45F4-80D5-797D8E326850}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{2862F03A-8A57-45F4-80D5-797D8E326850}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{2862F03A-8A57-45F4-80D5-797D8E326850}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
10
FirebaseRestTranslator/FirebaseRestTranslator.csproj
Normal file
10
FirebaseRestTranslator/FirebaseRestTranslator.csproj
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
<PackageId>FirebaseRestTranslator</PackageId>
|
||||||
|
<Version>0.1.0</Version>
|
||||||
|
<Authors>Denis Nutiu</Authors>
|
||||||
|
<Company>NucuLabs</Company>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
25
FirebaseRestTranslator/GeoPointValue.cs
Normal file
25
FirebaseRestTranslator/GeoPointValue.cs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace FirebaseRestTranslator
|
||||||
|
{
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
FirebaseRestTranslator/ReferenceValue.cs
Normal file
17
FirebaseRestTranslator/ReferenceValue.cs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
namespace FirebaseRestTranslator
|
||||||
|
{
|
||||||
|
public class ReferenceValue
|
||||||
|
{
|
||||||
|
private readonly string _value;
|
||||||
|
|
||||||
|
public ReferenceValue(string value)
|
||||||
|
{
|
||||||
|
_value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return _value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
193
FirebaseRestTranslator/Translator.cs
Normal file
193
FirebaseRestTranslator/Translator.cs
Normal file
|
@ -0,0 +1,193 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace FirebaseRestTranslator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// This class is used to translate C# dictionaries into Firebase json format.
|
||||||
|
/// </summary>
|
||||||
|
public static class Translator
|
||||||
|
{
|
||||||
|
|
||||||
|
public static Dictionary<string, object> Translate(string name, Dictionary<string, object> dict)
|
||||||
|
{
|
||||||
|
return BuildRoot(name, dict);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> BuildRoot(string name, Dictionary<string, object> dict)
|
||||||
|
{
|
||||||
|
var root = new Dictionary<string, object>();
|
||||||
|
if (name != null)
|
||||||
|
{
|
||||||
|
root["name"] = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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<Dictionary<string, object>> v:
|
||||||
|
{
|
||||||
|
return BuildArray(v);
|
||||||
|
}
|
||||||
|
case Dictionary<string, object>[] v:
|
||||||
|
{
|
||||||
|
return BuildArray(new List<Dictionary<string, object>>(v));
|
||||||
|
}
|
||||||
|
case Dictionary<string, object> 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<string, object> BuildSimpleValue(string type, object value)
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
[type] = value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> BuildString(string value)
|
||||||
|
{
|
||||||
|
return BuildSimpleValue("stringValue", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> BuildInteger(int value)
|
||||||
|
{
|
||||||
|
return BuildSimpleValue("integerValue", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> BuildTimestamp(DateTime value)
|
||||||
|
{
|
||||||
|
return BuildSimpleValue("timestampValue", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> BuildDouble(double value)
|
||||||
|
{
|
||||||
|
return BuildSimpleValue("doubleValue", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, object> BuildBool(bool value)
|
||||||
|
{
|
||||||
|
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>>();
|
||||||
|
var root = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["arrayValue"] = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["values"] = values
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var entry in array)
|
||||||
|
{
|
||||||
|
values.Add(BuildNode(entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
readme.md
Normal file
22
readme.md
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# FirebareRestTranslator
|
||||||
|
|
||||||
|
This is a small helper class that can be used to translate C#
|
||||||
|
dictionaries into the Firebase REST format so they be posted
|
||||||
|
to Firebase directly.
|
||||||
|
|
||||||
|
See the tests for usage.
|
||||||
|
|
||||||
|
```c#
|
||||||
|
[Fact]
|
||||||
|
public void Test_FirebaseTranslator_BoolValue()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
["myKey"] = false
|
||||||
|
};
|
||||||
|
var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"booleanValue\":false}}}";
|
||||||
|
var result = Translator.Translate("Test_FirebaseTranslator", data);
|
||||||
|
var actualJson = JsonConvert.SerializeObject(result);
|
||||||
|
Assert.Equal(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in a new issue