|
| 1 | +using System.Linq; |
| 2 | + |
| 3 | +#nullable enable |
| 4 | + |
| 5 | +namespace Mistral |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// |
| 9 | + /// </summary> |
| 10 | + public readonly partial struct AnyOf<T1, T2, T3> : global::System.IEquatable<AnyOf<T1, T2, T3>> |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// |
| 14 | + /// </summary> |
| 15 | +#if NET6_0_OR_GREATER |
| 16 | + public T1? Value1 { get; init; } |
| 17 | +#else |
| 18 | + public T1? Value1 { get; } |
| 19 | +#endif |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// |
| 23 | + /// </summary> |
| 24 | +#if NET6_0_OR_GREATER |
| 25 | + [global::System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(Value1))] |
| 26 | +#endif |
| 27 | + public bool IsValue1 => Value1 != null; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// |
| 31 | + /// </summary> |
| 32 | + public static implicit operator AnyOf<T1, T2, T3>(T1 value) => new AnyOf<T1, T2, T3>(value); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// |
| 36 | + /// </summary> |
| 37 | + public static implicit operator T1?(AnyOf<T1, T2, T3> @this) => @this.Value1; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// |
| 41 | + /// </summary> |
| 42 | + public AnyOf(T1? value) |
| 43 | + { |
| 44 | + Value1 = value; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// |
| 49 | + /// </summary> |
| 50 | +#if NET6_0_OR_GREATER |
| 51 | + public T2? Value2 { get; init; } |
| 52 | +#else |
| 53 | + public T2? Value2 { get; } |
| 54 | +#endif |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// |
| 58 | + /// </summary> |
| 59 | +#if NET6_0_OR_GREATER |
| 60 | + [global::System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(Value2))] |
| 61 | +#endif |
| 62 | + public bool IsValue2 => Value2 != null; |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// |
| 66 | + /// </summary> |
| 67 | + public static implicit operator AnyOf<T1, T2, T3>(T2 value) => new AnyOf<T1, T2, T3>(value); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// |
| 71 | + /// </summary> |
| 72 | + public static implicit operator T2?(AnyOf<T1, T2, T3> @this) => @this.Value2; |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// |
| 76 | + /// </summary> |
| 77 | + public AnyOf(T2? value) |
| 78 | + { |
| 79 | + Value2 = value; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// |
| 84 | + /// </summary> |
| 85 | +#if NET6_0_OR_GREATER |
| 86 | + public T3? Value3 { get; init; } |
| 87 | +#else |
| 88 | + public T3? Value3 { get; } |
| 89 | +#endif |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// |
| 93 | + /// </summary> |
| 94 | +#if NET6_0_OR_GREATER |
| 95 | + [global::System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(Value3))] |
| 96 | +#endif |
| 97 | + public bool IsValue3 => Value3 != null; |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// |
| 101 | + /// </summary> |
| 102 | + public static implicit operator AnyOf<T1, T2, T3>(T3 value) => new AnyOf<T1, T2, T3>(value); |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// |
| 106 | + /// </summary> |
| 107 | + public static implicit operator T3?(AnyOf<T1, T2, T3> @this) => @this.Value3; |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// |
| 111 | + /// </summary> |
| 112 | + public AnyOf(T3? value) |
| 113 | + { |
| 114 | + Value3 = value; |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// |
| 119 | + /// </summary> |
| 120 | + public AnyOf( |
| 121 | + T1? value1, |
| 122 | + T2? value2, |
| 123 | + T3? value3 |
| 124 | + ) |
| 125 | + { |
| 126 | + Value1 = value1; |
| 127 | + Value2 = value2; |
| 128 | + Value3 = value3; |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// |
| 133 | + /// </summary> |
| 134 | + public object? Object => |
| 135 | + Value3 as object ?? |
| 136 | + Value2 as object ?? |
| 137 | + Value1 as object |
| 138 | + ; |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// |
| 142 | + /// </summary> |
| 143 | + public bool Validate() |
| 144 | + { |
| 145 | + return IsValue1 || IsValue2 || IsValue3; |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// |
| 150 | + /// </summary> |
| 151 | + public TResult? Match<TResult>( |
| 152 | + global::System.Func<T1, TResult>? value1 = null, |
| 153 | + global::System.Func<T2, TResult>? value2 = null, |
| 154 | + global::System.Func<T3, TResult>? value3 = null, |
| 155 | + bool validate = true) |
| 156 | + { |
| 157 | + if (validate) |
| 158 | + { |
| 159 | + Validate(); |
| 160 | + } |
| 161 | + |
| 162 | + if (IsValue1 && value1 != null) |
| 163 | + { |
| 164 | + return value1(Value1!); |
| 165 | + } |
| 166 | + else if (IsValue2 && value2 != null) |
| 167 | + { |
| 168 | + return value2(Value2!); |
| 169 | + } |
| 170 | + else if (IsValue3 && value3 != null) |
| 171 | + { |
| 172 | + return value3(Value3!); |
| 173 | + } |
| 174 | + |
| 175 | + return default(TResult); |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// |
| 180 | + /// </summary> |
| 181 | + public void Match( |
| 182 | + global::System.Action<T1>? value1 = null, |
| 183 | + global::System.Action<T2>? value2 = null, |
| 184 | + global::System.Action<T3>? value3 = null, |
| 185 | + bool validate = true) |
| 186 | + { |
| 187 | + if (validate) |
| 188 | + { |
| 189 | + Validate(); |
| 190 | + } |
| 191 | + |
| 192 | + if (IsValue1) |
| 193 | + { |
| 194 | + value1?.Invoke(Value1!); |
| 195 | + } |
| 196 | + else if (IsValue2) |
| 197 | + { |
| 198 | + value2?.Invoke(Value2!); |
| 199 | + } |
| 200 | + else if (IsValue3) |
| 201 | + { |
| 202 | + value3?.Invoke(Value3!); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /// <summary> |
| 207 | + /// |
| 208 | + /// </summary> |
| 209 | + public override int GetHashCode() |
| 210 | + { |
| 211 | + var fields = new object?[] |
| 212 | + { |
| 213 | + Value1, |
| 214 | + typeof(T1), |
| 215 | + Value2, |
| 216 | + typeof(T2), |
| 217 | + Value3, |
| 218 | + typeof(T3), |
| 219 | + }; |
| 220 | + const int offset = unchecked((int)2166136261); |
| 221 | + const int prime = 16777619; |
| 222 | + static int HashCodeAggregator(int hashCode, object? value) => value == null |
| 223 | + ? (hashCode ^ 0) * prime |
| 224 | + : (hashCode ^ value.GetHashCode()) * prime; |
| 225 | + return fields.Aggregate(offset, HashCodeAggregator); |
| 226 | + } |
| 227 | + |
| 228 | + /// <summary> |
| 229 | + /// |
| 230 | + /// </summary> |
| 231 | + public bool Equals(AnyOf<T1, T2, T3> other) |
| 232 | + { |
| 233 | + return |
| 234 | + global::System.Collections.Generic.EqualityComparer<T1?>.Default.Equals(Value1, other.Value1) && |
| 235 | + global::System.Collections.Generic.EqualityComparer<T2?>.Default.Equals(Value2, other.Value2) && |
| 236 | + global::System.Collections.Generic.EqualityComparer<T3?>.Default.Equals(Value3, other.Value3) |
| 237 | + ; |
| 238 | + } |
| 239 | + |
| 240 | + /// <summary> |
| 241 | + /// |
| 242 | + /// </summary> |
| 243 | + public static bool operator ==(AnyOf<T1, T2, T3> obj1, AnyOf<T1, T2, T3> obj2) |
| 244 | + { |
| 245 | + return global::System.Collections.Generic.EqualityComparer<AnyOf<T1, T2, T3>>.Default.Equals(obj1, obj2); |
| 246 | + } |
| 247 | + |
| 248 | + /// <summary> |
| 249 | + /// |
| 250 | + /// </summary> |
| 251 | + public static bool operator !=(AnyOf<T1, T2, T3> obj1, AnyOf<T1, T2, T3> obj2) |
| 252 | + { |
| 253 | + return !(obj1 == obj2); |
| 254 | + } |
| 255 | + |
| 256 | + /// <summary> |
| 257 | + /// |
| 258 | + /// </summary> |
| 259 | + public override bool Equals(object? obj) |
| 260 | + { |
| 261 | + return obj is AnyOf<T1, T2, T3> o && Equals(o); |
| 262 | + } |
| 263 | + |
| 264 | + |
| 265 | + /// <summary> |
| 266 | + /// Serializes the current instance to a JSON string using the provided JsonSerializerContext. |
| 267 | + /// </summary> |
| 268 | + public string ToJson( |
| 269 | + global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext) |
| 270 | + { |
| 271 | + return global::System.Text.Json.JsonSerializer.Serialize( |
| 272 | + this, |
| 273 | + this.GetType(), |
| 274 | + jsonSerializerContext); |
| 275 | + } |
| 276 | + |
| 277 | + /// <summary> |
| 278 | + /// Serializes the current instance to a JSON string using the provided JsonSerializerOptions. |
| 279 | + /// </summary> |
| 280 | +#if NET8_0_OR_GREATER |
| 281 | + [global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] |
| 282 | + [global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] |
| 283 | +#endif |
| 284 | + public string ToJson( |
| 285 | + global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null) |
| 286 | + { |
| 287 | + return global::System.Text.Json.JsonSerializer.Serialize( |
| 288 | + this, |
| 289 | + jsonSerializerOptions); |
| 290 | + } |
| 291 | + |
| 292 | + /// <summary> |
| 293 | + /// Deserializes a JSON string using the provided JsonSerializerContext. |
| 294 | + /// </summary> |
| 295 | + public static global::Mistral.AnyOf<T1, T2, T3>? FromJson( |
| 296 | + string json, |
| 297 | + global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext) |
| 298 | + { |
| 299 | + return global::System.Text.Json.JsonSerializer.Deserialize( |
| 300 | + json, |
| 301 | + typeof(global::Mistral.AnyOf<T1, T2, T3>), |
| 302 | + jsonSerializerContext) as global::Mistral.AnyOf<T1, T2, T3>?; |
| 303 | + } |
| 304 | + |
| 305 | + /// <summary> |
| 306 | + /// Deserializes a JSON string using the provided JsonSerializerOptions. |
| 307 | + /// </summary> |
| 308 | +#if NET8_0_OR_GREATER |
| 309 | + [global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] |
| 310 | + [global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] |
| 311 | +#endif |
| 312 | + public static global::Mistral.AnyOf<T1, T2, T3>? FromJson( |
| 313 | + string json, |
| 314 | + global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null) |
| 315 | + { |
| 316 | + return global::System.Text.Json.JsonSerializer.Deserialize<global::Mistral.AnyOf<T1, T2, T3>>( |
| 317 | + json, |
| 318 | + jsonSerializerOptions); |
| 319 | + } |
| 320 | + |
| 321 | + /// <summary> |
| 322 | + /// Deserializes a JSON stream using the provided JsonSerializerContext. |
| 323 | + /// </summary> |
| 324 | + public static async global::System.Threading.Tasks.ValueTask<global::Mistral.AnyOf<T1, T2, T3>?> FromJsonStream( |
| 325 | + global::System.IO.Stream jsonStream, |
| 326 | + global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext) |
| 327 | + { |
| 328 | + return (await global::System.Text.Json.JsonSerializer.DeserializeAsync( |
| 329 | + jsonStream, |
| 330 | + typeof(global::Mistral.AnyOf<T1, T2, T3>), |
| 331 | + jsonSerializerContext).ConfigureAwait(false)) as global::Mistral.AnyOf<T1, T2, T3>?; |
| 332 | + } |
| 333 | + |
| 334 | + /// <summary> |
| 335 | + /// Deserializes a JSON stream using the provided JsonSerializerOptions. |
| 336 | + /// </summary> |
| 337 | +#if NET8_0_OR_GREATER |
| 338 | + [global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")] |
| 339 | + [global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")] |
| 340 | +#endif |
| 341 | + public static global::System.Threading.Tasks.ValueTask<global::Mistral.AnyOf<T1, T2, T3>?> FromJsonStream( |
| 342 | + global::System.IO.Stream jsonStream, |
| 343 | + global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null) |
| 344 | + { |
| 345 | + return global::System.Text.Json.JsonSerializer.DeserializeAsync<global::Mistral.AnyOf<T1, T2, T3>?>( |
| 346 | + jsonStream, |
| 347 | + jsonSerializerOptions); |
| 348 | + } |
| 349 | + |
| 350 | + } |
| 351 | +} |
0 commit comments