openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Internal/Argument.cs
126lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.Collections; |
| 7 | using System.Collections.Generic; |
| 8 | |
| 9 | namespace OpenAI |
| 10 | { |
| 11 | internal static partial class Argument |
| 12 | { |
| 13 | public static void AssertNotNull<T>(T value, string name) |
| 14 | { |
| 15 | if (value is null) |
| 16 | { |
| 17 | throw new ArgumentNullException(name); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | public static void AssertNotNull<T>(T? value, string name) |
| 22 | where T : struct |
| 23 | { |
| 24 | if (!value.HasValue) |
| 25 | { |
| 26 | throw new ArgumentNullException(name); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public static void AssertNotNullOrEmpty<T>(IEnumerable<T> value, string name) |
| 31 | { |
| 32 | if (value is null) |
| 33 | { |
| 34 | throw new ArgumentNullException(name); |
| 35 | } |
| 36 | if (value is ICollection<T> collectionOfT && collectionOfT.Count == 0) |
| 37 | { |
| 38 | throw new ArgumentException("Value cannot be an empty collection.", name); |
| 39 | } |
| 40 | if (value is ICollection collection && collection.Count == 0) |
| 41 | { |
| 42 | throw new ArgumentException("Value cannot be an empty collection.", name); |
| 43 | } |
| 44 | using IEnumerator<T> e = value.GetEnumerator(); |
| 45 | if (!e.MoveNext()) |
| 46 | { |
| 47 | throw new ArgumentException("Value cannot be an empty collection.", name); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public static void AssertNotNullOrEmpty(string value, string name) |
| 52 | { |
| 53 | if (value is null) |
| 54 | { |
| 55 | throw new ArgumentNullException(name); |
| 56 | } |
| 57 | if (value.Length == 0) |
| 58 | { |
| 59 | throw new ArgumentException("Value cannot be an empty string.", name); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static void AssertNotNullOrWhiteSpace(string value, string name) |
| 64 | { |
| 65 | if (value is null) |
| 66 | { |
| 67 | throw new ArgumentNullException(name); |
| 68 | } |
| 69 | if (string.IsNullOrWhiteSpace(value)) |
| 70 | { |
| 71 | throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public static void AssertNotDefault<T>(ref T value, string name) |
| 76 | where T : struct, IEquatable<T> |
| 77 | { |
| 78 | if (value.Equals(default)) |
| 79 | { |
| 80 | throw new ArgumentException("Value cannot be empty.", name); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public static void AssertInRange<T>(T value, T minimum, T maximum, string name) |
| 85 | where T : notnull, IComparable<T> |
| 86 | { |
| 87 | if (minimum.CompareTo(value) > 0) |
| 88 | { |
| 89 | throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed."); |
| 90 | } |
| 91 | if (maximum.CompareTo(value) < 0) |
| 92 | { |
| 93 | throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed."); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public static void AssertEnumDefined(Type enumType, object value, string name) |
| 98 | { |
| 99 | if (!Enum.IsDefined(enumType, value)) |
| 100 | { |
| 101 | throw new ArgumentException($"Value not defined for {enumType.FullName}.", name); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public static T CheckNotNull<T>(T value, string name) |
| 106 | where T : class |
| 107 | { |
| 108 | AssertNotNull(value, name); |
| 109 | return value; |
| 110 | } |
| 111 | |
| 112 | public static string CheckNotNullOrEmpty(string value, string name) |
| 113 | { |
| 114 | AssertNotNullOrEmpty(value, name); |
| 115 | return value; |
| 116 | } |
| 117 | |
| 118 | public static void AssertNull<T>(T value, string name, string message = null) |
| 119 | { |
| 120 | if (value != null) |
| 121 | { |
| 122 | throw new ArgumentException(message ?? "Value must be null.", name); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |