diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-11-14 22:02:45 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-11-14 22:02:45 +0200 |
| commit | 76b22e4d05cbd8d771f678e4b5adc2dc5159afa8 (patch) | |
| tree | 815892d26cbf716d9fae9e01d46109299875d94d /Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs | |
| parent | f838a715af54ef7fc35bf9d99fee95dd8ac6533f (diff) | |
| download | Tango-76b22e4d05cbd8d771f678e4b5adc2dc5159afa8.tar.gz Tango-76b22e4d05cbd8d771f678e4b5adc2dc5159afa8.zip | |
Moved data store projects to DataStore folder.
Diffstat (limited to 'Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs | 160 |
1 files changed, 0 insertions, 160 deletions
diff --git a/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs b/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs deleted file mode 100644 index 0ca8e484e..000000000 --- a/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs +++ /dev/null @@ -1,160 +0,0 @@ -using Google.Protobuf; -using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Core.ExtensionMethods; -using Tango.PMR; -using Tango.PMR.Common; - -namespace Tango.DataStore -{ - /// <summary> - /// Contains data store helper methods. - /// </summary> - public static class DataStoreHelper - { - /// <summary> - /// Gets the data store data type by the specified object type. - /// </summary> - /// <param name="value">The value.</param> - /// <returns></returns> - public static DataType GetDataType(Object value) - { - return GetDataType(value.GetType()); - } - - /// <summary> - /// Gets data store data type by the specified .net type. - /// </summary> - /// <param name="type">The type.</param> - /// <returns></returns> - /// <exception cref="System.NotSupportedException"></exception> - public static DataType GetDataType(Type type) - { - if (type == typeof(int)) - { - return DataType.Int32; - } - else if (type == typeof(Int64)) - { - return DataType.Int32; - } - else if (type == typeof(float)) - { - return DataType.Float; - } - else if (type == typeof(double)) - { - return DataType.Double; - } - else if (type == typeof(String)) - { - return DataType.String; - } - else if (type == typeof(bool)) - { - return DataType.Boolean; - } - else if (type == typeof(byte[])) - { - return DataType.Bytes; - } - else if (type == typeof(DataStoreProtoObject)) - { - return DataType.Proto; - } - else if (typeof(IMessage).IsAssignableFrom(type)) - { - return DataType.Proto; - } - - throw new NotSupportedException($"The specified type '{type.Name}' is not supported by the data store."); - } - - /// <summary> - /// Formats the data store item as a string. - /// </summary> - /// <param name="item">The item.</param> - /// <returns></returns> - public static String FormatDataStoreItem(IDataStoreItem item) - { - if (item.Type == DataType.Bytes) - { - return GetByteArrayHexString((byte[])item.Value); - } - else if (item.Type == DataType.Proto) - { - return (item.Value as DataStoreProtoObject).Message.ToJsonString(); - } - else - { - return item.Value.ToStringSafe(); - } - } - - /// <summary> - /// Returns a byte array string representation in hex format. - /// </summary> - /// <param name="data">The data.</param> - /// <returns></returns> - public static String GetByteArrayHexString(byte[] data) - { - StringBuilder hex = new StringBuilder(); - foreach (byte b in data) - { - hex.Append(b.ToString("X2") + " "); - } - return hex.ToString(); - } - - /// <summary> - /// Parses a data store value from a string. - /// </summary> - /// <param name="type">The type.</param> - /// <param name="text">The string.</param> - /// <param name="protoMessageType">Type of the proto message (if type is Proto).</param> - /// <returns></returns> - /// <exception cref="ArgumentNullException">No PMR message type specified.</exception> - /// <exception cref="NotSupportedException">The specified data store type is not supported.</exception> - public static Object ParseDataStoreValue(DataType type, String text, MessageType? protoMessageType = null) - { - switch (type) - { - case DataType.String: - return text; - case DataType.Int32: - return int.Parse(text); - case DataType.Float: - return float.Parse(text); - case DataType.Double: - return double.Parse(text); - case DataType.Boolean: - return bool.Parse(text); - case DataType.Proto: - if (protoMessageType == null) throw new ArgumentNullException("No PMR message type specified."); - var messageType = MessageFactory.GetPMRTypeFromMessageType(protoMessageType.Value); - var instance = Activator.CreateInstance(messageType) as IMessage; - instance = instance.GetParser().ParseJson(text); - return DataStoreProtoObject.FromMessage(instance); - case DataType.Bytes: - string[] hexValuesSplit = text.Split(' '); - List<byte> bytes = new List<byte>(); - foreach (string hex in hexValuesSplit) - { - if (hex.IsNotNullOrEmpty()) - { - byte b = (byte)Convert.ToInt32(hex.Trim(), 16); - bytes.Add(b); - } - } - return bytes.ToArray(); - } - - throw new NotSupportedException("The specified data store type is not supported."); - } - } -} |
