diff --git a/src/ES.Kubernetes.Reflector/Core/Resources/KubeRef.cs b/src/ES.Kubernetes.Reflector/Core/Resources/KubeRef.cs index 2ee5836..3913b83 100644 --- a/src/ES.Kubernetes.Reflector/Core/Resources/KubeRef.cs +++ b/src/ES.Kubernetes.Reflector/Core/Resources/KubeRef.cs @@ -8,73 +8,74 @@ public sealed record KubeRef public KubeRef(string ns, string name) { - Name = name; - Namespace = string.IsNullOrWhiteSpace(ns) ? string.Empty : ns; + if (string.IsNullOrWhiteSpace(ns)) + { + ns = string.Empty; + } + + Name = name ?? throw new ArgumentNullException(nameof(name), "Name cannot be null."); + Namespace = ns; } public KubeRef(string value) { - if (string.IsNullOrWhiteSpace(value)) return; - var split = value.Split(new[] {"/"}, StringSplitOptions.RemoveEmptyEntries).ToList(); - if (!split.Any()) return; - switch (split.Count) + if (string.IsNullOrWhiteSpace(value)) { - case > 2: - throw new ArgumentException("Invalid value", nameof(value)); - case 1: - Name = split.Single(); - break; - default: - Namespace = split.First(); - Name = split.Last(); - break; + throw new ArgumentException("Value cannot be null or whitespace.", nameof(value)); } + + var (namespacePart, namePart) = value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) + .Select(part => part.Trim()) + .ToTuple(); + + Namespace = namespacePart ?? string.Empty; + Name = namePart; } public KubeRef(V1ObjectMeta metadata) : this(metadata.Namespace() ?? string.Empty, metadata.Name) { } - public string Namespace { get; } = string.Empty; - public string Name { get; } = string.Empty; - + public string Namespace { get; } + public string Name { get; } public static bool TryParse(string value, out KubeRef id) { id = Empty; - if (string.IsNullOrWhiteSpace(value)) return false; - - var split = value.Trim().Split(new[] {"/"}, StringSplitOptions.RemoveEmptyEntries).ToList(); - if (!split.Any()) return false; - if (split.Count > 2) return false; - id = split.Count == 1 - ? new KubeRef(string.Empty, split.Single().Trim()) - : new KubeRef(split.First().Trim(), split.Last().Trim()); + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } - return true; - } + var (namespacePart, namePart) = value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) + .Select(part => part.Trim()) + .ToTuple(); + if (string.IsNullOrEmpty(namespacePart) && !string.IsNullOrEmpty(namePart)) + { + id = new KubeRef(string.Empty, namePart); + } + else if (!string.IsNullOrEmpty(namespacePart) && !string.IsNullOrEmpty(namePart)) + { + id = new KubeRef(namespacePart, namePart); + } - public bool Equals(KubeRef? other) - { - if (ReferenceEquals(this, other)) return true; - return string.Equals(Namespace, other?.Namespace) && string.Equals(Name, other?.Name); + return true; } + public override bool Equals(object? obj) => + this == obj || + (obj is KubeRef other && + string.Equals(Namespace, other.Namespace) && + string.Equals(Name, other.Name)); + public override int GetHashCode() => + (Namespace?.GetHashCode() ?? 0, Name?.GetHashCode() ?? 0); - - public override int GetHashCode() - { - unchecked + public override string ToString() => + (Namespace, Name) switch { - return ((!string.IsNullOrEmpty(Namespace) ? Namespace.GetHashCode() : 0) * 397) ^ - (string.IsNullOrEmpty(Name) ? Name.GetHashCode() : 0); - } - } - - public override string ToString() - { - return $"{(string.IsNullOrEmpty(Namespace) ? string.Empty : $"{Namespace}/")}{Name}"; - } -} \ No newline at end of file + ("", var name) => name, + (var ns, var name) => $"{ns}/{name}" + }; +}