Skip to content

Commit

Permalink
Merge pull request #219 from leekelleher/dev/3.3.1
Browse files Browse the repository at this point in the history
Preparing v3.3.1 release
  • Loading branch information
leekelleher authored May 11, 2022
2 parents 30745e9 + c1aab05 commit 5048636
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.0
3.3.1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand All @@ -29,7 +30,7 @@ namespace Umbraco.Community.Contentment.DataEditors
{
public sealed class EnumDataListSource : IDataListSource, IDataListSourceValueConverter
{
private readonly Dictionary<Type, (List<DataListItem>, Dictionary<string, object>)> _lookup;
private readonly ConcurrentDictionary<Type, (List<DataListItem>, Dictionary<string, object>)> _lookup;
private readonly IIOHelper _ioHelper;
private readonly IShortStringHelper _shortStringHelper;

Expand All @@ -48,7 +49,7 @@ public EnumDataListSource(
IShortStringHelper shortStringHelper,
IIOHelper ioHelper)
{
_lookup = new Dictionary<Type, (List<DataListItem>, Dictionary<string, object>)>();
_lookup = new ConcurrentDictionary<Type, (List<DataListItem>, Dictionary<string, object>)>();

_logger = logger;
_shortStringHelper = shortStringHelper;
Expand Down Expand Up @@ -94,13 +95,8 @@ public EnumDataListSource(
}
};

public void PopulateLookup(Type type)
private (List<DataListItem>, Dictionary<string, object>) EnumValueFactory(Type type)
{
if (_lookup.ContainsKey(type) == true)
{
return;
}

var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

var items = new List<DataListItem>(fields.Length);
Expand Down Expand Up @@ -130,19 +126,26 @@ public void PopulateLookup(Type type)
values.Add(value, Enum.Parse(type, field.Name));
}

_lookup.Add(type, (items, values));
return (items, values);
}

public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)
{
var type = GetValueType(config);

if (type == null || _lookup.ContainsKey(type) == false)
if (type == null)
{
return Enumerable.Empty<DataListItem>();
}

var items = _lookup[type].Item1;
var entry = _lookup.GetOrAdd(type, EnumValueFactory);

if (entry.Item1 == null)
{
return Enumerable.Empty<DataListItem>();
}

var items = entry.Item1;

if (config.TryGetValueAs("sortAlphabetically", out bool boolean) == true && boolean == true)
{
Expand Down Expand Up @@ -191,8 +194,6 @@ public Type GetValueType(Dictionary<string, object> config)

if (type != null && type.IsEnum == true)
{
PopulateLookup(type);

return type;
}
}
Expand All @@ -206,8 +207,8 @@ public object ConvertValue(Type type, string value)
{
if (string.IsNullOrWhiteSpace(value) == false && type?.IsEnum == true)
{
if (_lookup.TryGetValue(type, out var enumLookup) == true &&
enumLookup.Item2.TryGetValue(value, out var enumValue) == true)
var entry = _lookup.GetOrAdd(type, EnumValueFactory);
if (entry.Item2 != null && entry.Item2.TryGetValue(value, out var enumValue) == true)
{
return enumValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Title>Contentment for Umbraco</Title>
<Description>Contentment, a collection of components for Umbraco.</Description>
<PackageTags>umbraco</PackageTags>
<Version>3.3.0</Version>
<Version>3.3.1</Version>
<Company>Umbrella Inc Ltd</Company>
<Authors>Lee Kelleher</Authors>
<Copyright>2019 © Lee Kelleher</Copyright>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Contentment",
"version": "3.3.0",
"version": "3.3.1",
"css": [ "~/App_Plugins/Contentment/contentment.css" ],
"javascript": [ "~/App_Plugins/Contentment/contentment.js" ]
}

0 comments on commit 5048636

Please sign in to comment.