Add SequenceReference support for dictionaries.
This commit is contained in:
@@ -15,12 +15,14 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using OpenRA.Support;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Lint
|
||||
{
|
||||
public class LintExts
|
||||
{
|
||||
public static IEnumerable<string> GetFieldValues(object ruleInfo, FieldInfo fieldInfo, Action<string> emitError)
|
||||
public static IEnumerable<string> GetFieldValues(object ruleInfo, FieldInfo fieldInfo, Action<string> emitError,
|
||||
LintDictionaryReference dictionaryReference = LintDictionaryReference.None)
|
||||
{
|
||||
var type = fieldInfo.FieldType;
|
||||
if (type == typeof(string))
|
||||
@@ -35,11 +37,38 @@ namespace OpenRA.Mods.Common.Lint
|
||||
return expr != null ? expr.Variables : Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: string, IEnumerable<string>, BooleanExpression, IntegerExpression"
|
||||
.F(ruleInfo.GetType().Name, fieldInfo.Name));
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||
{
|
||||
// Use an intermediate list to cover the unlikely case where both keys and values are lintable
|
||||
var dictionaryValues = new List<string>();
|
||||
if (dictionaryReference.HasFlag(LintDictionaryReference.Keys) && type.GenericTypeArguments[0] == typeof(string))
|
||||
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)fieldInfo.GetValue(ruleInfo)).Keys);
|
||||
|
||||
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(string))
|
||||
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)fieldInfo.GetValue(ruleInfo)).Values);
|
||||
|
||||
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(IEnumerable<string>))
|
||||
foreach (var row in (IEnumerable<IEnumerable<string>>)((IDictionary)fieldInfo.GetValue(ruleInfo)).Values)
|
||||
dictionaryValues.AddRange(row);
|
||||
|
||||
return dictionaryValues;
|
||||
}
|
||||
|
||||
var supportedTypes = new[]
|
||||
{
|
||||
"string", "IEnumerable<string>",
|
||||
"Dictionary<string, T> (LintDictionaryReference.Keys)",
|
||||
"Dictionary<T, string> (LintDictionaryReference.Values)",
|
||||
"Dictionary<T, IEnumerable<string>> (LintDictionaryReference.Values)",
|
||||
"BooleanExpression", "IntegerExpression"
|
||||
};
|
||||
|
||||
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: {2}"
|
||||
.F(ruleInfo.GetType().Name, fieldInfo.Name, supportedTypes.JoinWith(", ")));
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetPropertyValues(object ruleInfo, PropertyInfo propertyInfo, Action<string> emitError)
|
||||
public static IEnumerable<string> GetPropertyValues(object ruleInfo, PropertyInfo propertyInfo, Action<string> emitError,
|
||||
LintDictionaryReference dictionaryReference = LintDictionaryReference.None)
|
||||
{
|
||||
var type = propertyInfo.PropertyType;
|
||||
if (type == typeof(string))
|
||||
@@ -54,8 +83,34 @@ namespace OpenRA.Mods.Common.Lint
|
||||
return expr != null ? expr.Variables : Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: string, IEnumerable<string>, BooleanExpression, IntegerExpression"
|
||||
.F(ruleInfo.GetType().Name, propertyInfo.Name));
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||
{
|
||||
// Use an intermediate list to cover the unlikely case where both keys and values are lintable
|
||||
var dictionaryValues = new List<string>();
|
||||
if (dictionaryReference.HasFlag(LintDictionaryReference.Keys) && type.GenericTypeArguments[0] == typeof(string))
|
||||
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)propertyInfo.GetValue(ruleInfo)).Keys);
|
||||
|
||||
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(string))
|
||||
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)propertyInfo.GetValue(ruleInfo)).Values);
|
||||
|
||||
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(IEnumerable<string>))
|
||||
foreach (var row in (IEnumerable<IEnumerable<string>>)((IDictionary)propertyInfo.GetValue(ruleInfo)).Values)
|
||||
dictionaryValues.AddRange(row);
|
||||
|
||||
return dictionaryValues;
|
||||
}
|
||||
|
||||
var supportedTypes = new[]
|
||||
{
|
||||
"string", "IEnumerable<string>",
|
||||
"Dictionary<string, T> (LintDictionaryReference.Keys)",
|
||||
"Dictionary<T, string> (LintDictionaryReference.Values)",
|
||||
"Dictionary<T, IEnumerable<string>> (LintDictionaryReference.Values)",
|
||||
"BooleanExpression", "IntegerExpression"
|
||||
};
|
||||
|
||||
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: {2}"
|
||||
.F(ruleInfo.GetType().Name, propertyInfo.Name, supportedTypes.JoinWith(", ")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user