Skip to content

Allows multi-format configurations. #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public class ConfigListener
/// Configuration group
/// </summary>
public string Group { get; set; }

/// <summary>
/// Configuration format
/// </summary>
public string Format { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using global::Microsoft.Extensions.Configuration;
using global::Microsoft.Extensions.Logging;
using Nacos.V2;
using Nacos.V2.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -14,6 +15,7 @@ internal class NacosV2ConfigurationProvider : ConfigurationProvider, IDisposable
private readonly NacosV2ConfigurationSource _configurationSource;

private readonly INacosConfigurationParser _parser;
private readonly IReadOnlyDictionary<string, INacosConfigurationParser> _parsers;

private readonly INacosConfigService _client;

Expand All @@ -27,6 +29,7 @@ public NacosV2ConfigurationProvider(NacosV2ConfigurationSource configurationSour
{
_configurationSource = configurationSource;
_parser = configurationSource.NacosConfigurationParser;
_parsers = configurationSource.NacosConfigurationParsers;
_configDict = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_listenerDict = new Dictionary<string, MsConfigListener>();

Expand Down Expand Up @@ -55,6 +58,18 @@ public NacosV2ConfigurationProvider(NacosV2ConfigurationSource configurationSour
}
}

private INacosConfigurationParser GetParser(string format)
{
if (format.IsNullOrWhiteSpace()) return _parser;

if (_parsers.TryGetValue(format, out var parser))
{
return parser;
}

throw new InvalidOperationException("Missing parser for format: " + format);
}

internal IDictionary<string, string> GetData() => Data;

public void Dispose()
Expand Down Expand Up @@ -92,7 +107,7 @@ public override void Load()

_configDict.AddOrUpdate($"{_configurationSource.GetNamespace()}#{listener.Group}#{listener.DataId}", config, (x, y) => config);

var data = _parser.Parse(config);
var data = GetParser(listener.Format).Parse(config);

foreach (var item in data)
{
Expand Down Expand Up @@ -167,7 +182,7 @@ public void ReceiveConfigInfo(string configInfo)
continue;
}

var data = _provider._parser.Parse(config);
var data = _provider.GetParser(listener.Format).Parse(config);

foreach (var item in data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class NacosV2ConfigurationSource : Nacos.V2.NacosSdkOptions, IConfigurati
/// </summary>
public INacosConfigurationParser NacosConfigurationParser { get; set; }

public Dictionary<string, INacosConfigurationParser> NacosConfigurationParsers { get; } =
new Dictionary<string, INacosConfigurationParser>()
{
{ "json", DefaultJsonConfigurationStringParser.Instance }
};

public NacosV2ConfigurationSource(INacosConfigService client, ILoggerFactory loggerFactory)
{
Client = client;
Expand Down
Loading