Skip to content

Commit

Permalink
output options provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVollmers committed May 4, 2024
1 parent f35eca3 commit 7d1995f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/Doki.CommandLine/JsonOutputOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@ namespace Doki.CommandLine;

internal class JsonOutputOptionsProvider : IOutputOptionsProvider
{
public TOptions RequireOptions<TOutput, TOptions>(string outputType) where TOutput : class, IOutput
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};

private readonly Dictionary<string, JsonElement?> _options = new();

public TOptions? GetOptions<TOutput, TOptions>(string outputType) where TOutput : class, IOutput
where TOptions : class, IOutputOptions<TOutput>
{
throw new NotImplementedException();
ArgumentNullException.ThrowIfNull(outputType);

return _options.TryGetValue(outputType, out var options)
? options?.Deserialize<TOptions>(JsonSerializerOptions)
: null;
}

public void AddOptions(string outputType, JsonElement? options)
{
throw new NotImplementedException();
_options[outputType] = options;
}
}
2 changes: 1 addition & 1 deletion src/Doki.Output.Extensions/IOutputOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface IOutputOptionsProvider
{
TOptions RequireOptions<TOutput, TOptions>(string outputType) where TOutput : class, IOutput
TOptions? GetOptions<TOutput, TOptions>(string outputType) where TOutput : class, IOutput
where TOptions : class, IOutputOptions<TOutput>;
}
4 changes: 2 additions & 2 deletions src/Doki.Output.Extensions/OutputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static IServiceCollection AddOutputOptions<TOutput, TOptions>(this IServi
services.AddSingleton<IOutputOptions<TOutput>>(provider =>
{
var optionsProvider = provider.GetService<IOutputOptionsProvider>();
if (optionsProvider != null) return optionsProvider.RequireOptions<TOutput, TOptions>(outputType);
var options = optionsProvider?.GetOptions<TOutput, TOptions>(outputType);

return ActivatorUtilities.CreateInstance<TOptions>(provider);
return options ?? ActivatorUtilities.CreateInstance<TOptions>(provider);
});

return services;
Expand Down

0 comments on commit 7d1995f

Please sign in to comment.