| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a variable in GraphQL query. |
| | | 7 | | /// </summary> |
| | | 8 | | public readonly struct Variable : IComparable, IComparable<Variable>, IEquatable<Variable> |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Name of the variable. |
| | | 12 | | /// </summary> |
| | 1815 | 13 | | public string Name { get; } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Type of the variable. |
| | | 17 | | /// </summary> |
| | 720 | 18 | | public string Type { get; } |
| | | 19 | | |
| | | 20 | | public Variable(string name, string type) |
| | | 21 | | { |
| | 429 | 22 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 23 | | { |
| | 15 | 24 | | throw new ArgumentException("Variable name cannot be null or whitespace.", nameof(name)); |
| | | 25 | | } |
| | | 26 | | |
| | 414 | 27 | | if (!name.StartsWith('$')) |
| | | 28 | | { |
| | 6 | 29 | | throw new ArgumentException("Variable name must start with '$'.", nameof(name)); |
| | | 30 | | } |
| | | 31 | | |
| | 408 | 32 | | if (string.IsNullOrWhiteSpace(type)) |
| | | 33 | | { |
| | 15 | 34 | | throw new ArgumentException("Variable type cannot be null or whitespace.", nameof(type)); |
| | | 35 | | } |
| | | 36 | | |
| | 393 | 37 | | Name = name; |
| | 393 | 38 | | Type = type; |
| | 393 | 39 | | } |
| | | 40 | | |
| | 9 | 41 | | public override string ToString() => $"{Name}:{Type}"; |
| | | 42 | | |
| | | 43 | | public int CompareTo(object? obj) |
| | | 44 | | { |
| | 114 | 45 | | return obj switch |
| | 114 | 46 | | { |
| | 3 | 47 | | null => 1, |
| | 105 | 48 | | Variable variable => CompareTo(variable), |
| | 6 | 49 | | _ => throw new ArgumentException($"Object must be of type {nameof(Variable)}") |
| | 114 | 50 | | }; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public int CompareTo(Variable other) |
| | | 54 | | { |
| | 486 | 55 | | var nameComparison = string.Compare(Name, other.Name, StringComparison.OrdinalIgnoreCase); |
| | 486 | 56 | | return nameComparison != 0 ? nameComparison : string.Compare(Type, other.Type, StringComparison.OrdinalIgnoreCas |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | internal void Print(StringBuilder builder, string key, bool isRootElement) |
| | | 60 | | { |
| | 198 | 61 | | builder.Append(isRootElement ? Name : key); |
| | 198 | 62 | | builder.Append(':'); |
| | 198 | 63 | | builder.Append(isRootElement ? Type : Name); |
| | 198 | 64 | | } |
| | | 65 | | |
| | 21 | 66 | | public static bool operator ==(Variable left, Variable right) => left.Equals(right); |
| | 27 | 67 | | public static bool operator !=(Variable left, Variable right) => !left.Equals(right); |
| | | 68 | | |
| | 15 | 69 | | public static bool operator <(Variable left, Variable right) => left.CompareTo(right) < 0; |
| | 12 | 70 | | public static bool operator >(Variable left, Variable right) => left.CompareTo(right) > 0; |
| | | 71 | | |
| | 12 | 72 | | public static bool operator <=(Variable left, Variable right) => left.CompareTo(right) <= 0; |
| | 12 | 73 | | public static bool operator >=(Variable left, Variable right) => left.CompareTo(right) >= 0; |
| | | 74 | | |
| | 105 | 75 | | public override bool Equals(object? obj) => CompareTo(obj) == 0; |
| | | 76 | | |
| | 69 | 77 | | public bool Equals(Variable other) => CompareTo(other) == 0; |
| | | 78 | | |
| | | 79 | | // Constructor rejects null/whitespace name/type, so neither field is null here. |
| | 12 | 80 | | public override int GetHashCode() => HashCode.Combine( |
| | 12 | 81 | | Name.ToUpperInvariant(), |
| | 12 | 82 | | Type.ToUpperInvariant()); |
| | | 83 | | } |