Skip to content

[Clone] Add Clone<T> #377

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

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/redmine-net-api/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
using System;
using System.Collections.Generic;
using System.Text;
using Redmine.Net.Api.Types;

namespace Redmine.Net.Api.Extensions
{
Expand All @@ -30,8 +31,9 @@ public static class CollectionExtensions
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listToClone">The list to clone.</param>
/// <param name="resetId"></param>
/// <returns></returns>
public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
public static IList<T> Clone<T>(this IList<T> listToClone, bool resetId) where T : ICloneable<T>
{
if (listToClone == null)
{
Expand All @@ -43,7 +45,7 @@ public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
for (var index = 0; index < listToClone.Count; index++)
{
var item = listToClone[index];
clonedList.Add((T) item.Clone());
clonedList.Add(item.Clone(resetId));
}

return clonedList;
Expand Down
14 changes: 14 additions & 0 deletions src/redmine-net-api/ICloneableOfT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Redmine.Net.Api;

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ICloneable<out T>
{
/// <summary>
///
/// </summary>
/// <returns></returns>
internal T Clone(bool resetId);
}
35 changes: 35 additions & 0 deletions src/redmine-net-api/Types/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Redmine.Net.Api.Types
[XmlRoot(RedmineKeys.ATTACHMENT)]
public sealed class Attachment :
Identifiable<Attachment>
, ICloneable<Attachment>
{
#region Properties
/// <summary>
Expand Down Expand Up @@ -266,5 +267,39 @@ public override int GetHashCode()
Author={Author},
CreatedOn={CreatedOn?.ToString("u", CultureInfo.InvariantCulture)}]";

/// <summary>
///
/// </summary>
/// <returns></returns>
public new Attachment Clone(bool resetId)
{
if (resetId)
{
return new Attachment
{
FileName = FileName,
FileSize = FileSize,
ContentType = ContentType,
Description = Description,
ContentUrl = ContentUrl,
ThumbnailUrl = ThumbnailUrl,
Author = Author?.Clone(false),
CreatedOn = CreatedOn
};
}

return new Attachment
{
Id = Id,
FileName = FileName,
FileSize = FileSize,
ContentType = ContentType,
Description = Description,
ContentUrl = ContentUrl,
ThumbnailUrl = ThumbnailUrl,
Author = Author?.Clone(true),
CreatedOn = CreatedOn
};
}
}
}
18 changes: 18 additions & 0 deletions src/redmine-net-api/Types/ChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Redmine.Net.Api.Types
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
[XmlRoot(RedmineKeys.CHANGE_SET)]
public sealed class ChangeSet : IXmlSerializable, IJsonSerializable, IEquatable<ChangeSet>
,ICloneable<ChangeSet>
{
#region Properties
/// <summary>
Expand Down Expand Up @@ -157,6 +158,23 @@ public bool Equals(ChangeSet other)
&& CommittedOn == other.CommittedOn;
}

/// <summary>
///
/// </summary>
/// <param name="resetId"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public ChangeSet Clone(bool resetId)
{
return new ChangeSet()
{
User = User,
Comments = Comments,
Revision = Revision,
CommittedOn = CommittedOn,
};
}

/// <summary>
///
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/redmine-net-api/Types/CustomFieldValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CustomFieldValue :
IXmlSerializable
,IJsonSerializable
,IEquatable<CustomFieldValue>
,ICloneable<CustomFieldValue>
{
/// <summary>
///
Expand Down Expand Up @@ -205,10 +206,9 @@ public override int GetHashCode()
///
/// </summary>
/// <returns></returns>
public object Clone()
public CustomFieldValue Clone(bool resetId)
{
var customFieldValue = new CustomFieldValue {Info = Info};
return customFieldValue;
return new CustomFieldValue { Info = Info };
}

#endregion
Expand Down
11 changes: 11 additions & 0 deletions src/redmine-net-api/Types/Detail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public sealed class Detail :
IXmlSerializable
,IJsonSerializable
,IEquatable<Detail>
,ICloneable<Detail>
{
/// <summary>
///
Expand Down Expand Up @@ -182,6 +183,16 @@ public bool Equals(Detail other)
&& string.Equals(NewValue, other.NewValue, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Detail Clone(bool resetId)
{
return new Detail(Name, Property, OldValue, NewValue);
}

/// <summary>
///
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/redmine-net-api/Types/Identifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Redmine.Net.Api.Types
/// <typeparam name="T"></typeparam>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
public abstract class Identifiable<T> : IXmlSerializable, IJsonSerializable, IEquatable<T>
, ICloneable<Identifiable<T>>
where T : Identifiable<T>
{
#region Properties
Expand Down Expand Up @@ -158,5 +159,13 @@ public override int GetHashCode()
/// <returns></returns>
private string DebuggerDisplay => $"Id={Id.ToString(CultureInfo.InvariantCulture)}";

/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual Identifiable<T> Clone(bool resetId)
{
throw new NotImplementedException();
}
}
}
14 changes: 14 additions & 0 deletions src/redmine-net-api/Types/IdentifiableName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Redmine.Net.Api.Types
/// </summary>
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
public class IdentifiableName : Identifiable<IdentifiableName>
, ICloneable<IdentifiableName>
{
/// <summary>
///
Expand Down Expand Up @@ -224,5 +225,18 @@ public override int GetHashCode()
/// </summary>
/// <returns></returns>
private string DebuggerDisplay => $"[{nameof(IdentifiableName)}: {base.ToString()}, Name={Name}]";

/// <summary>
///
/// </summary>
/// <returns></returns>
public new IdentifiableName Clone(bool resetId)
{
return new IdentifiableName
{
Id = Id,
Name = Name
};
}
}
}
49 changes: 32 additions & 17 deletions src/redmine-net-api/Types/Issue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace Redmine.Net.Api.Types
[XmlRoot(RedmineKeys.ISSUE)]
public sealed class Issue :
Identifiable<Issue>
,ICloneable<Issue>
{
#region Properties
/// <summary>
Expand Down Expand Up @@ -588,36 +589,51 @@ public override int GetHashCode()
}
#endregion

#region Implementation of IClonable
#region Implementation of IClonable<T>
/// <summary>
///
/// </summary>
/// <returns></returns>
public object Clone()
public new Issue Clone(bool resetId)
{
var issue = new Issue
{
AssignedTo = AssignedTo,
Author = Author,
Category = Category,
CustomFields = CustomFields,
Project = Project?.Clone(false),
Tracker = Tracker?.Clone(false),
Status = Status?.Clone(false),
Priority = Priority?.Clone(false),
Author = Author?.Clone(false),
Category = Category?.Clone(false),
Subject = Subject,
Description = Description,
DoneRatio = DoneRatio,
StartDate = StartDate,
DueDate = DueDate,
SpentHours = SpentHours,
DoneRatio = DoneRatio,
IsPrivate = IsPrivate,
EstimatedHours = EstimatedHours,
Priority = Priority,
StartDate = StartDate,
Status = Status,
Subject = Subject,
Tracker = Tracker,
Project = Project,
FixedVersion = FixedVersion,
TotalEstimatedHours = TotalEstimatedHours,
SpentHours = SpentHours,
TotalSpentHours = TotalSpentHours,
AssignedTo = AssignedTo?.Clone(false),
FixedVersion = FixedVersion?.Clone(false),
Notes = Notes,
Watchers = Watchers
PrivateNotes = PrivateNotes,
CreatedOn = CreatedOn,
UpdatedOn = UpdatedOn,
ClosedOn = ClosedOn,
ParentIssue = ParentIssue?.Clone(false),
CustomFields = CustomFields?.Clone(false),
Journals = Journals?.Clone(false),
Attachments = Attachments?.Clone(false),
Relations = Relations?.Clone(false),
Children = Children?.Clone(false),
Watchers = Watchers?.Clone(false),
Uploads = Uploads?.Clone(false),
};

return issue;
}

#endregion

/// <summary>
Expand Down Expand Up @@ -659,6 +675,5 @@ public IdentifiableName AsParent()
Children={Children.Dump()},
Uploads={Uploads.Dump()},
Watchers={Watchers.Dump()}]";

}
}
5 changes: 3 additions & 2 deletions src/redmine-net-api/Types/IssueChild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Redmine.Net.Api.Types
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
[XmlRoot(RedmineKeys.ISSUE)]
public sealed class IssueChild : Identifiable<IssueChild>
,ICloneable<IssueChild>
{
#region Properties
/// <summary>
Expand Down Expand Up @@ -173,12 +174,12 @@ public override int GetHashCode()
///
/// </summary>
/// <returns></returns>
public new IssueChild Clone()
public new IssueChild Clone(bool resetId)
{
return new IssueChild
{
Id = Id,
Tracker = Tracker,
Tracker = Tracker?.Clone(false),
Subject = Subject
};
}
Expand Down
30 changes: 26 additions & 4 deletions src/redmine-net-api/Types/IssueCustomField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Redmine.Net.Api.Types
public sealed class IssueCustomField :
IdentifiableName
,IEquatable<IssueCustomField>
,ICloneable<IssueCustomField>, IValue
{
#region Properties
/// <summary>
Expand Down Expand Up @@ -270,16 +271,37 @@ public override int GetHashCode()
}
#endregion

#region Implementation of IClonable
#region Implementation of IClonable<IssueCustomField>
/// <summary>
///
/// </summary>
/// <returns></returns>
public object Clone()
public new IssueCustomField Clone(bool resetId)
{
var issueCustomField = new IssueCustomField { Multiple = Multiple, Values = Values };
return issueCustomField;
IssueCustomField clone;
if (resetId)
{
clone = new IssueCustomField();
}
else
{
clone = new IssueCustomField
{
Id = Id,
};
}

clone.Name = Name;
clone.Multiple = Multiple;

if (Values != null)
{
clone.Values = new List<CustomFieldValue>(Values);
}

return clone;
}

#endregion

#region Implementation of IValue
Expand Down
Loading
Loading