| | | 1 | | namespace NGql.Core; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Wraps a GraphQL enum value so it renders unquoted (e.g. <c>role:ADMIN</c>) instead of as |
| | | 5 | | /// a string literal. Pass an <see cref="EnumValue"/> as an argument value to opt out of |
| | | 6 | | /// string quoting in the rendered query. |
| | | 7 | | /// </summary> |
| | | 8 | | public readonly struct EnumValue : IComparable, IComparable<EnumValue>, IEquatable<EnumValue> |
| | | 9 | | { |
| | | 10 | | /// <summary>The enum identifier as it will appear in the rendered GraphQL.</summary> |
| | 168 | 11 | | public string Value { get; } |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Creates an <see cref="EnumValue"/> from a non-empty string identifier or a CLR |
| | | 15 | | /// <see cref="System.Enum"/> instance (its <c>ToString()</c> becomes the rendered value). |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="value">A non-empty string or an <see cref="Enum"/> instance.</param> |
| | | 18 | | /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> |
| | | 19 | | /// <exception cref="ArgumentException"><paramref name="value"/> is an empty/whitespace string or an unsupported typ |
| | | 20 | | public EnumValue(object value) |
| | | 21 | | { |
| | 144 | 22 | | ArgumentNullException.ThrowIfNull(value); |
| | 138 | 23 | | Value = value switch |
| | 138 | 24 | | { |
| | 231 | 25 | | string str when !string.IsNullOrWhiteSpace(str) => str, |
| | 18 | 26 | | Enum enumValue => enumValue.ToString(), |
| | 3 | 27 | | string => throw new ArgumentException("Enum value cannot be null or whitespace.", nameof(value)), |
| | 3 | 28 | | _ => throw new ArgumentException($"Invalid enum value type: {value.GetType().Name}. Expected a non-empty str |
| | 138 | 29 | | }; |
| | 132 | 30 | | } |
| | | 31 | | |
| | 3 | 32 | | public override string ToString() => Value; |
| | | 33 | | |
| | 6 | 34 | | public static bool operator ==(EnumValue left, EnumValue right) => left.Equals(right); |
| | | 35 | | |
| | 6 | 36 | | public static bool operator !=(EnumValue left, EnumValue right) => !left.Equals(right); |
| | | 37 | | |
| | 3 | 38 | | public static bool operator <(EnumValue left, EnumValue right) => left.CompareTo(right) < 0; |
| | | 39 | | |
| | 3 | 40 | | public static bool operator >(EnumValue left, EnumValue right) => left.CompareTo(right) > 0; |
| | | 41 | | |
| | 6 | 42 | | public static bool operator <=(EnumValue left, EnumValue right) => left.CompareTo(right) <= 0; |
| | | 43 | | |
| | 6 | 44 | | public static bool operator >=(EnumValue left, EnumValue right) => left.CompareTo(right) >= 0; |
| | | 45 | | |
| | | 46 | | public int CompareTo(object? obj) |
| | | 47 | | { |
| | 24 | 48 | | return obj switch |
| | 24 | 49 | | { |
| | 9 | 50 | | null => 1, |
| | 3 | 51 | | EnumValue enumValue => CompareTo(enumValue), |
| | 12 | 52 | | _ => throw new ArgumentException($"Object must be of type {nameof(EnumValue)}") |
| | 24 | 53 | | }; |
| | | 54 | | } |
| | | 55 | | |
| | 57 | 56 | | public int CompareTo(EnumValue other) => string.Compare(Value, other.Value, StringComparison.OrdinalIgnoreCase); |
| | | 57 | | |
| | 12 | 58 | | public override bool Equals(object? obj) => CompareTo(obj) == 0; |
| | | 59 | | |
| | 27 | 60 | | public bool Equals(EnumValue other) => CompareTo(other) == 0; |
| | | 61 | | |
| | 12 | 62 | | public override int GetHashCode() => Value.GetHashCode(StringComparison.OrdinalIgnoreCase); |
| | | 63 | | } |