aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore/DataStoreHelper.cs
blob: f376afcec720ed5c758e61ec5d97782c05664b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using Google.Protobuf;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tango.Core.ExtensionMethods;
using Tango.PMR;
using Tango.PMR.Common;
using Tango.PMR.DataStore;

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)
        {
            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[])obj);
            }
            else if (type == DataType.Proto)
            {
                return (obj as DataStoreProtoObject).Message.ToJsonString();
            }
            else
            {
                return obj.ToStringSafe();
            }
        }

        /// <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, DataStoreMessageType? 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.GetDataStorePMRTypeFromDataStoreMessageType(protoMessageType.Value);
                    var instance = Activator.CreateInstance(messageType) as IMessage;
                    instance = instance.GetParser().ParseJson(text);
                    return DataStoreProtoObject.FromMessage(instance);
                case DataType.Bytes:
                    return Convert.FromBase64String(text);
            }

            throw new NotSupportedException("The specified data store type is not supported.");
        }

        /// <summary>
        /// Validates the name of the collection or key.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static bool ValidateCollectionOrKeyName(String name)
        {
            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.");
        }
    }
}