|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
| 15 | +using System.Diagnostics; |
| 16 | + |
15 | 17 | namespace RestSharp;
|
16 | 18 |
|
17 | 19 | /// <summary>
|
18 | 20 | /// Parameter container for REST requests
|
19 | 21 | /// </summary>
|
20 |
| -public abstract record Parameter(string? Name, object? Value, ParameterType Type, bool Encode) { |
| 22 | +[DebuggerDisplay($"{{{nameof(DebuggerDisplay)}()}}")] |
| 23 | +public abstract record Parameter { |
| 24 | + /// <summary> |
| 25 | + /// Parameter container for REST requests |
| 26 | + /// </summary> |
| 27 | + protected Parameter(string? name, object? value, ParameterType type, bool encode) { |
| 28 | + Name = name; |
| 29 | + Value = value; |
| 30 | + Type = type; |
| 31 | + Encode = encode; |
| 32 | + } |
| 33 | + |
21 | 34 | /// <summary>
|
22 | 35 | /// MIME content type of the parameter
|
23 | 36 | /// </summary>
|
24 | 37 | public ContentType ContentType { get; protected init; } = ContentType.Undefined;
|
| 38 | + public string? Name { get; } |
| 39 | + public object? Value { get; } |
| 40 | + public ParameterType Type { get; } |
| 41 | + public bool Encode { get; } |
25 | 42 |
|
26 | 43 | /// <summary>
|
27 | 44 | /// Return a human-readable representation of this parameter
|
28 | 45 | /// </summary>
|
29 | 46 | /// <returns>String</returns>
|
30 |
| - public sealed override string ToString() => Value == null ? $"{Name}" : $"{Name}={Value}"; |
| 47 | + public sealed override string ToString() => Value == null ? $"{Name}" : $"{Name}={ValueString}"; |
| 48 | + |
| 49 | + protected virtual string ValueString => Value?.ToString() ?? "null"; |
31 | 50 |
|
32 | 51 | public static Parameter CreateParameter(string? name, object? value, ParameterType type, bool encode = true)
|
33 | 52 | // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
|
34 | 53 | => type switch {
|
35 | 54 | ParameterType.GetOrPost => new GetOrPostParameter(Ensure.NotEmptyString(name, nameof(name)), value?.ToString(), encode),
|
36 | 55 | ParameterType.UrlSegment => new UrlSegmentParameter(Ensure.NotEmptyString(name, nameof(name)), value?.ToString()!, encode),
|
37 |
| - ParameterType.HttpHeader => new HeaderParameter(name, value?.ToString()), |
| 56 | + ParameterType.HttpHeader => new HeaderParameter(name!, value?.ToString()!), |
38 | 57 | ParameterType.QueryString => new QueryParameter(Ensure.NotEmptyString(name, nameof(name)), value?.ToString(), encode),
|
39 | 58 | _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
40 | 59 | };
|
| 60 | + |
| 61 | + [PublicAPI] |
| 62 | + public void Deconstruct(out string? name, out object? value, out ParameterType type, out bool encode) { |
| 63 | + name = Name; |
| 64 | + value = Value; |
| 65 | + type = Type; |
| 66 | + encode = Encode; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Assists with debugging by displaying in the debugger output |
| 71 | + /// </summary> |
| 72 | + /// <returns></returns> |
| 73 | + [UsedImplicitly] |
| 74 | + protected string DebuggerDisplay() => $"{GetType().Name.Replace("Parameter", "")} {ToString()}"; |
41 | 75 | }
|
42 | 76 |
|
43 | 77 | public record NamedParameter : Parameter {
|
|
0 commit comments