Skip to content

Commit

Permalink
Merge branch 'stefanolsen-feature/optimize-resourcekeybuilder'
Browse files Browse the repository at this point in the history
  • Loading branch information
Valdis Iljuconoks committed Aug 4, 2024
2 parents 50b7e3e + 5ed2419 commit 787fafc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
14 changes: 9 additions & 5 deletions src/DbLocalizationProvider/Internal/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ namespace DbLocalizationProvider.Internal;

internal static class StringExtensions
{
internal static string JoinNonEmpty(this string target, string separator, string arg)
{
ArgumentNullException.ThrowIfNull(target);

return string.IsNullOrEmpty(arg) ? target : $"{target}{separator}{arg}";
}

internal static string JoinNonEmpty(this string target, string separator, params string[] args)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
ArgumentNullException.ThrowIfNull(target);

return string.Join(separator, new[] { target }.Union(args.Where(s => !string.IsNullOrEmpty(s)).ToArray()));
return string.Join(separator, new[] { target }.Union(args.Where(s => !string.IsNullOrEmpty(s))));
}
}
36 changes: 19 additions & 17 deletions src/DbLocalizationProvider/ResourceKeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public string BuildResourceKey(Type containerType, Stack<string> keyStack)
/// <returns>Full length resource key</returns>
public string BuildResourceKey(string keyPrefix, Attribute attribute)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
}
ArgumentNullException.ThrowIfNull(attribute);

var result = BuildResourceKey(keyPrefix, attribute.GetType());
if (attribute.GetType().IsAssignableFrom(typeof(DataTypeAttribute)))
Expand All @@ -87,10 +84,7 @@ public string BuildResourceKey(string keyPrefix, Attribute attribute)
/// <returns>Full length resource key</returns>
public string BuildResourceKey(string keyPrefix, Type attributeType)
{
if (attributeType == null)
{
throw new ArgumentNullException(nameof(attributeType));
}
ArgumentNullException.ThrowIfNull(attributeType);

if (!typeof(Attribute).IsAssignableFrom(attributeType))
{
Expand Down Expand Up @@ -133,22 +127,29 @@ public string BuildResourceKey(Type containerType, string memberName, Attribute
/// <returns>Full length resource key</returns>
public string BuildResourceKey(Type containerType, string memberName, string separator = ".")
{
var modelAttribute = containerType.GetCustomAttribute<LocalizedModelAttribute>();
var mi = containerType.GetMember(memberName).FirstOrDefault();

var prefix = string.Empty;

var modelAttribute = containerType.GetCustomAttribute<LocalizedModelAttribute>();
if (!string.IsNullOrEmpty(modelAttribute?.KeyPrefix))
{
prefix = modelAttribute.KeyPrefix;
}

var resourceAttributeOnClass = containerType.GetCustomAttribute<LocalizedResourceAttribute>();
if (!string.IsNullOrEmpty(resourceAttributeOnClass?.KeyPrefix))
if (string.IsNullOrEmpty(prefix))
{
prefix = resourceAttributeOnClass.KeyPrefix;
var resourceAttributeOnClass = containerType.GetCustomAttribute<LocalizedResourceAttribute>();
if (!string.IsNullOrEmpty(resourceAttributeOnClass?.KeyPrefix))
{
prefix = resourceAttributeOnClass.KeyPrefix;
}
}

var mi = string.IsNullOrEmpty(memberName)
? null
: containerType.GetMember(memberName,
MemberTypes.Field | MemberTypes.Property,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public).FirstOrDefault();

if (mi != null)
{
var resourceKeyAttributes = mi.GetCustomAttributes<ResourceKeyAttribute>().ToList();
Expand Down Expand Up @@ -178,7 +179,8 @@ public string BuildResourceKey(Type containerType, string memberName, string sep
return potentialResourceKey;
}

// 3. if not - then we scan through discovered and cached properties during initial scanning process and try to find on which type that property is declared
// 3. if not - then we scan through discovered and cached properties during initial scanning process
// and try to find on which type that property is declared
var declaringTypeName = FindPropertyDeclaringTypeName(containerType, memberName);

return declaringTypeName != null
Expand Down Expand Up @@ -206,7 +208,7 @@ public string BuildResourceKey(Type containerType)
return containerType.FullName;
}

private string FindPropertyDeclaringTypeName(Type containerType, string memberName)
private string? FindPropertyDeclaringTypeName(Type containerType, string memberName)
{
// make private copy
var currentContainerType = containerType;
Expand All @@ -219,7 +221,7 @@ private string FindPropertyDeclaringTypeName(Type containerType, string memberNa
}

var fullName = currentContainerType.FullName;
if (currentContainerType.IsGenericType && !currentContainerType.IsGenericTypeDefinition)
if (currentContainerType is { IsGenericType: true, IsGenericTypeDefinition: false })
{
fullName = currentContainerType.GetGenericTypeDefinition().FullName;
}
Expand Down

0 comments on commit 787fafc

Please sign in to comment.