From 63cd484fb9c3e2c20d1b17aad9cab2007f194754 Mon Sep 17 00:00:00 2001 From: Sjoerd Heijligers Date: Thu, 25 Apr 2024 13:36:02 +0200 Subject: [PATCH] Added logging (#115) --- .../Serialization/ActionMenuDeserializer.cs | 16 +++++-- .../RequiredKeyValueTypeDiscriminator.cs | 47 +++++++++++++++++++ .../YamlModel/DefaultMenuAction.cs | 16 +++++++ .../ActionMenuDeserializerTests.cs | 3 +- 4 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/RepoM.ActionMenu.Core/Yaml/Serialization/RequiredKeyValueTypeDiscriminator.cs create mode 100644 src/RepoM.ActionMenu.Interface/YamlModel/DefaultMenuAction.cs diff --git a/src/RepoM.ActionMenu.Core/Yaml/Serialization/ActionMenuDeserializer.cs b/src/RepoM.ActionMenu.Core/Yaml/Serialization/ActionMenuDeserializer.cs index 037abd50..817ed485 100644 --- a/src/RepoM.ActionMenu.Core/Yaml/Serialization/ActionMenuDeserializer.cs +++ b/src/RepoM.ActionMenu.Core/Yaml/Serialization/ActionMenuDeserializer.cs @@ -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; @@ -34,17 +36,21 @@ internal class ActionMenuDeserializer : IActionMenuDeserializer { typeof(Predicate), () => new ScribanPredicate() }, { typeof(Text), () => new ScribanText() }, }; + private readonly ILogger _logger; - public ActionMenuDeserializer(IEnumerable> keyTypeRegistrations, ITemplateParser templateParser) - : this(keyTypeRegistrations.ToDictionary(item => item.Tag, item => item.ConfigurationType), templateParser) + public ActionMenuDeserializer(IEnumerable> keyTypeRegistrations, ITemplateParser templateParser, ILogger logger) + : this(keyTypeRegistrations.ToDictionary(item => item.Tag, item => item.ConfigurationType), templateParser, logger) { } - private ActionMenuDeserializer(IDictionary menuActionTypes, ITemplateParser templateParser) + private ActionMenuDeserializer(IDictionary 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) @@ -70,7 +76,7 @@ private ActionMenuDeserializer(IDictionary menuActionTypes, ITempl { ContextActionSetVariableV1.TYPE_VALUE, typeof(ContextActionSetVariableV1)}, { ContextActionLoadFileV1.TYPE_VALUE, typeof(ContextActionLoadFileV1)}, })); - options.AddKeyValueTypeDiscriminator("type", menuActionTypes); + options.AddTypeDiscriminator(new RequiredKeyValueTypeDiscriminator("type", menuActionTypes, _logger)); }, maxDepth: -1, maxLength: -1) diff --git a/src/RepoM.ActionMenu.Core/Yaml/Serialization/RequiredKeyValueTypeDiscriminator.cs b/src/RepoM.ActionMenu.Core/Yaml/Serialization/RequiredKeyValueTypeDiscriminator.cs new file mode 100644 index 00000000..fd4058ab --- /dev/null +++ b/src/RepoM.ActionMenu.Core/Yaml/Serialization/RequiredKeyValueTypeDiscriminator.cs @@ -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 : ITypeDiscriminator + where TInterface : class +{ + private readonly KeyValueTypeDiscriminator _discriminator; + private readonly ILogger _logger; + + public RequiredKeyValueTypeDiscriminator(string key, IDictionary 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; + } +} \ No newline at end of file diff --git a/src/RepoM.ActionMenu.Interface/YamlModel/DefaultMenuAction.cs b/src/RepoM.ActionMenu.Interface/YamlModel/DefaultMenuAction.cs new file mode 100644 index 00000000..dee38b60 --- /dev/null +++ b/src/RepoM.ActionMenu.Interface/YamlModel/DefaultMenuAction.cs @@ -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(); + } +} \ No newline at end of file diff --git a/tests/RepoM.ActionMenu.Core.Tests/Yaml/Serialization/ActionMenuDeserializerTests.cs b/tests/RepoM.ActionMenu.Core.Tests/Yaml/Serialization/ActionMenuDeserializerTests.cs index 6ef9f381..6b33eb33 100644 --- a/tests/RepoM.ActionMenu.Core.Tests/Yaml/Serialization/ActionMenuDeserializerTests.cs +++ b/tests/RepoM.ActionMenu.Core.Tests/Yaml/Serialization/ActionMenuDeserializerTests.cs @@ -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; @@ -22,7 +23,7 @@ public class ActionMenuDeserializerTests public ActionMenuDeserializerTests() { - _sut = new ActionMenuDeserializer(_registrations, _templateParser); + _sut = new ActionMenuDeserializer(_registrations, _templateParser, NullLogger.Instance); } [Fact]