Skip to content

Commit

Permalink
refactor for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
asilverman committed Oct 12, 2023
1 parent fea7164 commit bb88dfe
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/Bicep.Cli/Commands/PublishCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Bicep.Core.Features;
using Bicep.Core.FileSystem;
using Bicep.Core.Registry;
using Bicep.Core.Registry.Oci;
using Bicep.Core.SourceCode;

namespace Bicep.Cli.Commands
Expand Down Expand Up @@ -99,7 +98,7 @@ private async Task PublishModuleAsync(ArtifactReference target, Stream compiledA

private ArtifactReference ValidateReference(string targetModuleReference, Uri targetModuleUri)
{
if (!this.moduleDispatcher.TryGetArtifactReference(targetModuleReference, "module", targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder))
if (!this.moduleDispatcher.TryGetModuleReference(targetModuleReference, targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder))
{
// TODO: We should probably clean up the dispatcher contract so this sort of thing isn't necessary (unless we change how target module is set in this command)
var message = failureBuilder(DiagnosticBuilder.ForDocumentStart()).Message;
Expand Down
8 changes: 4 additions & 4 deletions src/Bicep.Core.IntegrationTests/RegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public async Task ModuleRestoreContentionShouldProduceConsistentState(bool publi

var moduleReferences = dataSet.RegistryModules.Values
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(7);
Expand Down Expand Up @@ -238,7 +238,7 @@ public async Task ModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnumerab
var configuration = BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled;
var moduleReferences = moduleInfos
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(moduleCount);
Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task ForceModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnu

var moduleReferences = moduleInfos
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(moduleCount);
Expand Down Expand Up @@ -384,7 +384,7 @@ public async Task ForceModuleRestoreShouldRestoreAllModules(IEnumerable<External
var configuration = BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled;
var moduleReferences = moduleInfos
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(moduleCount);
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Core.Samples/DataSetsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static Mock<IContainerRegistryClientFactory> CreateMockRegistryClients(Im
{
var target = publishInfo.Metadata.Target;

if (!dispatcher.TryGetArtifactReference(target, "module", RandomFileUri()).IsSuccess(out var @ref) || @ref is not OciArtifactReference targetReference)
if (!dispatcher.TryGetModuleReference(target, RandomFileUri()).IsSuccess(out var @ref) || @ref is not OciArtifactReference targetReference)
{
throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{target}'. Specify a reference to an OCI artifact.");
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public static ITemplateSpecRepositoryFactory CreateMockTemplateSpecRepositoryFac

foreach (var (moduleName, templateSpecInfo) in templateSpecs)
{
if (!dispatcher.TryGetArtifactReference(templateSpecInfo.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) || @ref is not TemplateSpecModuleReference reference)
if (!dispatcher.TryGetModuleReference(templateSpecInfo.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) || @ref is not TemplateSpecModuleReference reference)
{
throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{templateSpecInfo.Metadata.Target}'. Specify a reference to a template spec.");
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public static async Task PublishModuleToRegistryAsync(IContainerRegistryClientFa
.AddSingleton(featureProviderFactory)
).Construct<IModuleDispatcher>();

var targetReference = dispatcher.TryGetArtifactReference(target, "TODO", RandomFileUri()).IsSuccess(out var @ref) ? @ref
var targetReference = dispatcher.TryGetModuleReference(target, RandomFileUri()).IsSuccess(out var @ref) ? @ref
: throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{target}'. Specify a reference to an OCI artifact.");

var result = CompilationHelper.Compile(moduleSource);
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ public ErrorDiagnostic DirectAccessToCollectionNotSupported(IEnumerable<string>?
"BCP162",
"Expected a loop item variable identifier or \"(\" at this location.");

public ErrorDiagnostic ScopeUnsupportedOnChildResource(string parentIdentifier) => new(
public ErrorDiagnostic ScopeUnsupportedOnChildResource() => new(
TextSpan,
"BCP164",
$"A child resource's scope is computed based on the scope of its ancestor resource. This means that using the \"{LanguageConstants.ResourceScopePropertyName}\" property on a child resource is unsupported.");
Expand Down Expand Up @@ -1761,7 +1761,7 @@ public ErrorDiagnostic IndexOutOfBounds(string typeName, long tupleLength, long
var message = new StringBuilder("The provided index value of \"").Append(indexSought).Append("\" is not valid for type \"").Append(typeName).Append("\".");
if (tupleLength > 0)
{
message.Append(" Indexes for this type must be between 0 and ").Append(tupleLength - 1).Append(".");
message.Append(" Indexes for this type must be between 0 and ").Append(tupleLength - 1).Append('.');
}

return new(TextSpan, "BCP311", message.ToString());
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/Registry/ArtifactRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class ArtifactRegistry<T> : IArtifactRegistry where T : Artifact

public abstract ResultWithDiagnostic<Uri> TryGetLocalArtifactEntryPointUri(T reference);

public abstract ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri);
public abstract ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(string? aliasName, string artifactType, string reference);

public abstract string? TryGetDocumentationUri(T reference);

Expand Down
2 changes: 2 additions & 0 deletions src/Bicep.Core/Registry/IArtifactReferenceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public interface IArtifactReferenceFactory

ResultWithDiagnostic<ArtifactReference> TryGetArtifactReference(string reference, string artifactType, Uri parentModuleUri);

ResultWithDiagnostic<ArtifactReference> TryGetModuleReference(string reference, Uri parentModuleUri);

ResultWithDiagnostic<ArtifactReference> TryGetArtifactReference(IArtifactReferenceSyntax artifactDeclaration, Uri parentModuleUri);
}
3 changes: 1 addition & 2 deletions src/Bicep.Core/Registry/IArtifactRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public interface IArtifactRegistry
/// <param name="aliasName">The alias name</param>
/// <param name="reference">The unqualified artifact reference</param>
/// <param name="artifactType">The artifact type. Either "module" or "provider"</param>
/// <param name="parentModuleUri">The URI of the parent module</param>
ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri);
ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(string? aliasName, string artifactType, string reference);

/// <summary>
/// Returns true if the specified artifact is already cached in the local cache.
Expand Down
8 changes: 6 additions & 2 deletions src/Bicep.Core/Registry/ModuleDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ private ImmutableDictionary<string, IArtifactRegistry> Registries(Uri parentModu
public ImmutableArray<string> AvailableSchemes(Uri parentModuleUri)
=> Registries(parentModuleUri).Keys.OrderBy(s => s).ToImmutableArray();

public ResultWithDiagnostic<ArtifactReference> TryGetModuleReference(string reference, Uri parentModuleUri)
=> TryGetArtifactReference(reference, "module", parentModuleUri);


public ResultWithDiagnostic<ArtifactReference> TryGetArtifactReference(string reference, string artifactType, Uri parentModuleUri)
{
var registries = Registries(parentModuleUri);
Expand All @@ -51,7 +55,7 @@ public ResultWithDiagnostic<ArtifactReference> TryGetArtifactReference(string re
// local path reference
if (registries.TryGetValue(ModuleReferenceSchemes.Local, out var localRegistry))
{
return localRegistry.TryParseArtifactReference(null, "module", parts[0], parentModuleUri);
return localRegistry.TryParseArtifactReference(null, "module", parts[0]);
}

return new(x => x.UnknownModuleReferenceScheme(ModuleReferenceSchemes.Local, this.AvailableSchemes(parentModuleUri)));
Expand All @@ -73,7 +77,7 @@ public ResultWithDiagnostic<ArtifactReference> TryGetArtifactReference(string re
// the scheme is recognized
var rawValue = parts[1];

return registry.TryParseArtifactReference(aliasName, artifactType, rawValue, parentModuleUri);
return registry.TryParseArtifactReference(aliasName, artifactType, rawValue);
}

// unknown scheme
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/Registry/OciArtifactRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override RegistryCapabilities GetCapabilities(OciArtifactReference refere
return reference.Tag is null ? RegistryCapabilities.Default : RegistryCapabilities.Publish;
}

public override ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(string? aliasName, string artifactType, string reference, Uri parentModuleUri)
public override ResultWithDiagnostic<ArtifactReference> TryParseArtifactReference(string? aliasName, string artifactType, string reference)
{
var type = artifactType switch
{
Expand Down

0 comments on commit bb88dfe

Please sign in to comment.