| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Models |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using Common; |
| | 9 | | using Newtonsoft.Json; |
| | 10 | | using Serialization; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Defines the data type of a field in a search index. |
| | 14 | | /// </summary> |
| | 15 | | [JsonConverter(typeof(ExtensibleEnumConverter<DataType>))] |
| | 16 | | public struct DataType : IEquatable<DataType> |
| | 17 | | { |
| | 18 | | private readonly string _value; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Indicates that a field contains a string. |
| | 22 | | /// </summary> |
| 2 | 23 | | public static readonly DataType String = new DataType(AsString.String); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Indicates that a field contains a 32-bit signed integer. |
| | 27 | | /// </summary> |
| 2 | 28 | | public static readonly DataType Int32 = new DataType(AsString.Int32); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Indicates that a field contains a 64-bit signed integer. |
| | 32 | | /// </summary> |
| 2 | 33 | | public static readonly DataType Int64 = new DataType(AsString.Int64); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Indicates that a field contains an IEEE double-precision floating point number. |
| | 37 | | /// </summary> |
| 2 | 38 | | public static readonly DataType Double = new DataType(AsString.Double); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Indicates that a field contains a Boolean value (true or false). |
| | 42 | | /// </summary> |
| 2 | 43 | | public static readonly DataType Boolean = new DataType(AsString.Boolean); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Indicates that a field contains a date/time value, including timezone information. |
| | 47 | | /// </summary> |
| 2 | 48 | | public static readonly DataType DateTimeOffset = new DataType(AsString.DateTimeOffset); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Indicates that a field contains a geo-location in terms of longitude and latitude. |
| | 52 | | /// </summary> |
| 2 | 53 | | public static readonly DataType GeographyPoint = new DataType(AsString.GeographyPoint); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Indicates that a field contains one or more complex objects that in turn have sub-fields of other types. |
| | 57 | | /// </summary> |
| 2 | 58 | | public static readonly DataType Complex = new DataType(AsString.Complex); |
| | 59 | |
|
| | 60 | | private DataType(string typeName) |
| | 61 | | { |
| 131038 | 62 | | Throw.IfArgumentNull(typeName, nameof(typeName)); |
| 131038 | 63 | | _value = typeName; |
| 131038 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Creates a DataType for a collection of the given type. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="elementType">The DataType of the elements of the collection.</param> |
| | 70 | | /// <returns>A new DataType for a collection.</returns> |
| 116634 | 71 | | public static DataType Collection(DataType elementType) => new DataType($"Collection({elementType})"); |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Defines implicit conversion from string to DataType. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="value">string to convert.</param> |
| | 77 | | /// <returns>The string as a DataType.</returns> |
| 204 | 78 | | public static implicit operator DataType(string value) => new DataType(value); |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Defines explicit conversion from DataType to string. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="dataType">DataType to convert.</param> |
| | 84 | | /// <returns>The DataType as a string.</returns> |
| 0 | 85 | | public static explicit operator string(DataType dataType) => dataType.ToString(); |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Compares two DataType values for equality. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="lhs">The first DataType to compare.</param> |
| | 91 | | /// <param name="rhs">The second DataType to compare.</param> |
| | 92 | | /// <returns>true if the DataType objects are equal or are both null; false otherwise.</returns> |
| 166604 | 93 | | public static bool operator ==(DataType lhs, DataType rhs) => Equals(lhs, rhs); |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Compares two DataType values for inequality. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="lhs">The first DataType to compare.</param> |
| | 99 | | /// <param name="rhs">The second DataType to compare.</param> |
| | 100 | | /// <returns>true if the DataType objects are not equal; false otherwise.</returns> |
| 0 | 101 | | public static bool operator !=(DataType lhs, DataType rhs) => !Equals(lhs, rhs); |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Compares the DataType for equality with another DataType. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="other">The DataType with which to compare.</param> |
| | 107 | | /// <returns><c>true</c> if the DataType objects are equal; otherwise, <c>false</c>.</returns> |
| 167692 | 108 | | public bool Equals(DataType other) => _value == other._value; |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Determines whether the specified object is equal to the current object. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="obj">The object to compare with the current object.</param> |
| | 114 | | /// <returns><c>true</c> if the specified object is equal to the current object; otherwise, <c>false</c>.</retur |
| 166604 | 115 | | public override bool Equals(object obj) => obj is DataType ? Equals((DataType)obj) : false; |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Serves as the default hash function. |
| | 119 | | /// </summary> |
| | 120 | | /// <returns>A hash code for the current object.</returns> |
| 0 | 121 | | public override int GetHashCode() => _value.GetHashCode(); |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Returns a string representation of the DataType. |
| | 125 | | /// </summary> |
| | 126 | | /// <returns>The DataType as a string.</returns> |
| 131212 | 127 | | public override string ToString() => _value; |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// The names of all of the data types as plain strings. |
| | 131 | | /// </summary> |
| | 132 | | public static class AsString |
| | 133 | | { |
| | 134 | | /// <summary> |
| | 135 | | /// Indicates that a field contains a string. |
| | 136 | | /// </summary> |
| | 137 | | public const string String = "Edm.String"; |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Indicates that a field contains a 32-bit signed integer. |
| | 141 | | /// </summary> |
| | 142 | | public const string Int32 = "Edm.Int32"; |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Indicates that a field contains a 64-bit signed integer. |
| | 146 | | /// </summary> |
| | 147 | | public const string Int64 = "Edm.Int64"; |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Indicates that a field contains an IEEE double-precision floating point number. |
| | 151 | | /// </summary> |
| | 152 | | public const string Double = "Edm.Double"; |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Indicates that a field contains a Boolean value (true or false). |
| | 156 | | /// </summary> |
| | 157 | | public const string Boolean = "Edm.Boolean"; |
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// Indicates that a field contains a date/time value, including timezone information. |
| | 161 | | /// </summary> |
| | 162 | | public const string DateTimeOffset = "Edm.DateTimeOffset"; |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// Indicates that a field contains a geo-location in terms of longitude and latitude. |
| | 166 | | /// </summary> |
| | 167 | | public const string GeographyPoint = "Edm.GeographyPoint"; |
| | 168 | |
|
| | 169 | | /// <summary> |
| | 170 | | /// Indicates that a field contains one or more complex objects that in turn have sub-fields of other types. |
| | 171 | | /// </summary> |
| | 172 | | public const string Complex = "Edm.ComplexType"; |
| | 173 | | } |
| | 174 | | } |
| | 175 | | } |