Skip to content

Commit

Permalink
Added logging (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjoerdsjoerd authored Apr 25, 2024
1 parent 1dec0c5 commit 63cd484
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace RepoM.ActionMenu.Core.Yaml.Serialization;

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using Microsoft.Extensions.Logging;
using RepoM.ActionMenu.Core.Misc;
using RepoM.ActionMenu.Core.Model;
using RepoM.ActionMenu.Core.Yaml.Model;
Expand Down Expand Up @@ -34,17 +36,21 @@ internal class ActionMenuDeserializer : IActionMenuDeserializer
{ typeof(Predicate), () => new ScribanPredicate() },
{ typeof(Text), () => new ScribanText() },
};
private readonly ILogger _logger;

public ActionMenuDeserializer(IEnumerable<IKeyTypeRegistration<IMenuAction>> keyTypeRegistrations, ITemplateParser templateParser)
: this(keyTypeRegistrations.ToDictionary(item => item.Tag, item => item.ConfigurationType), templateParser)
public ActionMenuDeserializer(IEnumerable<IKeyTypeRegistration<IMenuAction>> keyTypeRegistrations, ITemplateParser templateParser, ILogger logger)
: this(keyTypeRegistrations.ToDictionary(item => item.Tag, item => item.ConfigurationType), templateParser, logger)
{
}

private ActionMenuDeserializer(IDictionary<string, Type> menuActionTypes, ITemplateParser templateParser)
private ActionMenuDeserializer(IDictionary<string, Type> menuActionTypes, ITemplateParser templateParser, ILogger logger)
{
ArgumentNullException.ThrowIfNull(menuActionTypes);
ArgumentNullException.ThrowIfNull(templateParser);

ArgumentNullException.ThrowIfNull(logger);

_logger = logger;

_serializer = new SerializerBuilder()
.WithNamingConvention(HyphenatedNamingConvention.Instance) // CamelCaseNamingConvention.Instance
.WithDefaultScalarStyle(ScalarStyle.Any)
Expand All @@ -70,7 +76,7 @@ private ActionMenuDeserializer(IDictionary<string, Type> menuActionTypes, ITempl
{ ContextActionSetVariableV1.TYPE_VALUE, typeof(ContextActionSetVariableV1)},
{ ContextActionLoadFileV1.TYPE_VALUE, typeof(ContextActionLoadFileV1)},
}));
options.AddKeyValueTypeDiscriminator<IMenuAction>("type", menuActionTypes);
options.AddTypeDiscriminator(new RequiredKeyValueTypeDiscriminator<IMenuAction>("type", menuActionTypes, _logger));
},
maxDepth: -1,
maxLength: -1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace RepoM.ActionMenu.Core.Yaml.Serialization;

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization.BufferedDeserialization;
using YamlDotNet.Serialization.BufferedDeserialization.TypeDiscriminators;

internal class RequiredKeyValueTypeDiscriminator<TInterface> : ITypeDiscriminator
where TInterface : class
{
private readonly KeyValueTypeDiscriminator _discriminator;
private readonly ILogger _logger;

public RequiredKeyValueTypeDiscriminator(string key, IDictionary<string, Type> mapping, ILogger logger)
{
_discriminator = new KeyValueTypeDiscriminator(typeof(TInterface), key, mapping);
BaseType = typeof(TInterface);
_logger = logger;
}

public Type BaseType { get; }

public bool TryDiscriminate(IParser buffer, out Type? suggestedType)
{
var result = _discriminator.TryDiscriminate(buffer, out suggestedType);

if (!result)
{
if (buffer.Current is Scalar scalar)
{
_logger.LogError("Could not find required type. Type found {Type}", scalar.Value);
}
else
{
_logger.LogError("Could not find required type");
}

suggestedType = null;
return false;
}

return true;
}
}
16 changes: 16 additions & 0 deletions src/RepoM.ActionMenu.Interface/YamlModel/DefaultMenuAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace RepoM.ActionMenu.Interface.YamlModel;

using RepoM.ActionMenu.Interface.YamlModel.Templating;

public class DefaultMenuAction : IMenuAction
{
public string Type { get; }

public Predicate Active { get; }

public DefaultMenuAction()
{
Type = string.Empty;
Active = new Predicate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace RepoM.ActionMenu.Core.Tests.Yaml.Serialization;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using RepoM.ActionMenu.Core.Misc;
using RepoM.ActionMenu.Core.Yaml.Model;
using RepoM.ActionMenu.Core.Yaml.Model.ActionContext.SetVariable;
Expand All @@ -22,7 +23,7 @@ public class ActionMenuDeserializerTests

public ActionMenuDeserializerTests()
{
_sut = new ActionMenuDeserializer(_registrations, _templateParser);
_sut = new ActionMenuDeserializer(_registrations, _templateParser, NullLogger.Instance);
}

[Fact]
Expand Down

0 comments on commit 63cd484

Please sign in to comment.