aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/DataStore/Tango.DataStore')
-rw-r--r--Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs92
1 files changed, 87 insertions, 5 deletions
diff --git a/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs b/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs
index 0ceecd81b..0409dbf7a 100644
--- a/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs
+++ b/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs
@@ -83,17 +83,28 @@ namespace Tango.DataStore
/// <returns></returns>
public static String FormatDataStoreItem(IDataStoreItem item)
{
- if (item.Type == DataType.Bytes)
+ return FormatDataStoreValue(item.Type, item.Value);
+ }
+
+ /// <summary>
+ /// Formats the data store value.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="obj">The object.</param>
+ /// <returns></returns>
+ public static String FormatDataStoreValue(DataType type, object obj)
+ {
+ if (type == DataType.Bytes)
{
- return Convert.ToBase64String((byte[])item.Value);
+ return Convert.ToBase64String((byte[])obj);
}
- else if (item.Type == DataType.Proto)
+ else if (type == DataType.Proto)
{
- return (item.Value as DataStoreProtoObject).Message.ToJsonString();
+ return (obj as DataStoreProtoObject).Message.ToJsonString();
}
else
{
- return item.Value.ToStringSafe();
+ return obj.ToStringSafe();
}
}
@@ -143,5 +154,76 @@ namespace Tango.DataStore
var regexItem = new Regex("^[a-zA-Z0-9_-]*$");
return regexItem.IsMatch(name);
}
+
+ /// <summary>
+ /// Creates a data store value from byte array that are stored in database.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="bytes">The bytes.</param>
+ /// <returns></returns>
+ /// <exception cref="System.NotSupportedException">The specified type is not supported.</exception>
+ public static Object CreateObject(DataType type, byte[] bytes)
+ {
+ switch (type)
+ {
+ case DataType.Int32:
+ return BitConverter.ToInt32(bytes, 0);
+ case DataType.Float:
+ return BitConverter.ToSingle(bytes, 0);
+ case DataType.Double:
+ return BitConverter.ToDouble(bytes, 0);
+ case DataType.Boolean:
+ return BitConverter.ToBoolean(bytes, 0);
+ case DataType.String:
+ return Encoding.Default.GetString(bytes);
+ case DataType.Bytes:
+ return bytes;
+ case DataType.Proto:
+ return DataStoreProtoObject.FromBytes(bytes);
+ }
+
+ throw new NotSupportedException("The specified type is not supported.");
+ }
+
+ /// <summary>
+ /// Creates a byte array that can be stored on database from the specified data store type and value.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="obj">The object.</param>
+ /// <returns></returns>
+ /// <exception cref="System.NotSupportedException">
+ /// DataStoreProtoObject
+ /// or
+ /// The specified type is not supported.
+ /// </exception>
+ public static byte[] CreateBytes(DataType type, Object obj)
+ {
+ switch (type)
+ {
+ case DataType.Int32:
+ return BitConverter.GetBytes((int)obj);
+ case DataType.Float:
+ return BitConverter.GetBytes((float)obj);
+ case DataType.Double:
+ return BitConverter.GetBytes((double)obj);
+ case DataType.Boolean:
+ return BitConverter.GetBytes((bool)obj);
+ case DataType.String:
+ return Encoding.Default.GetBytes(obj.ToString());
+ case DataType.Bytes:
+ return (byte[])obj;
+ case DataType.Proto:
+ if (obj is DataStoreProtoObject protoMessage)
+ {
+ return protoMessage.ToBytes();
+ }
+ else
+ {
+ throw new NotSupportedException($"Data type is 'Proto' but object is not of type '{nameof(DataStoreProtoObject)}'.");
+ }
+ }
+
+ throw new NotSupportedException("The specified type is not supported.");
+ }
}
}