Skip to content

Commit 8e5f686

Browse files
committed
feat(duration): enhance DurationValue structure and operators
- Refactored the DurationValue struct to improve readability and maintainability. - Moved the property declarations for Value and Unit above their documentation comments for better structure. - Added operator overloads for comparison to simplify comparisons between DurationValue instances. - Ensured documentation formatting is consistent throughout the struct.
1 parent 84d85c4 commit 8e5f686

5 files changed

Lines changed: 114 additions & 49 deletions

File tree

src/ES.FX/Net/BasicHttpProxyOptions.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,38 @@
33
namespace ES.FX.Net;
44

55
/// <summary>
6-
/// Represents configuration options for an HTTP proxy,
7-
/// including address, bypass rules, credential usage, and authentication details.
6+
/// Represents configuration options for an HTTP proxy,
7+
/// including address, bypass rules, credential usage, and authentication details.
88
/// </summary>
99
[PublicAPI]
1010
public class BasicHttpProxyOptions
1111
{
1212
/// <summary>
13-
/// Gets or sets the URI address of the proxy server.
14-
/// Example: <c>http://proxy.example.com:8080</c>.
13+
/// Gets or sets the URI address of the proxy server.
14+
/// Example: <c>http://proxy.example.com:8080</c>.
1515
/// </summary>
1616
public string? Address { get; set; }
1717

1818
/// <summary>
19-
/// Gets or sets a value indicating whether to bypass the proxy server for local addresses.
19+
/// Gets or sets a value indicating whether to bypass the proxy server for local addresses.
2020
/// </summary>
2121
public bool BypassProxyOnLocal { get; set; }
2222

2323
/// <summary>
24-
/// Gets or sets the list of addresses that should bypass the proxy server.
25-
/// Wildcard patterns can be used.
24+
/// Gets or sets the list of addresses that should bypass the proxy server.
25+
/// Wildcard patterns can be used.
2626
/// </summary>
2727
public string[]? BypassList { get; set; }
2828

2929
/// <summary>
30-
/// Gets or sets a value indicating whether the default system credentials should be used for authentication with the proxy server.
30+
/// Gets or sets a value indicating whether the default system credentials should be used for authentication with the
31+
/// proxy server.
3132
/// </summary>
3233
public bool UseDefaultCredentials { get; set; }
3334

3435
/// <summary>
35-
/// Gets or sets the network credentials to use for authenticating with the proxy server.
36-
/// If <see cref="UseDefaultCredentials"/> is <c>false</c> and credentials are provided, these will be used.
36+
/// Gets or sets the network credentials to use for authenticating with the proxy server.
37+
/// If <see cref="UseDefaultCredentials" /> is <c>false</c> and credentials are provided, these will be used.
3738
/// </summary>
3839
public NetworkCredentialOptions? Credentials { get; set; }
3940
}

