Skip to content

Commit

Permalink
Optimize resource name filtering in extractor. Addresses #610.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Fankam committed Aug 2, 2024
1 parent e6feb1e commit f1c1231
Show file tree
Hide file tree
Showing 35 changed files with 623 additions and 498 deletions.
8 changes: 7 additions & 1 deletion tools/code/common/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static Option<ApiRevisionNumber> TryFrom(string? value) =>
: Option<ApiRevisionNumber>.None;
}

public sealed record ApiName : ResourceName
public sealed record ApiName : ResourceName, IResourceName<ApiName>
{
private const string RevisionSeparator = ";rev=";

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

public static async ValueTask<Option<ApiDto>> TryGetDto(this ApiUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<ApiDto>());
}

public static async ValueTask<ApiDto> GetDto(this ApiUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Backend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace common;

public sealed record BackendName : ResourceName
public sealed record BackendName : ResourceName, IResourceName<BackendName>
{
private BackendName(string value) : base(value) { }

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

public static async ValueTask<Option<BackendDto>> TryGetDto(this BackendUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<BackendDto>());
}

public static async ValueTask<BackendDto> GetDto(this BackendUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
5 changes: 5 additions & 0 deletions tools/code/common/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ protected ResourceName(string value) : base(value) { }
public override int GetHashCode() => Value.GetHashCode(StringComparison.OrdinalIgnoreCase);
}

public interface IResourceName<T>
{
public abstract static T From(string value);
}

public abstract record ResourceDirectory
{
protected abstract DirectoryInfo Value { get; }
Expand Down
9 changes: 7 additions & 2 deletions tools/code/common/Diagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

namespace common;

public sealed record DiagnosticName : ResourceName
public sealed record DiagnosticName : ResourceName, IResourceName<DiagnosticName>
{
private DiagnosticName(string value) : base(value) { }

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

public static async ValueTask<Option<DiagnosticDto>> TryGetDto(this DiagnosticUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<DiagnosticDto>());
}

public static async ValueTask<DiagnosticDto> GetDto(this DiagnosticUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace common;

public sealed record GatewayName : ResourceName
public sealed record GatewayName : ResourceName, IResourceName<GatewayName>
{
private GatewayName(string value) : base(value) { }

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

public static async ValueTask<Option<GatewayDto>> TryGetDto(this GatewayUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<GatewayDto>());
}

public static async ValueTask<GatewayDto> GetDto(this GatewayUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace common;

public sealed record GroupName : ResourceName
public sealed record GroupName : ResourceName, IResourceName<GroupName>
{
private GroupName(string value) : base(value) { }

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

public static async ValueTask<Option<GroupDto>> TryGetDto(this GroupUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<GroupDto>());
}

public static async ValueTask<GroupDto> GetDto(this GroupUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
44 changes: 43 additions & 1 deletion tools/code/common/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public static Either<string, Uri> TryAsAbsoluteUri(this JsonValue? jsonValue) =>
.Bind(uriString => Uri.TryCreate(uriString, UriKind.Absolute, out var uri)
? Either<string, Uri>.Right(uri)
: $"JSON value '{uriString}' is not a valid absolute URI.");

public static Either<string, int> TryAsInt(this JsonValue? jsonValue) =>
jsonValue is null
? "JSON value is null."
: jsonValue.TryGetValue<int>(out var intValue)
|| (jsonValue.TryGetValue<string>(out var stringValue) && int.TryParse(stringValue, out intValue))
? intValue
: "JSON value is not an integer.";

public static Either<string, bool> TryAsBool(this JsonValue? jsonValue) =>
jsonValue is null
? "JSON value is null."
: jsonValue.TryGetValue<bool>(out var boolValue)
|| (jsonValue.TryGetValue<string>(out var stringValue) && bool.TryParse(stringValue, out boolValue))
? boolValue
: "JSON value is not a boolean.";
}

public static class JsonNodeExtensions
Expand All @@ -49,6 +65,14 @@ public static Either<string, string> TryAsString(this JsonNode? node) =>
public static Either<string, Uri> TryAsAbsoluteUri(this JsonNode? node) =>
node.TryAsJsonValue()
.Bind(jsonValue => jsonValue.TryAsAbsoluteUri());

public static Either<string, int> TryAsInt(this JsonNode? node) =>
node.TryAsJsonValue()
.Bind(jsonValue => jsonValue.TryAsInt());

public static Either<string, bool> TryAsBool(this JsonNode? node) =>
node.TryAsJsonValue()
.Bind(jsonValue => jsonValue.TryAsBool());
}

public static class JsonArrayExtensions
Expand Down Expand Up @@ -134,7 +158,25 @@ public static Either<string, string> TryGetNonEmptyOrWhiteSpaceStringProperty(th
public static string GetStringProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetStringProperty(propertyName)
.IfLeftThrow();


public static int GetIntProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetIntProperty(propertyName)
.IfLeftThrow();

public static Either<string, int> TryGetIntProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsInt)
.BindPropertyError(propertyName);

public static bool GetBoolProperty(this JsonObject jsonObject, string propertyName) =>
jsonObject.TryGetBoolProperty(propertyName)
.IfLeftThrow();

public static Either<string, bool> TryGetBoolProperty(this JsonObject? jsonObject, string propertyName) =>
jsonObject.TryGetProperty(propertyName)
.Bind(JsonNodeExtensions.TryAsBool)
.BindPropertyError(propertyName);

public static JsonObject Parse<T>(T obj) =>
TryParse(obj)
.IfLeft(() => throw new JsonException($"Could not parse {typeof(T).Name} as a JSON object."));
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace common;

public sealed record LoggerName : ResourceName
public sealed record LoggerName : ResourceName, IResourceName<LoggerName>
{
private LoggerName(string value) : base(value) { }

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

public static async ValueTask<Option<LoggerDto>> TryGetDto(this LoggerUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<LoggerDto>());
}

public static async ValueTask<LoggerDto> GetDto(this LoggerUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/NamedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace common;

public sealed record NamedValueName : ResourceName
public sealed record NamedValueName : ResourceName, IResourceName<NamedValueName>
{
private NamedValueName(string value) : base(value) { }

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

public static async ValueTask<Option<NamedValueDto>> TryGetDto(this NamedValueUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<NamedValueDto>());
}

public static async ValueTask<NamedValueDto> GetDto(this NamedValueUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
9 changes: 8 additions & 1 deletion tools/code/common/PolicyFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace common;

public sealed record PolicyFragmentName : ResourceName
public sealed record PolicyFragmentName : ResourceName, IResourceName<PolicyFragmentName>
{
private PolicyFragmentName(string value) : base(value) { }

Expand Down Expand Up @@ -184,6 +184,13 @@ public static IAsyncEnumerable<PolicyFragmentName> ListNames(this PolicyFragment
return (name, dto);
});

public static async ValueTask<Option<PolicyFragmentDto>> TryGetDto(this PolicyFragmentUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentOption = await pipeline.GetContentOption(contentUri, cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<PolicyFragmentDto>());
}

public static async ValueTask<PolicyFragmentDto> GetDto(this PolicyFragmentUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace common;

public sealed record ProductName : ResourceName
public sealed record ProductName : ResourceName, IResourceName<ProductName>
{
private ProductName(string value) : base(value) { }

Expand Down Expand Up @@ -175,6 +175,12 @@ public static IAsyncEnumerable<ProductName> ListNames(this ProductsUri uri, Http
return (name, dto);
});

public static async ValueTask<Option<ProductDto>> TryGetDto(this ProductUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<ProductDto>());
}

public static async ValueTask<ProductDto> GetDto(this ProductUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace common;

public sealed record SubscriptionName : ResourceName
public sealed record SubscriptionName : ResourceName, IResourceName<SubscriptionName>
{
private SubscriptionName(string value) : base(value) { }

Expand Down Expand Up @@ -175,6 +175,12 @@ public static IAsyncEnumerable<SubscriptionName> ListNames(this SubscriptionsUri
return (name, dto);
});

public static async ValueTask<Option<SubscriptionDto>> TryGetDto(this SubscriptionUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<SubscriptionDto>());
}

public static async ValueTask<SubscriptionDto> GetDto(this SubscriptionUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace common;

public sealed record TagName : ResourceName
public sealed record TagName : ResourceName, IResourceName<TagName>
{
private TagName(string value) : base(value) { }

Expand Down Expand Up @@ -146,6 +146,12 @@ public static IAsyncEnumerable<TagName> ListNames(this TagsUri uri, HttpPipeline
return (name, dto);
});

public static async ValueTask<Option<TagDto>> TryGetDto(this TagUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<TagDto>());
}

public static async ValueTask<TagDto> GetDto(this TagUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/VersionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace common;

public sealed record VersionSetName : ResourceName
public sealed record VersionSetName : ResourceName, IResourceName<VersionSetName>
{
private VersionSetName(string value) : base(value) { }

Expand Down Expand Up @@ -167,6 +167,12 @@ public static IAsyncEnumerable<VersionSetName> ListNames(this VersionSetsUri uri
return (name, dto);
});

public static async ValueTask<Option<VersionSetDto>> TryGetDto(this VersionSetUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<VersionSetDto>());
}

public static async ValueTask<VersionSetDto> GetDto(this VersionSetUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
8 changes: 7 additions & 1 deletion tools/code/common/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace common;

public sealed record WorkspaceName : ResourceName
public sealed record WorkspaceName : ResourceName, IResourceName<WorkspaceName>
{
private WorkspaceName(string value) : base(value) { }

Expand Down Expand Up @@ -175,6 +175,12 @@ public static IAsyncEnumerable<WorkspaceName> ListNames(this WorkspacesUri uri,
return (name, dto);
});

public static async ValueTask<Option<WorkspaceDto>> TryGetDto(this WorkspaceUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var contentOption = await pipeline.GetContentOption(uri.ToUri(), cancellationToken);
return contentOption.Map(content => content.ToObjectFromJson<WorkspaceDto>());
}

public static async ValueTask<WorkspaceDto> GetDto(this WorkspaceUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = await pipeline.GetContent(uri.ToUri(), cancellationToken);
Expand Down
Loading

0 comments on commit f1c1231

Please sign in to comment.