Skip to content

Commit f1c1231

Browse files
author
Guy Fankam
committed
Optimize resource name filtering in extractor. Addresses #610.
1 parent e6feb1e commit f1c1231

35 files changed

Lines changed: 623 additions & 498 deletions

tools/code/common/Api.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Option<ApiRevisionNumber> TryFrom(string? value) =>
4343
: Option<ApiRevisionNumber>.None;
4444
}
4545

46-
public sealed record ApiName : ResourceName
46+
public sealed record ApiName : ResourceName, IResourceName<ApiName>
4747
{
4848
private const string RevisionSeparator = ";rev=";
4949

@@ -424,6 +424,12 @@ public static IAsyncEnumerable<ApiName> ListNames(this ApisUri uri, HttpPipeline
424424
return (name, dto);
425425
});
426426

427+
public static async ValueTask<Option<ApiDto>> TryGetDto(this ApiUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
428+
{
429+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
430+
return contentOption.Map(content => content.ToObjectFromJson<ApiDto>());
431+
}
432+
427433
public static async ValueTask<ApiDto> GetDto(this ApiUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
428434
{
429435
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/Backend.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace common;
1515

16-
public sealed record BackendName : ResourceName
16+
public sealed record BackendName : ResourceName, IResourceName<BackendName>
1717
{
1818
private BackendName(string value) : base(value) { }
1919

@@ -300,6 +300,12 @@ public static IAsyncEnumerable<BackendName> ListNames(this BackendsUri uri, Http
300300
return (name, dto);
301301
});
302302

303+
public static async ValueTask<Option<BackendDto>> TryGetDto(this BackendUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
304+
{
305+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
306+
return contentOption.Map(content => content.ToObjectFromJson<BackendDto>());
307+
}
308+
303309
public static async ValueTask<BackendDto> GetDto(this BackendUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
304310
{
305311
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/Common.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ protected ResourceName(string value) : base(value) { }
2525
public override int GetHashCode() => Value.GetHashCode(StringComparison.OrdinalIgnoreCase);
2626
}
2727

28+
public interface IResourceName<T>
29+
{
30+
public abstract static T From(string value);
31+
}
32+
2833
public abstract record ResourceDirectory
2934
{
3035
protected abstract DirectoryInfo Value { get; }

tools/code/common/Diagnostic.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
using System.Collections.Immutable;
77
using System.IO;
88
using System.Linq;
9-
using System.Net;
109
using System.Text.Json.Serialization;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312

1413
namespace common;
1514

16-
public sealed record DiagnosticName : ResourceName
15+
public sealed record DiagnosticName : ResourceName, IResourceName<DiagnosticName>
1716
{
1817
private DiagnosticName(string value) : base(value) { }
1918

@@ -255,6 +254,12 @@ public static IAsyncEnumerable<DiagnosticName> ListNames(this DiagnosticsUri uri
255254
return (name, dto);
256255
});
257256

257+
public static async ValueTask<Option<DiagnosticDto>> TryGetDto(this DiagnosticUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
258+
{
259+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
260+
return contentOption.Map(content => content.ToObjectFromJson<DiagnosticDto>());
261+
}
262+
258263
public static async ValueTask<DiagnosticDto> GetDto(this DiagnosticUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
259264
{
260265
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/Gateway.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace common;
1515

16-
public sealed record GatewayName : ResourceName
16+
public sealed record GatewayName : ResourceName, IResourceName<GatewayName>
1717
{
1818
private GatewayName(string value) : base(value) { }
1919

@@ -185,6 +185,12 @@ public static IAsyncEnumerable<GatewayName> ListNames(this GatewaysUri uri, Http
185185
return (name, dto);
186186
});
187187

188+
public static async ValueTask<Option<GatewayDto>> TryGetDto(this GatewayUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
189+
{
190+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
191+
return contentOption.Map(content => content.ToObjectFromJson<GatewayDto>());
192+
}
193+
188194
public static async ValueTask<GatewayDto> GetDto(this GatewayUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
189195
{
190196
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/Group.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace common;
1414

15-
public sealed record GroupName : ResourceName
15+
public sealed record GroupName : ResourceName, IResourceName<GroupName>
1616
{
1717
private GroupName(string value) : base(value) { }
1818

@@ -163,6 +163,12 @@ public static IAsyncEnumerable<GroupName> ListNames(this GroupsUri uri, HttpPipe
163163
return (name, dto);
164164
});
165165

166+
public static async ValueTask<Option<GroupDto>> TryGetDto(this GroupUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
167+
{
168+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
169+
return contentOption.Map(content => content.ToObjectFromJson<GroupDto>());
170+
}
171+
166172
public static async ValueTask<GroupDto> GetDto(this GroupUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
167173
{
168174
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/Json.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ public static Either<string, Uri> TryAsAbsoluteUri(this JsonValue? jsonValue) =>
2323
.Bind(uriString => Uri.TryCreate(uriString, UriKind.Absolute, out var uri)
2424
? Either<string, Uri>.Right(uri)
2525
: $"JSON value '{uriString}' is not a valid absolute URI.");
26+
27+
public static Either<string, int> TryAsInt(this JsonValue? jsonValue) =>
28+
jsonValue is null
29+
? "JSON value is null."
30+
: jsonValue.TryGetValue<int>(out var intValue)
31+
|| (jsonValue.TryGetValue<string>(out var stringValue) && int.TryParse(stringValue, out intValue))
32+
? intValue
33+
: "JSON value is not an integer.";
34+
35+
public static Either<string, bool> TryAsBool(this JsonValue? jsonValue) =>
36+
jsonValue is null
37+
? "JSON value is null."
38+
: jsonValue.TryGetValue<bool>(out var boolValue)
39+
|| (jsonValue.TryGetValue<string>(out var stringValue) && bool.TryParse(stringValue, out boolValue))
40+
? boolValue
41+
: "JSON value is not a boolean.";
2642
}
2743

2844
public static class JsonNodeExtensions
@@ -49,6 +65,14 @@ public static Either<string, string> TryAsString(this JsonNode? node) =>
4965
public static Either<string, Uri> TryAsAbsoluteUri(this JsonNode? node) =>
5066
node.TryAsJsonValue()
5167
.Bind(jsonValue => jsonValue.TryAsAbsoluteUri());
68+
69+
public static Either<string, int> TryAsInt(this JsonNode? node) =>
70+
node.TryAsJsonValue()
71+
.Bind(jsonValue => jsonValue.TryAsInt());
72+
73+
public static Either<string, bool> TryAsBool(this JsonNode? node) =>
74+
node.TryAsJsonValue()
75+
.Bind(jsonValue => jsonValue.TryAsBool());
5276
}
5377

5478
public static class JsonArrayExtensions
@@ -134,7 +158,25 @@ public static Either<string, string> TryGetNonEmptyOrWhiteSpaceStringProperty(th
134158
public static string GetStringProperty(this JsonObject jsonObject, string propertyName) =>
135159
jsonObject.TryGetStringProperty(propertyName)
136160
.IfLeftThrow();
137-
161+
162+
public static int GetIntProperty(this JsonObject jsonObject, string propertyName) =>
163+
jsonObject.TryGetIntProperty(propertyName)
164+
.IfLeftThrow();
165+
166+
public static Either<string, int> TryGetIntProperty(this JsonObject? jsonObject, string propertyName) =>
167+
jsonObject.TryGetProperty(propertyName)
168+
.Bind(JsonNodeExtensions.TryAsInt)
169+
.BindPropertyError(propertyName);
170+
171+
public static bool GetBoolProperty(this JsonObject jsonObject, string propertyName) =>
172+
jsonObject.TryGetBoolProperty(propertyName)
173+
.IfLeftThrow();
174+
175+
public static Either<string, bool> TryGetBoolProperty(this JsonObject? jsonObject, string propertyName) =>
176+
jsonObject.TryGetProperty(propertyName)
177+
.Bind(JsonNodeExtensions.TryAsBool)
178+
.BindPropertyError(propertyName);
179+
138180
public static JsonObject Parse<T>(T obj) =>
139181
TryParse(obj)
140182
.IfLeft(() => throw new JsonException($"Could not parse {typeof(T).Name} as a JSON object."));

tools/code/common/Logger.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace common;
1414

15-
public sealed record LoggerName : ResourceName
15+
public sealed record LoggerName : ResourceName, IResourceName<LoggerName>
1616
{
1717
private LoggerName(string value) : base(value) { }
1818

@@ -168,6 +168,12 @@ public static IAsyncEnumerable<LoggerName> ListNames(this LoggersUri uri, HttpPi
168168
return (name, dto);
169169
});
170170

171+
public static async ValueTask<Option<LoggerDto>> TryGetDto(this LoggerUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
172+
{
173+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
174+
return contentOption.Map(content => content.ToObjectFromJson<LoggerDto>());
175+
}
176+
171177
public static async ValueTask<LoggerDto> GetDto(this LoggerUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
172178
{
173179
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/NamedValue.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace common;
1414

15-
public sealed record NamedValueName : ResourceName
15+
public sealed record NamedValueName : ResourceName, IResourceName<NamedValueName>
1616
{
1717
private NamedValueName(string value) : base(value) { }
1818

@@ -179,6 +179,12 @@ public static IAsyncEnumerable<NamedValueName> ListNames(this NamedValuesUri uri
179179
return (name, dto);
180180
});
181181

182+
public static async ValueTask<Option<NamedValueDto>> TryGetDto(this NamedValueUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
183+
{
184+
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
185+
return contentOption.Map(content => content.ToObjectFromJson<NamedValueDto>());
186+
}
187+
182188
public static async ValueTask<NamedValueDto> GetDto(this NamedValueUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
183189
{
184190
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);

tools/code/common/PolicyFragment.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace common;
1313

14-
public sealed record PolicyFragmentName : ResourceName
14+
public sealed record PolicyFragmentName : ResourceName, IResourceName<PolicyFragmentName>
1515
{
1616
private PolicyFragmentName(string value) : base(value) { }
1717

@@ -184,6 +184,13 @@ public static IAsyncEnumerable<PolicyFragmentName> ListNames(this PolicyFragment
184184
return (name, dto);
185185
});
186186

187+
public static async ValueTask<Option<PolicyFragmentDto>> TryGetDto(this PolicyFragmentUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
188+
{
189+
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
190+
var contentOption = await pipeline.GetContentOption(contentUri, cancellationToken);
191+
return contentOption.Map(content => content.ToObjectFromJson<PolicyFragmentDto>());
192+
}
193+
187194
public static async ValueTask<PolicyFragmentDto> GetDto(this PolicyFragmentUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
188195
{
189196
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();

0 commit comments

Comments
 (0)