Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve consistency and clarity in KubeRef class #392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 47 additions & 46 deletions src/ES.Kubernetes.Reflector/Core/Resources/KubeRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}
("", var name) => name,
(var ns, var name) => $"{ns}/{name}"
};
}