-
Notifications
You must be signed in to change notification settings - Fork 727
/
ProvidersConfiguration.cs
69 lines (56 loc) · 2.28 KB
/
ProvidersConfiguration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text.Json;
using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.Semantics.Namespaces;
namespace Bicep.Core.Configuration;
public record ProviderConfigEntry
{
public bool BuiltIn => this.Scheme == "builtin";
public string Path { get; }
public string Scheme { get; }
public ProviderConfigEntry(string providerConfigEntry)
{
var parts = providerConfigEntry.Split(':', StringSplitOptions.None);
Debug.Assert(parts.Length is >= 1 and <= 3, "The provider configuration entry must have 1-3 parts separated by colons.");
this.Scheme = parts[0]; // Is ensured to exist since there is a pattern match in the bicepconfig json schema
this.Path = parts.Length > 1 ? string.Join(':', parts[1..]) : string.Empty;
}
public override string ToString()
{
return $"{this.Scheme}:{this.Path}";
}
}
public partial class ProvidersConfiguration : ConfigurationSection<ImmutableDictionary<string, ProviderConfigEntry>>
{
private ProvidersConfiguration(ImmutableDictionary<string, ProviderConfigEntry> data) : base(data) { }
public static ProvidersConfiguration Bind(JsonElement element)
=> new(element.ToNonNullObject<ImmutableDictionary<string, string>>()
.ToImmutableDictionary(
pair => pair.Key,
pair => new ProviderConfigEntry(pair.Value))
);
public ResultWithDiagnostic<ProviderConfigEntry> TryGetProviderSource(string providerName)
{
if (!this.Data.TryGetValue(providerName, out var providerConfigEntry))
{
return new(x => x.UnrecognizedProvider(providerName));
}
return new(providerConfigEntry);
}
public override void WriteTo(Utf8JsonWriter writer)
{
writer.WriteStartObject();
foreach (var (key, value) in this.Data)
{
writer.WritePropertyName(key);
writer.WriteStringValue(value.ToString());
}
writer.WriteEndObject();
}
public bool IsSysOrBuiltIn(string providerName)
=> providerName == SystemNamespaceType.BuiltInName || this.Data.TryGetValue(providerName)?.BuiltIn == true;
}