Skip to content

Commit

Permalink
Minor refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ygg01 committed Jan 7, 2024
1 parent ebca66b commit f4df5d0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Linguini.Bundle.Test/Yaml/YamlSuiteParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using Linguini.Bundle.Builder;
using Linguini.Bundle.Errors;
using Linguini.Bundle.Func;
using Linguini.Bundle.Function;
using Linguini.Bundle.Types;
using NUnit.Framework;
using YamlDotNet.RepresentationModel;
Expand Down
31 changes: 20 additions & 11 deletions Linguini.Bundle/ConcurrentBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Linguini.Bundle.Errors;
using Linguini.Bundle.Types;
using Linguini.Shared.Types.Bundle;
using Linguini.Syntax.Ast;

// ReSharper disable UnusedType.Global
Expand All @@ -13,7 +15,7 @@ namespace Linguini.Bundle
{
public sealed class ConcurrentBundle : FluentBundle
{
internal ConcurrentDictionary<string, FluentFunction> _funcList = new();
internal ConcurrentDictionary<string, FluentFunction> FuncList = new();
private ConcurrentDictionary<string, AstTerm> _terms = new();
private ConcurrentDictionary<string, AstMessage> _messages = new();

Expand Down Expand Up @@ -51,19 +53,19 @@ protected override bool TryAddMessage(AstMessage message, List<FluentError>? err
/// <inheritdoc />
public override bool TryAddFunction(string funcName, ExternalFunction fluentFunction)
{
return _funcList.TryAdd(funcName, fluentFunction);
return FuncList.TryAdd(funcName, fluentFunction);
}

/// <inheritdoc />
public override void AddFunctionOverriding(string funcName, ExternalFunction fluentFunction)
{
_funcList[funcName] = fluentFunction;
FuncList[funcName] = fluentFunction;
}

/// <inheritdoc />
public override void AddFunctionUnchecked(string funcName, ExternalFunction fluentFunction)
{
if (_funcList.TryAdd(funcName, fluentFunction)) return;
if (FuncList.TryAdd(funcName, fluentFunction)) return;
throw new ArgumentException($"Function with name {funcName} already exist");
}

Expand All @@ -89,7 +91,7 @@ public override bool TryGetAstTerm(string ident, [NotNullWhen(true)] out AstTerm
/// <inheritdoc />
public override bool TryGetFunction(string funcName, [NotNullWhen(true)] out FluentFunction? function)
{
return _funcList.TryGetValue(funcName, out function);
return FuncList.TryGetValue(funcName, out function);
}

/// <inheritdoc />
Expand All @@ -101,25 +103,32 @@ public override IEnumerable<string> GetMessageEnumerable()
/// <inheritdoc />
public override IEnumerable<string> GetFuncEnumerable()
{
return _funcList.Keys.ToArray();
return FuncList.Keys.ToArray();
}

/// <inheritdoc />
public override IEnumerable<string> GetTermEnumerable()
{
return _terms.Keys.ToArray();
}


/// <inheritdoc />

/// <inheritdoc/>
public override FluentBundle DeepClone()
{
return new ConcurrentBundle()
return new ConcurrentBundle
{
_funcList = new ConcurrentDictionary<string, FluentFunction>(_funcList),
FuncList = new ConcurrentDictionary<string, FluentFunction>(FuncList),
_terms = new ConcurrentDictionary<string, AstTerm>(_terms),
_messages = new ConcurrentDictionary<string, AstMessage>(_messages),
Culture = (CultureInfo) Culture.Clone(),
Locales = new List<string>(Locales),
UseIsolating = UseIsolating,
TransformFunc = (Func<string, string>?)TransformFunc?.Clone(),
FormatterFunc = (Func<IFluentType, string>?)FormatterFunc?.Clone(),
MaxPlaceable = MaxPlaceable,
EnableExtensions = EnableExtensions,
};
}

}
}
18 changes: 11 additions & 7 deletions Linguini.Bundle/FluentBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml.Schema;
using Linguini.Bundle.Builder;
using Linguini.Bundle.Errors;
using Linguini.Bundle.Resolver;
Expand All @@ -18,7 +17,6 @@ namespace Linguini.Bundle
{
public abstract class FluentBundle
{

/// <summary>
/// <see cref="CultureInfo"/> of the bundle. Primary bundle locale
/// </summary>
Expand Down Expand Up @@ -48,7 +46,7 @@ public abstract class FluentBundle
/// <summary>
/// Limit of placeable <see cref="AstTerm"/> within one <see cref="Pattern"/>, when fully expanded (all nested elements count towards it). Useful for preventing billion laughs attack. Defaults to 100.
/// </summary>
public byte MaxPlaceable { get; private init; } = 100;
public byte MaxPlaceable { get; internal init; } = 100;

/// <summary>
/// Whether experimental features are enabled.
Expand All @@ -60,7 +58,8 @@ public abstract class FluentBundle
/// <item>term reference as parameters</item>
/// </list>
/// </summary>
public bool EnableExtensions { get; init; } = false;
// ReSharper disable once MemberCanBeProtected.Global
public bool EnableExtensions { get; init; }

public bool AddResource(string input, [NotNullWhen(false)] out List<FluentError>? errors)
{
Expand Down Expand Up @@ -350,6 +349,11 @@ public string FormatPattern(Pattern pattern, IDictionary<string, IFluentType>? a
/// <returns>A new instance of the AbstractFluentBundle class that is a deep clone of the current instance.</returns>
public abstract FluentBundle DeepClone();

/// <summary>
/// Creates a FluentBundle object with the specified options.
/// </summary>
/// <param name="option">The FluentBundleOption object that contains the options for creating the FluentBundle</param>
/// <returns>A FluentBundle object created with the specified options</returns>
public static FluentBundle MakeUnchecked(FluentBundleOption option)
{
var primaryLocale = option.Locales.Count > 0
Expand All @@ -368,9 +372,9 @@ public static FluentBundle MakeUnchecked(FluentBundleOption option)
TransformFunc = option.TransformFunc,
MaxPlaceable = option.MaxPlaceable,
UseIsolating = option.UseIsolating,
_funcList = new ConcurrentDictionary<string, FluentFunction>(func),
FuncList = new ConcurrentDictionary<string, FluentFunction>(func),
},
_ => new NonConcurrentBundle()
_ => new NonConcurrentBundle
{
Locales = option.Locales,
Culture = cultureInfo,
Expand All @@ -379,7 +383,7 @@ public static FluentBundle MakeUnchecked(FluentBundleOption option)
TransformFunc = option.TransformFunc,
MaxPlaceable = option.MaxPlaceable,
UseIsolating = option.UseIsolating,
_funcList = func,
FuncList = func,
}
};
}
Expand Down
7 changes: 7 additions & 0 deletions Linguini.Bundle/FrozenBundle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Linguini.Bundle
{
public class FrozenBundle
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Linguini.Bundle.Types;
using Linguini.Shared.Types.Bundle;

namespace Linguini.Bundle.Func
namespace Linguini.Bundle.Function
{
public static class LinguiniFluentFunctions
{
Expand Down
30 changes: 20 additions & 10 deletions Linguini.Bundle/NonConcurrentBundle.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Linguini.Bundle.Errors;
using Linguini.Bundle.Types;
using Linguini.Shared.Types.Bundle;
using Linguini.Syntax.Ast;

// ReSharper disable UnusedType.Global
Expand All @@ -11,9 +14,9 @@ namespace Linguini.Bundle
{
public sealed class NonConcurrentBundle : FluentBundle
{
internal Dictionary<string, FluentFunction> _funcList = new();
internal Dictionary<string, AstTerm> _terms = new();
internal Dictionary<string, AstMessage> _messages = new();
internal Dictionary<string, FluentFunction> FuncList = new();
private Dictionary<string, AstTerm> _terms = new();
private Dictionary<string, AstMessage> _messages = new();

/// <inheritdoc />
protected override void AddMessageOverriding(AstMessage message)
Expand Down Expand Up @@ -49,19 +52,19 @@ protected override bool TryAddMessage(AstMessage message, List<FluentError>? err
/// <inheritdoc />
public override bool TryAddFunction(string funcName, ExternalFunction fluentFunction)
{
return _funcList.TryAdd(funcName, fluentFunction);
return FuncList.TryAdd(funcName, fluentFunction);
}

/// <inheritdoc />
public override void AddFunctionOverriding(string funcName, ExternalFunction fluentFunction)
{
_funcList[funcName] = fluentFunction;
FuncList[funcName] = fluentFunction;
}

/// <inheritdoc />
public override void AddFunctionUnchecked(string funcName, ExternalFunction fluentFunction)
{
_funcList.Add(funcName, fluentFunction);
FuncList.Add(funcName, fluentFunction);
}

/// <inheritdoc />
Expand All @@ -86,7 +89,7 @@ public override bool TryGetAstTerm(string ident, [NotNullWhen(true)] out AstTerm
/// <inheritdoc />
public override bool TryGetFunction(string funcName, [NotNullWhen(true)] out FluentFunction? function)
{
return _funcList.TryGetValue(funcName, out function);
return FuncList.TryGetValue(funcName, out function);
}

/// <inheritdoc />
Expand All @@ -98,7 +101,7 @@ public override IEnumerable<string> GetMessageEnumerable()
/// <inheritdoc />
public override IEnumerable<string> GetFuncEnumerable()
{
return _funcList.Keys.ToArray();
return FuncList.Keys.ToArray();
}

/// <inheritdoc />
Expand All @@ -113,9 +116,16 @@ public override FluentBundle DeepClone()
{
return new NonConcurrentBundle()
{
_funcList = new Dictionary<string, FluentFunction>(_funcList),
FuncList = new Dictionary<string, FluentFunction>(FuncList),
_terms = new Dictionary<string, AstTerm>(_terms),
_messages = new Dictionary<string, AstMessage>(_messages),
Culture = (CultureInfo) Culture.Clone(),
Locales = new List<string>(Locales),
UseIsolating = UseIsolating,
TransformFunc = (Func<string, string>?)TransformFunc?.Clone(),
FormatterFunc = (Func<IFluentType, string>?)FormatterFunc?.Clone(),
MaxPlaceable = MaxPlaceable,
EnableExtensions = EnableExtensions,
};
}
}
Expand Down

0 comments on commit f4df5d0

Please sign in to comment.