aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.DataStore/DataStoreHelper.cs
blob: 3cf24bdd08ab50de3fdd335530bf6e03b20f4323 (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
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(Int32))
            {
                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>
        /// Gets the CLR type by specified data store data type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">The specified data type is not supported.</exception>
        //public static Type GetType(DataType type)
        //{
        //    switch (type)
        //    {
        //        case DataType.Boolean:
        //            return typeof(bool);
        //        case DataType.Bytes:
        //            return typeof(byte[]);
        //        case DataType.Double:
        //            return typeof(double);
        //        case DataType.Float:
        //            return typeof(float);
        //        case DataType.Int32:
        //            return typeof(Int32);
        //        case DataType.String:
        //            return typeof(String);
        //        case DataType.Proto:
        //            return typeof(DataStoreProtoObject);
        //    }

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

        /// <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)
            {
                byte[] bytes = (byte[])item.Value;

                StringBuilder hex = new StringBuilder();
                foreach (byte b in bytes)
                {
                    hex.AppendFormat("{0:x2} ", b);
                }
                return hex.ToString();
            }
            else if (item.Type == DataType.Proto)
            {
                return (item.Value as DataStoreProtoObject).Message.ToJsonString();
            }
            else
            {
                return $"{item.Key}: {item.Value}";
            }
        }
    }
}