YamlSerializerFactory continues to grow as we add parsing features to YAML files. A new behavior system now provides granular sets of features to the factory. To extend the functionality of the YAML parser, implement the `IYamlBehavior` interface. It will automatically be registered to Autofac and injected into `YamlSerializerFactory`.pull/153/head
parent
f2bd1fd1be
commit
eb9898fdd7
@ -1,9 +0,0 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Recyclarr.TrashLib.Config;
|
||||
|
||||
public interface IYamlSerializerFactory
|
||||
{
|
||||
IDeserializer CreateDeserializer(Action<DeserializerBuilder>? extraBuilder = null);
|
||||
ISerializer CreateSerializer();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Recyclarr.TrashLib.Config.Yaml;
|
||||
|
||||
public interface IYamlBehavior
|
||||
{
|
||||
void Setup(DeserializerBuilder builder);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Recyclarr.TrashLib.Config.Yaml;
|
||||
|
||||
public interface IYamlSerializerFactory
|
||||
{
|
||||
IDeserializer CreateDeserializer();
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using JetBrains.Annotations;
|
||||
using Recyclarr.TrashLib.Config.Secrets;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Recyclarr.TrashLib.Config.Yaml;
|
||||
|
||||
[UsedImplicitly]
|
||||
public class SecretsYamlBehavior : IYamlBehavior
|
||||
{
|
||||
private readonly ISecretsProvider _secretsProvider;
|
||||
|
||||
public SecretsYamlBehavior(ISecretsProvider secretsProvider)
|
||||
{
|
||||
_secretsProvider = secretsProvider;
|
||||
}
|
||||
|
||||
public void Setup(DeserializerBuilder builder)
|
||||
{
|
||||
builder
|
||||
.WithNodeDeserializer(new SecretsDeserializer(_secretsProvider))
|
||||
.WithTagMapping("!secret", typeof(SecretTag));
|
||||
}
|
||||
}
|
@ -1,42 +1,34 @@
|
||||
using Recyclarr.Common.YamlDotNet;
|
||||
using Recyclarr.TrashLib.Config.Secrets;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
|
||||
namespace Recyclarr.TrashLib.Config;
|
||||
namespace Recyclarr.TrashLib.Config.Yaml;
|
||||
|
||||
public class YamlSerializerFactory : IYamlSerializerFactory
|
||||
{
|
||||
private readonly IObjectFactory _objectFactory;
|
||||
private readonly ISecretsProvider _secretsProvider;
|
||||
private readonly IEnumerable<IYamlBehavior> _behaviors;
|
||||
|
||||
public YamlSerializerFactory(IObjectFactory objectFactory, ISecretsProvider secretsProvider)
|
||||
public YamlSerializerFactory(IObjectFactory objectFactory, IEnumerable<IYamlBehavior> behaviors)
|
||||
{
|
||||
_objectFactory = objectFactory;
|
||||
_secretsProvider = secretsProvider;
|
||||
_behaviors = behaviors;
|
||||
}
|
||||
|
||||
public IDeserializer CreateDeserializer(Action<DeserializerBuilder>? extraBuilder = null)
|
||||
public IDeserializer CreateDeserializer()
|
||||
{
|
||||
var builder = new DeserializerBuilder()
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||
.WithTypeConverter(new YamlNullableEnumTypeConverter())
|
||||
.WithNodeDeserializer(new SecretsDeserializer(_secretsProvider))
|
||||
.WithTagMapping("!secret", typeof(SecretTag))
|
||||
.WithNodeTypeResolver(new ReadOnlyCollectionNodeTypeResolver())
|
||||
.WithNodeDeserializer(new ForceEmptySequences(_objectFactory))
|
||||
.WithObjectFactory(_objectFactory);
|
||||
|
||||
extraBuilder?.Invoke(builder);
|
||||
foreach (var behavior in _behaviors)
|
||||
{
|
||||
behavior.Setup(builder);
|
||||
}
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
public ISerializer CreateSerializer()
|
||||
{
|
||||
return new SerializerBuilder()
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||
.WithTypeConverter(new YamlNullableEnumTypeConverter())
|
||||
.Build();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue