Skip to content

Commit

Permalink
Actually just separate out the dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr committed Dec 23, 2023
1 parent 58cfba1 commit 506a05c
Showing 1 changed file with 62 additions and 59 deletions.
121 changes: 62 additions & 59 deletions Linguini.Bundle/FluentBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace Linguini.Bundle
public class FluentBundle
{
private IDictionary<string, FluentFunction> _funcList;
private IDictionary<(string, EntryKind), IEntry> _entries;
private IDictionary<string, AstTerm> _terms;
private IDictionary<string, AstMessage> _messages;

#region Properties
/// <summary>
Expand Down Expand Up @@ -67,7 +68,8 @@ public class FluentBundle

private FluentBundle()
{
_entries = new Dictionary<(string, EntryKind), IEntry>();
_terms = new Dictionary<string, AstTerm>();
_messages = new Dictionary<string, AstMessage>();
_funcList = new Dictionary<string, FluentFunction>();
Culture = CultureInfo.CurrentCulture;
Locales = new List<string>();
Expand All @@ -90,24 +92,28 @@ private static FluentBundle ConstructBundle(FluentBundleOption option)
: CultureInfo.CurrentCulture.Name;
var cultureInfo = new CultureInfo(primaryLocale, false);
var locales = new List<string> { primaryLocale };
IDictionary<(string, EntryKind), IEntry> entries;
IDictionary<string, AstTerm> terms;
IDictionary<string, AstMessage> messages;
IDictionary<string, FluentFunction> functions;
if (option.UseConcurrent)
{
entries = new ConcurrentDictionary<(string, EntryKind), IEntry>();
terms = new ConcurrentDictionary<string, AstTerm>();
messages = new ConcurrentDictionary<string, AstMessage>();
functions = new ConcurrentDictionary<string, FluentFunction>();
}
else
{
entries = new Dictionary<(string, EntryKind), IEntry>();
terms = new Dictionary<string, AstTerm>();
messages = new Dictionary<string, AstMessage>();
functions = new Dictionary<string, FluentFunction>();
}

return new FluentBundle
{
Culture = cultureInfo,
Locales = locales,
_entries = entries,
_terms = terms,
_messages = messages,
_funcList = functions,
TransformFunc = option.TransformFunc,
FormatterFunc = option.FormatterFunc,
Expand Down Expand Up @@ -148,10 +154,10 @@ internal bool AddResource(Resource res, out List<FluentError> errors)
switch (entry)
{
case AstMessage message:
AddEntry(errors, message);
AddMessage(errors, message);
break;
case AstTerm term:
AddEntry(errors, term);
AddTerm(errors, term);
break;
}
}
Expand All @@ -170,17 +176,26 @@ private void InternalResourceOverriding(Resource resource)
{
var entry = resource.Entries[entryPos];

if (entry is AstTerm or AstMessage)
switch (entry)
{
AddEntryOverriding(entry);
case AstMessage message:
AddMessageOverriding(message);
break;
case AstTerm term:
AddTermOverriding(term);
break;
}
}
}
private void AddEntryOverriding(IEntry term)

private void AddMessageOverriding(AstMessage message)
{
var id = (term.GetId(), term.ToKind());
_entries[id] = term;
_messages[message.GetId()] = message;
}

private void AddTermOverriding(AstTerm term)
{
_terms[term.GetId()] = term;
}

public void AddResourceOverriding(string input)
Expand All @@ -195,19 +210,22 @@ public void AddResourceOverriding(TextReader input)
InternalResourceOverriding(res);
}

private void AddEntry(List<FluentError> errors, IEntry term)
private void AddTerm(List<FluentError> errors, AstTerm term)
{
var id = (term.GetId(), term.ToKind());
if (_entries.ContainsKey(id))
{
errors.Add(new OverrideFluentError(id.Item1, id.Item2));
}
else
{
_entries[id] = term;
}
if (_terms.TryAdd(term.GetId(), term))
return;

errors.Add(new OverrideFluentError(term.GetId(), EntryKind.Term));
}


private void AddMessage(List<FluentError> errors, AstMessage msg)
{
if (_messages.TryAdd(msg.GetId(), msg))
return;

errors.Add(new OverrideFluentError(msg.GetId(), EntryKind.Message));
}

public bool TryAddFunction(string funcName, ExternalFunction fluentFunction)
{
return TryInsert(funcName, fluentFunction, InsertBehavior.None);
Expand Down Expand Up @@ -276,9 +294,7 @@ private bool TryInsert(string funcName, ExternalFunction fluentFunction,
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasMessage(string identifier)
{
var id = (identifier, EntryKind.Message);
return _entries.TryGetValue(id, out var message)
&& message is AstMessage;
return _messages.ContainsKey(identifier);
}

public bool HasAttrMessage(string idWithAttr)
Expand Down Expand Up @@ -374,30 +390,12 @@ public bool TryGetMessage(string id, string? attribute, FluentArgs? args,

public bool TryGetAstMessage(string ident, [NotNullWhen(true)] out AstMessage? message)
{
var id = (ident, EntryKind.Message);
if (_entries.TryGetValue(id, out var value)
&& value is AstMessage astMessage)
{
message = astMessage;
return true;
}

message = null;
return false;
return _messages.TryGetValue(ident, out message);
}

public bool TryGetAstTerm(string ident, [NotNullWhen(true)] out AstTerm? term)
{
var termId = (ident, EntryKind.Term);
if (_entries.TryGetValue(termId, out var value)
&& value is AstTerm astTerm)
{
term = astTerm;
return true;
}

term = null;
return false;
return _terms.TryGetValue(ident, out term);
}

public bool TryGetFunction(Identifier id, [NotNullWhen(true)] out FluentFunction? function)
Expand All @@ -407,7 +405,13 @@ public bool TryGetFunction(Identifier id, [NotNullWhen(true)] out FluentFunction

public bool TryGetFunction(string funcName, [NotNullWhen(true)] out FluentFunction? function)
{
return _funcList.TryGetValue(funcName, out function);
if (_funcList.ContainsKey(funcName))
{
return _funcList.TryGetValue(funcName, out function);
}

function = null;
return false;
}

#endregion
Expand All @@ -423,19 +427,17 @@ public string FormatPattern(Pattern pattern, FluentArgs? args,

public IEnumerable<string> GetMessageEnumerable()
{
foreach (var keyValue in _entries)
{
if (keyValue.Value.ToKind() == EntryKind.Message)
yield return keyValue.Key.Item1;
}
return _messages.Keys;
}

public IEnumerable<string> GetFuncEnumerable()
{
foreach (var keyValue in _funcList)
{
yield return keyValue.Key;
}
return _funcList.Keys;
}

public IEnumerable<string> GetTermEnumerable()
{
return _terms.Keys;
}

public FluentBundle DeepClone()
Expand All @@ -445,7 +447,8 @@ public FluentBundle DeepClone()
Culture = (CultureInfo)Culture.Clone(),
FormatterFunc = FormatterFunc,
Locales = new List<string>(Locales),
_entries = new Dictionary<(string, EntryKind), IEntry>(_entries),
_messages = new Dictionary<string, AstMessage>(_messages),
_terms = new Dictionary<string, AstTerm>(_terms),
_funcList = new Dictionary<string, FluentFunction>(_funcList),
TransformFunc = TransformFunc,
UseIsolating = UseIsolating,
Expand Down

0 comments on commit 506a05c

Please sign in to comment.