src/ES.FX/Net/Extensions/WebProxyExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace ES.FX.Net.Extensions;
77
public static class WebProxyExtensions
88
{
99
/// <summary>
10-
/// Builds an <see cref="IWebProxy"/> instance from the specified <see cref="BasicHttpProxyOptions"/>.
10+
/// Builds an <see cref="IWebProxy" /> instance from the specified <see cref="BasicHttpProxyOptions" />.
1111
/// </summary>
1212
/// <param name="options">The proxy options to use for building the proxy.</param>
13-
/// <returns>A configured <see cref="IWebProxy"/> instance, or <c>null</c> if no address is specified.</returns>
13+
/// <returns>A configured <see cref="IWebProxy" /> instance, or <c>null</c> if no address is specified.</returns>
1414
public static IWebProxy? BuildBasicHttpProxy(this BasicHttpProxyOptions? options)
1515
{
1616
if (options == null || string.IsNullOrWhiteSpace(options.Address))
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
using JetBrains.Annotations;
22

3-
namespace ES.FX.Net
3+
namespace ES.FX.Net;
4+
5+
/// <summary>
6+
/// Represents configuration options for network credentials,
7+
/// including username, password, and optional domain information.
8+
/// </summary>
9+
[PublicAPI]
10+
public class NetworkCredentialOptions
411
{
512
/// <summary>
6-
/// Represents configuration options for network credentials,
7-
/// including username, password, and optional domain information.
13+
/// Gets or sets the username associated with the credentials.
814
/// </summary>
9-
[PublicAPI]
10-
public class NetworkCredentialOptions
11-
{
12-
/// <summary>
13-
/// Gets or sets the username associated with the credentials.
14-
/// </summary>
15-
public string? UserName { get; set; }
15+
public string? UserName { get; set; }
1616

17-
/// <summary>
18-
/// Gets or sets the password associated with the credentials.
19-
/// </summary>
20-
public string? Password { get; set; }
17+
/// <summary>
18+
/// Gets or sets the password associated with the credentials.
19+
/// </summary>
20+
public string? Password { get; set; }
2121

22-
/// <summary>
23-
/// Gets or sets the domain associated with the credentials.
24-
/// Used primarily for NTLM or Kerberos authentication.
25-
/// </summary>
26-
public string? Domain { get; set; }
27-
}
22+
/// <summary>
23+
/// Gets or sets the domain associated with the credentials.
24+
/// Used primarily for NTLM or Kerberos authentication.
25+
/// </summary>
26+
public string? Domain { get; set; }
2827
}

src/ES.FX/Primitives/DurationValue.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@
33
namespace ES.FX.Primitives;
44

55
/// <summary>
6-
/// Represents a quantity of time, in a specified unit.
6+
/// Represents a quantity of time, in a specified unit.
77
/// </summary>
88
[PublicAPI]
99
public readonly record struct DurationValue : IComparable<DurationValue>
1010
{
1111
/// <summary>
12-
/// The number of units.
13-
/// </summary>
14-
public long Value { get; init; }
15-
16-
/// <summary>
17-
/// The unit of time for this duration.
18-
/// </summary>
19-
public DurationUnit Unit { get; init; }
20-
21-
/// <summary>
22-
/// Initializes a new <see cref="DurationValue"/> with the specified value and unit,
23-
/// enforcing non-negative values.
12+
/// Initializes a new <see cref="DurationValue" /> with the specified value and unit,
13+
/// enforcing non-negative values.
2414
/// </summary>
2515
/// <param name="value">The number of units. Must be non-negative.</param>
2616
/// <param name="unit">The duration unit.</param>
@@ -34,13 +24,18 @@ public DurationValue(long value, DurationUnit unit)
3424
}
3525

3626
/// <summary>
37-
/// Returns a string such as "7 Days".
27+
/// The number of units.
3828
/// </summary>
39-
public override string ToString() => $"{Value} {Unit}{(Value == 1 ? string.Empty : "s")}";
29+
public long Value { get; init; }
30+
31+
/// <summary>
32+
/// The unit of time for this duration.
33+
/// </summary>
34+
public DurationUnit Unit { get; init; }
4035

4136
/// <summary>
42-
/// Compares two durations of the same unit.
43-
/// Comparing different units is not supported.
37+
/// Compares two durations of the same unit.
38+
/// Comparing different units is not supported.
4439
/// </summary>
4540
public int CompareTo(DurationValue other)
4641
{
@@ -49,4 +44,21 @@ public int CompareTo(DurationValue other)
4944

5045
return Value.CompareTo(other.Value);
5146
}
47+
48+
/// <summary>
49+
/// Returns a string such as "7 Days".
50+
/// </summary>
51+
public override string ToString() => $"{Value} {Unit}{(Value == 1 ? string.Empty : "s")}";
52+
53+
public static bool operator >(DurationValue left, DurationValue right) =>
54+
left.CompareTo(right) > 0;
55+
56+
public static bool operator <(DurationValue left, DurationValue right) =>
57+
left.CompareTo(right) < 0;
58+
59+
public static bool operator >=(DurationValue left, DurationValue right) =>
60+
left.CompareTo(right) >= 0;
61+
62+
public static bool operator <=(DurationValue left, DurationValue right) =>
63+
left.CompareTo(right) <= 0;
5264
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using ES.FX.Primitives;
2+
3+
namespace ES.FX.Tests.Primitives;
4+
5+
public class DurationValueTests
6+
{
7+
8+
[Fact]
9+
public void DurationValue_CanBe_Equal()
10+
{
11+
var a = new DurationValue(1, DurationUnit.Day);
12+
var b = new DurationValue(1, DurationUnit.Day);
13+
14+
Assert.Equal(a, b);
15+
Assert.True(a.Equals(b));
16+
}
17+
18+
[Fact]
19+
public void DurationValue_CanBe_Different()
20+
{
21+
var a = new DurationValue(1, DurationUnit.Day);
22+
var b = new DurationValue(1, DurationUnit.Month);
23+
24+
Assert.NotEqual(a, b);
25+
}
26+
27+
28+
[Fact]
29+
public void DurationValue_CanBe_Compared_SameUnit()
30+
{
31+
var a = new DurationValue(1, DurationUnit.Day);
32+
var b = new DurationValue(2, DurationUnit.Day);
33+
34+
Assert.True(b > a);
35+
}
36+
37+
38+
[Fact]
39+
public void DurationValue_CannotBe_Compared_DifferentUnit()
40+
{
41+
var a = new DurationValue(1, DurationUnit.Day);
42+
var b = new DurationValue(2, DurationUnit.Month);
43+
44+
try
45+
{
46+
Assert.True(b > a);
47+
}
48+
catch (Exception e)
49+
{
50+
Assert.True(e is InvalidOperationException);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)