From 038a3c3da8d168694f67f80f411078a284448dd1 Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Sat, 19 Nov 2022 15:10:14 +0100 Subject: [PATCH] Adds HasAttrMessage, and obsoletes `TryGetAttrMsg` and `TryGetMsg`. (#26) - Adds `LinguiniBundle.HasAttrMessage` method - Obsoletes `LinguiniBundle.TryGetAttrMsg` for `LinguiniBundle.TryGetAttrMessage` - Obsoletes `LinguiniBundle.TryGetMsg` for `LinguiniBundle.TryGetMessage` --- Linguini.Bundle.Test/Unit/BundleTests.cs | 32 ++++++++++--- Linguini.Bundle/FluentBundle.cs | 57 +++++++++++++++++++----- Linguini.Bundle/Linguini.Bundle.csproj | 2 +- changelog.md | 9 +++- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/Linguini.Bundle.Test/Unit/BundleTests.cs b/Linguini.Bundle.Test/Unit/BundleTests.cs index 836af19..01edaae 100644 --- a/Linguini.Bundle.Test/Unit/BundleTests.cs +++ b/Linguini.Bundle.Test/Unit/BundleTests.cs @@ -93,16 +93,16 @@ public void TestReplaceMessage() .SetUseIsolating(false); var bundle = bundler.UncheckedBuild(); - Assert.IsTrue(bundle.TryGetAttrMsg("term1", null, out _, out var termMsg)); + Assert.IsTrue(bundle.TryGetAttrMessage("term1", null, out _, out var termMsg)); Assert.AreEqual("val1", termMsg); - Assert.IsTrue(bundle.TryGetAttrMsg("term2", null, out _, out var termMsg2)); + Assert.IsTrue(bundle.TryGetAttrMessage("term2", null, out _, out var termMsg2)); Assert.AreEqual("val2", termMsg2); bundle.AddResourceOverriding(_replace2); - Assert.IsTrue(bundle.TryGetAttrMsg("term2", null, out _, out _)); - Assert.IsTrue(bundle.TryGetAttrMsg("term1", null, out _, out termMsg)); + Assert.IsTrue(bundle.TryGetAttrMessage("term2", null, out _, out _)); + Assert.IsTrue(bundle.TryGetAttrMessage("term1", null, out _, out termMsg)); Assert.AreEqual("xxx", termMsg); - Assert.IsTrue(bundle.TryGetAttrMsg("new1.attr", null, out _, out var newMsg)); + Assert.IsTrue(bundle.TryGetAttrMessage("new1.attr", null, out _, out var newMsg)); Assert.AreEqual("6", newMsg); } @@ -150,7 +150,7 @@ public void TestConcurrencyBundler() Parallel.For(0, 10, i => bundler.AddResource($"term-1 = {i}", out _)); Parallel.For(0, 10, i => bundler.AddResource($"term-2= {i}", out _)); - Parallel.For(0, 10, i => bundler.TryGetAttrMsg("term-1", null, out _, out _)); + Parallel.For(0, 10, i => bundler.TryGetAttrMessage("term-1", null, out _, out _)); Parallel.For(0, 10, i => bundler.AddResourceOverriding($"term-2= {i + 1}")); Assert.True(bundler.HasMessage("term-1")); } @@ -166,7 +166,7 @@ public void TestConcurrencyOption() var optBundle = FluentBundle.MakeUnchecked(bundleOpt); Parallel.For(0, 10, i => optBundle.AddResource($"term-1 = {i}", out _)); Parallel.For(0, 10, i => optBundle.AddResource($"term-2= {i}", out _)); - Parallel.For(0, 10, i => optBundle.TryGetAttrMsg("term-1", null, out _, out _)); + Parallel.For(0, 10, i => optBundle.TryGetAttrMessage("term-1", null, out _, out _)); Parallel.For(0, 10, i => optBundle.AddResourceOverriding($"term-2= {i + 1}")); Assert.True(optBundle.HasMessage("term-1")); } @@ -190,6 +190,24 @@ public void TestExample() Assert.AreEqual("Hello, Test!", message); } + [Test] + [Parallelizable] + [TestCase("new1.attr", true)] + [TestCase("new1", true)] + [TestCase("new1.", false)] + [TestCase("new2", false)] + public void TestHasAttrMessage(string idWithAttr, bool found) + { + var bundle = LinguiniBuilder.Builder() + .CultureInfo(new CultureInfo("en")) + .AddResource(_replace2) + .SetUseIsolating(false) + .UncheckedBuild(); + + Assert.AreEqual(bundle.TryGetAttrMessage(idWithAttr, null, out _, out _), + bundle.HasAttrMessage(idWithAttr)); + } + public static IEnumerable TestBundleErrors { get diff --git a/Linguini.Bundle/FluentBundle.cs b/Linguini.Bundle/FluentBundle.cs index 5875c25..0ac8d96 100644 --- a/Linguini.Bundle/FluentBundle.cs +++ b/Linguini.Bundle/FluentBundle.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Runtime.CompilerServices; using Linguini.Bundle.Builder; using Linguini.Bundle.Errors; using Linguini.Bundle.Resolver; @@ -173,6 +174,7 @@ public void AddResourceOverriding(Resource res) } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool HasMessage(string identifier) { var id = (identifier, EntryKind.Message); @@ -180,9 +182,27 @@ public bool HasMessage(string identifier) && _entries[id] is AstMessage; } + public bool HasAttrMessage(string idWithAttr) + { + var attributes = idWithAttr.IndexOf('.'); + if (attributes < 0) + { + return HasMessage(idWithAttr); + } + + var id = idWithAttr.AsSpan(0, attributes).ToString(); + var attr = idWithAttr.AsSpan(attributes + 1).ToString(); + if (TryGetAstMessage(id, out var astMessage)) + { + return astMessage.GetAttribute(attr) != null; + } + + return false; + } + public string? GetAttrMessage(string msgWithAttr, FluentArgs? args = null) { - TryGetAttrMsg(msgWithAttr, args, out var errors, out var message); + TryGetAttrMessage(msgWithAttr, args, out var errors, out var message); if (errors.Count > 0) { throw new LinguiniException(errors); @@ -191,25 +211,23 @@ public bool HasMessage(string identifier) return message; } - public bool TryGetAttrMsg(string msgWithAttr, FluentArgs? args, + public bool TryGetAttrMessage(string msgWithAttr, FluentArgs? args, out IList errors, out string? message) { if (msgWithAttr.Contains(".")) { var split = msgWithAttr.Split('.'); - return TryGetMsg(split[0], split[1], args, out errors, out message); + return TryGetMessage(split[0], split[1], args, out errors, out message); } - return TryGetMsg(msgWithAttr, args, out errors, out message); + return TryGetMessage(msgWithAttr, null, args, out errors, out message); } - public bool TryGetMsg(string id, FluentArgs? args, + public bool TryGetMessage(string id, FluentArgs? args, out IList errors, [NotNullWhen(true)] out string? message) - { - return TryGetMsg(id, null, args, out errors, out message); - } + => TryGetMessage(id, null, args, out errors, out message); - public bool TryGetMsg(string id, string? attribute, FluentArgs? args, + public bool TryGetMessage(string id, string? attribute, FluentArgs? args, out IList errors, [NotNullWhen(true)] out string? message) { string? value = null; @@ -325,5 +343,24 @@ public FluentBundle DeepClone() UseIsolating = UseIsolating, }; } + + #region Obsolete + + [Obsolete("Use TryGetAttrMessage(string, FluentArgs?, out IList, out string?)")] + public bool TryGetAttrMsg(string msgWithAttr, FluentArgs? args, + out IList errors, out string? message) => + TryGetAttrMessage(msgWithAttr, args, out errors, out message); + + [Obsolete("Use TryGetMessage(string, FluentArgs?, out IList, out string?")] + public bool TryGetMsg(string id, FluentArgs? args, + out IList errors, [NotNullWhen(true)] out string? message) + => TryGetMessage(id, args, out errors, out message); + + [Obsolete("Use TryGetMessage")] + public bool TryGetMsg(string id, string? attribute, FluentArgs? args, + out IList errors, [NotNullWhen(true)] out string? message) + => TryGetMessage(id, attribute, args, out errors, out message); + + #endregion } -} +} \ No newline at end of file diff --git a/Linguini.Bundle/Linguini.Bundle.csproj b/Linguini.Bundle/Linguini.Bundle.csproj index 6bbe1ff..719564e 100644 --- a/Linguini.Bundle/Linguini.Bundle.csproj +++ b/Linguini.Bundle/Linguini.Bundle.csproj @@ -18,7 +18,7 @@ It provides easy to use and extend system for describing translations. https://github.com/Ygg01/Linguini git - 0.3.1 + 0.3.2 net461;net5.0;netstandard2.1 diff --git a/changelog.md b/changelog.md index 047b00f..53c06c7 100644 --- a/changelog.md +++ b/changelog.md @@ -59,4 +59,11 @@ version 0.3.0 version 0.3.1 ======== -- Fixed error in `Linguini.Bundle` that prevented term and scope arguments from coexisting. @adcdefg30 \ No newline at end of file +- Fixed error in `Linguini.Bundle` that prevented term and scope arguments from coexisting. @adcdefg30 + +version 0.3.2 +======== + +- Adds `LinguiniBundle.HasAttrMessage` method +- Obsoletes `LinguiniBundle.TryGetAttrMsg` for `LinguiniBundle.TryGetAttrMessage` +- Obsoletes `LinguiniBundle.TryGetMsg` for `LinguiniBundle.TryGetMessage` \ No newline at end of file