Skip to content

Commit

Permalink
try to deserialize the config contents
Browse files Browse the repository at this point in the history
  • Loading branch information
asilverman committed Jan 22, 2024
1 parent d09a014 commit 50f0a8d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Bicep.Core.Configuration;
using Bicep.Core.Json;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bicep.Core.UnitTests.Configuration;

[TestClass]
public class ProvidersConfigurationTests
{
private static readonly JsonSerializerOptions DefaultDeserializeOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReadCommentHandling = JsonCommentHandling.Skip,
Converters = { new JsonStringEnumConverter() },
};

[TestMethod]
public void MyExploratoryTest()
{
var data = JsonElementFactory.CreateElement("""
{
"providers": {
"az": {
"source": {
"registry": "mcr.microsoft.com/bicep/providers",
"version": "0.2.3"
},
"isDefaultImport": true
},
"kubernetes": {
"source": {
"builtin": true
}
}
}
}
""");

var res = JsonSerializer.Deserialize<ProvidersConfigurationSection>(data, DefaultDeserializeOptions);
res.Should().NotBeNull();
res!.Providers.Should().NotBeNull();
res.Providers.Count.Should().Be(2);
res.Providers["az"].Source.Should().NotBeNull();
// verifies that 'registry' and 'version' are valid properties for 'source'
res.Providers["az"].Source!.Registry.Should().Be("mcr.microsoft.com/bicep/providers");
res.Providers["az"].Source!.Version.Should().Be("0.2.3");
// verifies the default value for 'IsDefaultImport' is true when specified
res.Providers["az"].IsDefaultImport.Should().BeTrue();
res.Providers["kubernetes"].Source.Should().NotBeNull();
// verifies that 'builtin' is a valid property for 'source'
res.Providers["kubernetes"].Source!.Builtin.Should().BeTrue();
// verifies the default value for 'IsDefaultImport' is false when unspecified
res.Providers["kubernetes"].IsDefaultImport.Should().BeFalse();
}
}
30 changes: 30 additions & 0 deletions src/Bicep.Core/Configuration/ProvidersConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Immutable;
using System.Text.Json;
using Bicep.Core.Extensions;

namespace Bicep.Core.Configuration;

public record ProvidersConfigurationSection(ImmutableSortedDictionary<string, ProviderDescriptor> Providers) { }

public record ProviderDescriptor(bool IsDefaultImport, ProviderSource? Source) { }

public record ProviderSource(bool Builtin, string? Registry, string? Version) { }

public partial class ProvidersConfiguration : ConfigurationSection<ProvidersConfigurationSection>
{
private readonly string? configurationPath;

private ProvidersConfiguration(ProvidersConfigurationSection data, string? configurationPath)
: base(data)
{
this.configurationPath = configurationPath;
}

public static ProvidersConfiguration Bind(JsonElement element, string? configurationPath) => new(element.ToNonNullObject<ProvidersConfigurationSection>(), configurationPath);

}


0 comments on commit 50f0a8d

Please sign in to comment.