Skip to content

Commit cf63106

Browse files
CopilotYunchuWang
andauthored
Add copy constructors to TaskOptions and sub-classes (#587)
* Initial plan * Add copy constructors to TaskOptions and sub-classes Co-authored-by: YunchuWang <[email protected]> * Remove redundant null check in SubOrchestrationOptions copy constructor Co-authored-by: YunchuWang <[email protected]> * Address review feedback: add null check and preserve PascalCase parameters Co-authored-by: YunchuWang <[email protected]> * Remove accidentally committed test file Co-authored-by: YunchuWang <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: YunchuWang <[email protected]>
1 parent cdf3f27 commit cf63106

File tree

2 files changed

+168
-10
lines changed

2 files changed

+168
-10
lines changed

src/Abstractions/TaskOptions.cs

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public TaskOptions(TaskRetryOptions? retry = null, IDictionary<string, string>?
3030
this.Tags = tags;
3131
}
3232

33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="TaskOptions"/> class by copying from another instance.
35+
/// </summary>
36+
/// <param name="options">The task options to copy from.</param>
37+
public TaskOptions(TaskOptions options)
38+
{
39+
Check.NotNull(options);
40+
this.Retry = options.Retry;
41+
this.Tags = options.Tags;
42+
}
43+
3344
/// <summary>
3445
/// Gets the task retry options.
3546
/// </summary>
@@ -96,12 +107,29 @@ public SubOrchestrationOptions(TaskOptions options, string? instanceId = null)
96107
: base(options)
97108
{
98109
this.InstanceId = instanceId;
99-
if (instanceId is null && options is SubOrchestrationOptions derived)
110+
if (options is SubOrchestrationOptions derived)
100111
{
101-
this.InstanceId = derived.InstanceId;
112+
if (instanceId is null)
113+
{
114+
this.InstanceId = derived.InstanceId;
115+
}
116+
117+
this.Version = derived.Version;
102118
}
103119
}
104120

121+
/// <summary>
122+
/// Initializes a new instance of the <see cref="SubOrchestrationOptions"/> class by copying from another instance.
123+
/// </summary>
124+
/// <param name="options">The sub-orchestration options to copy from.</param>
125+
public SubOrchestrationOptions(SubOrchestrationOptions options)
126+
: base(options)
127+
{
128+
Check.NotNull(options);
129+
this.InstanceId = options.InstanceId;
130+
this.Version = options.Version;
131+
}
132+
105133
/// <summary>
106134
/// Gets the orchestration instance ID.
107135
/// </summary>
@@ -116,15 +144,51 @@ public SubOrchestrationOptions(TaskOptions options, string? instanceId = null)
116144
/// <summary>
117145
/// Options for submitting new orchestrations via the client.
118146
/// </summary>
119-
/// <param name="InstanceId">
120-
/// The unique ID of the orchestration instance to schedule. If not specified, a new GUID value is used.
121-
/// </param>
122-
/// <param name="StartAt">
123-
/// The time when the orchestration instance should start executing. If not specified or if a date-time in the past
124-
/// is specified, the orchestration instance will be scheduled immediately.
125-
/// </param>
126-
public record StartOrchestrationOptions(string? InstanceId = null, DateTimeOffset? StartAt = null)
147+
public record StartOrchestrationOptions
127148
{
149+
/// <summary>
150+
/// Initializes a new instance of the <see cref="StartOrchestrationOptions"/> class.
151+
/// </summary>
152+
/// <param name="InstanceId">
153+
/// The unique ID of the orchestration instance to schedule. If not specified, a new GUID value is used.
154+
/// </param>
155+
/// <param name="StartAt">
156+
/// The time when the orchestration instance should start executing. If not specified or if a date-time in the past
157+
/// is specified, the orchestration instance will be scheduled immediately.
158+
/// </param>
159+
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter - using PascalCase to maintain backward compatibility with positional record syntax
160+
public StartOrchestrationOptions(string? InstanceId = null, DateTimeOffset? StartAt = null)
161+
#pragma warning restore SA1313
162+
{
163+
this.InstanceId = InstanceId;
164+
this.StartAt = StartAt;
165+
}
166+
167+
/// <summary>
168+
/// Initializes a new instance of the <see cref="StartOrchestrationOptions"/> class by copying from another instance.
169+
/// </summary>
170+
/// <param name="options">The start orchestration options to copy from.</param>
171+
public StartOrchestrationOptions(StartOrchestrationOptions options)
172+
{
173+
Check.NotNull(options);
174+
this.InstanceId = options.InstanceId;
175+
this.StartAt = options.StartAt;
176+
this.Tags = options.Tags;
177+
this.Version = options.Version;
178+
this.DedupeStatuses = options.DedupeStatuses;
179+
}
180+
181+
/// <summary>
182+
/// Gets the unique ID of the orchestration instance to schedule. If not specified, a new GUID value is used.
183+
/// </summary>
184+
public string? InstanceId { get; init; }
185+
186+
/// <summary>
187+
/// Gets the time when the orchestration instance should start executing. If not specified or if a date-time in the past
188+
/// is specified, the orchestration instance will be scheduled immediately.
189+
/// </summary>
190+
public DateTimeOffset? StartAt { get; init; }
191+
128192
/// <summary>
129193
/// Gets the tags to associate with the orchestration instance.
130194
/// </summary>

test/Abstractions.Tests/TaskOptionsTests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,98 @@ public void WithDedupeStatuses_ConvertsAllEnumValuesToStrings()
148148
result.DedupeStatuses.Should().Contain(status.ToString());
149149
}
150150
}
151+
152+
[Fact]
153+
public void TaskOptions_CopyConstructor_CopiesAllProperties()
154+
{
155+
// Arrange
156+
RetryPolicy policy = new(3, TimeSpan.FromSeconds(1));
157+
TaskRetryOptions retry = new(policy);
158+
Dictionary<string, string> tags = new() { { "key1", "value1" }, { "key2", "value2" } };
159+
TaskOptions original = new(retry, tags);
160+
161+
// Act
162+
TaskOptions copy = new(original);
163+
164+
// Assert
165+
copy.Retry.Should().Be(original.Retry);
166+
copy.Tags.Should().BeSameAs(original.Tags);
167+
}
168+
169+
[Fact]
170+
public void SubOrchestrationOptions_CopyConstructor_CopiesAllProperties()
171+
{
172+
// Arrange
173+
RetryPolicy policy = new(3, TimeSpan.FromSeconds(1));
174+
TaskRetryOptions retry = new(policy);
175+
Dictionary<string, string> tags = new() { { "key1", "value1" }, { "key2", "value2" } };
176+
string instanceId = Guid.NewGuid().ToString();
177+
TaskVersion version = new("1.0");
178+
SubOrchestrationOptions original = new(retry, instanceId)
179+
{
180+
Tags = tags,
181+
Version = version,
182+
};
183+
184+
// Act
185+
SubOrchestrationOptions copy = new(original);
186+
187+
// Assert
188+
copy.Retry.Should().Be(original.Retry);
189+
copy.Tags.Should().BeSameAs(original.Tags);
190+
copy.InstanceId.Should().Be(original.InstanceId);
191+
copy.Version.Should().Be(original.Version);
192+
}
193+
194+
[Fact]
195+
public void SubOrchestrationOptions_CopyFromTaskOptions_CopiesVersionWhenSourceIsSubOrchestration()
196+
{
197+
// Arrange
198+
RetryPolicy policy = new(3, TimeSpan.FromSeconds(1));
199+
TaskRetryOptions retry = new(policy);
200+
Dictionary<string, string> tags = new() { { "key1", "value1" } };
201+
string instanceId = Guid.NewGuid().ToString();
202+
TaskVersion version = new("1.0");
203+
SubOrchestrationOptions original = new(retry, instanceId)
204+
{
205+
Tags = tags,
206+
Version = version,
207+
};
208+
209+
// Act
210+
SubOrchestrationOptions copy = new(original as TaskOptions);
211+
212+
// Assert
213+
copy.Retry.Should().Be(original.Retry);
214+
copy.Tags.Should().BeSameAs(original.Tags);
215+
copy.InstanceId.Should().Be(original.InstanceId);
216+
copy.Version.Should().Be(original.Version);
217+
}
218+
219+
[Fact]
220+
public void StartOrchestrationOptions_CopyConstructor_CopiesAllProperties()
221+
{
222+
// Arrange
223+
string instanceId = Guid.NewGuid().ToString();
224+
DateTimeOffset startAt = DateTimeOffset.UtcNow.AddHours(1);
225+
Dictionary<string, string> tags = new() { { "key1", "value1" }, { "key2", "value2" } };
226+
TaskVersion version = new("1.0");
227+
List<string> dedupeStatuses = new() { "Completed", "Failed" };
228+
StartOrchestrationOptions original = new(instanceId, startAt)
229+
{
230+
Tags = tags,
231+
Version = version,
232+
DedupeStatuses = dedupeStatuses,
233+
};
234+
235+
// Act
236+
StartOrchestrationOptions copy = new(original);
237+
238+
// Assert
239+
copy.InstanceId.Should().Be(original.InstanceId);
240+
copy.StartAt.Should().Be(original.StartAt);
241+
copy.Tags.Should().BeSameAs(original.Tags);
242+
copy.Version.Should().Be(original.Version);
243+
copy.DedupeStatuses.Should().BeSameAs(original.DedupeStatuses);
244+
}
151245
}

0 commit comments

Comments
 (0)