Skip to content

Commit

Permalink
add static FromString() method and update the string conversion opera…
Browse files Browse the repository at this point in the history
…tor to call FromString() method

Signed-off-by: Sky Ao <[email protected]>
  • Loading branch information
skyao committed Jul 18, 2024
1 parent 87b15e1 commit 6ecfde1
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Abstractions/TaskName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public TaskName(string name, string version)
/// Implicitly converts a <see cref="string"/> into a <see cref="TaskName"/> value.
/// </summary>
/// <param name="value">The string to convert into a <see cref="TaskName"/>.</param>
public static implicit operator TaskName(string value) => string.IsNullOrEmpty(value) ? default : new(value);
public static implicit operator TaskName(string value) => FromString(value);

/// <summary>
/// Compares two <see cref="TaskName"/> objects for equality.
Expand All @@ -114,6 +114,32 @@ public TaskName(string name, string version)
return !a.Equals(b);
}

/// <summary>
/// Parses the taskname string and initializes a new instance of the <see cref="TaskName"/> struct.
/// </summary>
/// <param name="taskname">The taskname string in format of "Name:Version" or "Name".</param>
/// <returns>New <see cref="TaskName"/> instance parsed from taskname string.</returns>
public static TaskName FromString(string taskname)
{
if (string.IsNullOrEmpty(taskname))
{
return default;
}

string[] parts = taskname.Split(':');
if (parts.Length == 1)
{
return new TaskName(parts[0]);
}

if (parts.Length == 2)
{
return new TaskName(parts[0], parts[1]);
}

throw new ArgumentException("Invalid task name format: taskname=" + taskname);
}

/// <summary>
/// Gets a value indicating whether to <see cref="TaskName"/> objects
/// are equal using value semantics.
Expand Down

0 comments on commit 6ecfde1

Please sign in to comment.