From fe956f340c398fc769f37595b7360986dd3d2d8e Mon Sep 17 00:00:00 2001 From: ta264 Date: Wed, 4 Aug 2021 21:42:40 +0100 Subject: [PATCH] Unused (cherry picked from commit d348232e0d43c0b6e1a5d22bb7b86c7c709e9acc) --- src/Lidarr.Http/Mapping/MappingValidation.cs | 54 ----- .../Mapping/ResourceMappingException.cs | 14 -- .../Expansive/CircularReferenceException.cs | 12 - src/NzbDrone.Common/Expansive/Expansive.cs | 213 ------------------ src/NzbDrone.Common/Expansive/PatternStyle.cs | 12 - src/NzbDrone.Common/Expansive/Tree.cs | 11 - src/NzbDrone.Common/Expansive/TreeNode.cs | 94 -------- src/NzbDrone.Common/Expansive/TreeNodeList.cs | 31 --- src/NzbDrone.Common/Expansive/license.txt | 2 - .../Instrumentation/LogEventExtensions.cs | 37 --- 10 files changed, 480 deletions(-) delete mode 100644 src/Lidarr.Http/Mapping/MappingValidation.cs delete mode 100644 src/Lidarr.Http/Mapping/ResourceMappingException.cs delete mode 100644 src/NzbDrone.Common/Expansive/CircularReferenceException.cs delete mode 100644 src/NzbDrone.Common/Expansive/Expansive.cs delete mode 100644 src/NzbDrone.Common/Expansive/PatternStyle.cs delete mode 100644 src/NzbDrone.Common/Expansive/Tree.cs delete mode 100644 src/NzbDrone.Common/Expansive/TreeNode.cs delete mode 100644 src/NzbDrone.Common/Expansive/TreeNodeList.cs delete mode 100644 src/NzbDrone.Common/Expansive/license.txt delete mode 100644 src/NzbDrone.Common/Instrumentation/LogEventExtensions.cs diff --git a/src/Lidarr.Http/Mapping/MappingValidation.cs b/src/Lidarr.Http/Mapping/MappingValidation.cs deleted file mode 100644 index af5a1da85..000000000 --- a/src/Lidarr.Http/Mapping/MappingValidation.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Linq; -using System.Reflection; -using Lidarr.Http.REST; -using NzbDrone.Common.Reflection; - -namespace Lidarr.Http.Mapping -{ - public static class MappingValidation - { - public static void ValidateMapping(Type modelType, Type resourceType) - { - var errors = modelType.GetSimpleProperties().Where(c => !c.GetGetMethod().IsStatic).Select(p => GetError(resourceType, p)).Where(c => c != null).ToList(); - - if (errors.Any()) - { - throw new ResourceMappingException(errors); - } - - PrintExtraProperties(modelType, resourceType); - } - - private static void PrintExtraProperties(Type modelType, Type resourceType) - { - var resourceBaseProperties = typeof(RestResource).GetProperties().Select(c => c.Name); - var resourceProperties = resourceType.GetProperties().Select(c => c.Name).Except(resourceBaseProperties); - var modelProperties = modelType.GetProperties().Select(c => c.Name); - - var extra = resourceProperties.Except(modelProperties); - - foreach (var extraProp in extra) - { - Console.WriteLine("Extra: [{0}]", extraProp); - } - } - - private static string GetError(Type resourceType, PropertyInfo modelProperty) - { - var resourceProperty = resourceType.GetProperties().FirstOrDefault(c => c.Name == modelProperty.Name); - - if (resourceProperty == null) - { - return string.Format("public {0} {1} {{ get; set; }}", modelProperty.PropertyType.Name, modelProperty.Name); - } - - if (resourceProperty.PropertyType != modelProperty.PropertyType && !typeof(RestResource).IsAssignableFrom(resourceProperty.PropertyType)) - { - return string.Format("Expected {0}.{1} to have type of {2} but found {3}", resourceType.Name, resourceProperty.Name, modelProperty.PropertyType, resourceProperty.PropertyType); - } - - return null; - } - } -} diff --git a/src/Lidarr.Http/Mapping/ResourceMappingException.cs b/src/Lidarr.Http/Mapping/ResourceMappingException.cs deleted file mode 100644 index aace10ac4..000000000 --- a/src/Lidarr.Http/Mapping/ResourceMappingException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Lidarr.Http.Mapping -{ - public class ResourceMappingException : ApplicationException - { - public ResourceMappingException(IEnumerable error) - : base(Environment.NewLine + string.Join(Environment.NewLine, error.OrderBy(c => c))) - { - } - } -} diff --git a/src/NzbDrone.Common/Expansive/CircularReferenceException.cs b/src/NzbDrone.Common/Expansive/CircularReferenceException.cs deleted file mode 100644 index 737663c54..000000000 --- a/src/NzbDrone.Common/Expansive/CircularReferenceException.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace NzbDrone.Common.Expansive -{ - public class CircularReferenceException : Exception - { - public CircularReferenceException(string message) - : base(message) - { - } - } -} diff --git a/src/NzbDrone.Common/Expansive/Expansive.cs b/src/NzbDrone.Common/Expansive/Expansive.cs deleted file mode 100644 index f6a2ff3c5..000000000 --- a/src/NzbDrone.Common/Expansive/Expansive.cs +++ /dev/null @@ -1,213 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Dynamic; -using System.Linq; -using System.Text.RegularExpressions; - -namespace NzbDrone.Common.Expansive -{ - public static class Expansive - { - private static PatternStyle _patternStyle; - - public static bool RequireAllExpansions { get; set; } - - public static Func DefaultExpansionFactory { get; set; } - - static Expansive() - { - Initialize(); - } - - public static string Expand(this string source) - { - return source.Expand(DefaultExpansionFactory); - } - - public static string Expand(this string source, params string[] args) - { - var output = source; - var tokens = new List(); - var pattern = new Regex(_patternStyle.TokenMatchPattern, RegexOptions.IgnoreCase); - var calls = new Stack(); - string callingToken = null; - - while (pattern.IsMatch(output)) - { - foreach (Match match in pattern.Matches(output)) - { - var token = _patternStyle.TokenReplaceFilter(match.Value); - var tokenIndex = 0; - if (!tokens.Contains(token)) - { - tokens.Add(token); - tokenIndex = tokens.Count - 1; - } - else - { - tokenIndex = tokens.IndexOf(token); - } - - output = Regex.Replace(output, _patternStyle.OutputFilter(match.Value), "{" + tokenIndex + "}"); - } - } - - var newArgs = new List(); - foreach (var arg in args) - { - var newArg = arg; - var tokenPattern = new Regex(_patternStyle.TokenFilter(string.Join("|", tokens))); - while (tokenPattern.IsMatch(newArg)) - { - foreach (Match match in tokenPattern.Matches(newArg)) - { - var token = _patternStyle.TokenReplaceFilter(match.Value); - if (calls.Contains(string.Format("{0}:{1}", callingToken, token))) - { - throw new CircularReferenceException(string.Format("Circular Reference Detected for token '{0}'.", callingToken)); - } - - calls.Push(string.Format("{0}:{1}", callingToken, token)); - callingToken = token; - newArg = Regex.Replace(newArg, _patternStyle.OutputFilter(match.Value), args[tokens.IndexOf(token)]); - } - } - - newArgs.Add(newArg); - } - - return string.Format(output, newArgs.ToArray()); - } - - public static string Expand(this string source, Func expansionFactory) - { - return source.ExpandInternal(expansionFactory); - } - - public static string Expand(this string source, object model) - { - return source.ExpandInternal( - name => - { - IDictionary modelDict = model.ToDictionary(); - if (RequireAllExpansions && !modelDict.ContainsKey(name)) - { - return ""; - } - - if (modelDict[name] == null) - { - return ""; - } - - return modelDict[name].ToString(); - }); - } - - private static void Initialize() - { - _patternStyle = new PatternStyle - { - TokenMatchPattern = @"\{[a-zA-Z]\w*\}", - TokenReplaceFilter = token => token.Replace("{", "").Replace("}", ""), - OutputFilter = output => (output.StartsWith("{") && output.EndsWith("}") ? output : @"\{" + output + @"\}"), - TokenFilter = tokens => "{(" + tokens + ")}" - }; - } - - private static string ExpandInternal(this string source, Func expansionFactory) - { - if (expansionFactory == null) - { - throw new ApplicationException("ExpansionFactory not defined.\nDefine a DefaultExpansionFactory or call Expand(source, Func expansionFactory))"); - } - - var pattern = new Regex(_patternStyle.TokenMatchPattern, RegexOptions.IgnoreCase); - - var callTreeParent = new Tree("root").Root; - - return source.Explode(pattern, _patternStyle, expansionFactory, callTreeParent); - } - - private static string Explode(this string source, Regex pattern, PatternStyle patternStyle, Func expansionFactory, TreeNode parent) - { - var output = source; - while (output.HasChildren(pattern)) - { - foreach (Match match in pattern.Matches(source)) - { - var child = match.Value; - var token = patternStyle.TokenReplaceFilter(match.Value); - - var thisNode = parent.Children.Add(token); - - // if we have already encountered this token in this call tree, we have a circular reference - if (thisNode.CallTree.Contains(token)) - { - throw new CircularReferenceException(string.Format("Circular Reference Detected for token '{0}'. Call Tree: {1}->{2}", - token, - string.Join("->", thisNode.CallTree.ToArray().Reverse()), - token)); - } - - // expand this match - var expandedValue = expansionFactory(token); - - // Replace the match with the expanded value - child = Regex.Replace(child, patternStyle.OutputFilter(match.Value), expandedValue); - - // Recursively expand the child until we no longer encounter nested tokens (or hit a circular reference) - child = child.Explode(pattern, patternStyle, expansionFactory, thisNode); - - // finally, replace the match in the output with the fully-expanded value - output = Regex.Replace(output, patternStyle.OutputFilter(match.Value), child); - } - } - - return output; - } - - private static bool HasChildren(this string token, Regex pattern) - { - return pattern.IsMatch(token); - } - - /// - /// Turns the object into an ExpandoObject - /// - private static dynamic ToExpando(this object o) - { - var result = new ExpandoObject(); - var d = result as IDictionary; //work with the Expando as a Dictionary - if (o is ExpandoObject) - { - return o; //shouldn't have to... but just in case - } - - if (o is NameValueCollection || o.GetType().IsSubclassOf(typeof(NameValueCollection))) - { - var nv = (NameValueCollection)o; - nv.Cast().Select(key => new KeyValuePair(key, nv[key])).ToList().ForEach(i => d.Add(i)); - } - else - { - var props = o.GetType().GetProperties(); - foreach (var item in props) - { - d.Add(item.Name, item.GetValue(o, null)); - } - } - - return result; - } - - /// - /// Turns the object into a Dictionary - /// - private static IDictionary ToDictionary(this object thingy) - { - return (IDictionary)thingy.ToExpando(); - } - } -} diff --git a/src/NzbDrone.Common/Expansive/PatternStyle.cs b/src/NzbDrone.Common/Expansive/PatternStyle.cs deleted file mode 100644 index 011e77315..000000000 --- a/src/NzbDrone.Common/Expansive/PatternStyle.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace NzbDrone.Common.Expansive -{ - internal class PatternStyle - { - public string TokenMatchPattern { get; set; } - public Func TokenFilter { get; set; } - public Func TokenReplaceFilter { get; set; } - public Func OutputFilter { get; set; } - } -} diff --git a/src/NzbDrone.Common/Expansive/Tree.cs b/src/NzbDrone.Common/Expansive/Tree.cs deleted file mode 100644 index 744c0714e..000000000 --- a/src/NzbDrone.Common/Expansive/Tree.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace NzbDrone.Common.Expansive -{ - internal class Tree : TreeNode - { - public Tree(T rootValue) - : base(rootValue) - { - Value = rootValue; - } - } -} diff --git a/src/NzbDrone.Common/Expansive/TreeNode.cs b/src/NzbDrone.Common/Expansive/TreeNode.cs deleted file mode 100644 index 5f44d1277..000000000 --- a/src/NzbDrone.Common/Expansive/TreeNode.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Collections.Generic; - -namespace NzbDrone.Common.Expansive -{ - internal class TreeNode - { - private List _CallTree; - private TreeNode _Parent; - - public TreeNode(T value) - { - Value = value; - Parent = null; - Children = new TreeNodeList(this); - _CallTree = new List(); - } - - public TreeNode(T value, TreeNode parent) - { - Value = value; - Parent = parent; - Children = new TreeNodeList(this); - _CallTree = new List(); - } - - public TreeNode Parent - { - get - { - return _Parent; - } - - set - { - if (value == _Parent) - { - return; - } - - if (_Parent != null) - { - _Parent.Children.Remove(this); - } - - if (value != null && !value.Children.Contains(this)) - { - value.Children.Add(this); - } - - _Parent = value; - } - } - - public TreeNode Root - { - get - { - //return (Parent == null) ? this : Parent.Root; - TreeNode node = this; - while (node.Parent != null) - { - node = node.Parent; - } - - return node; - } - } - - public TreeNodeList Children { get; private set; } - - public List CallTree - { - get - { - _CallTree = new List(); - TreeNode node = this; - while (node.Parent != null) - { - node = node.Parent; - _CallTree.Add(node.Value); - } - - return _CallTree; - } - - private set - { - _CallTree = value; - } - } - - public T Value { get; set; } - } -} diff --git a/src/NzbDrone.Common/Expansive/TreeNodeList.cs b/src/NzbDrone.Common/Expansive/TreeNodeList.cs deleted file mode 100644 index a239a3b15..000000000 --- a/src/NzbDrone.Common/Expansive/TreeNodeList.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Collections.Generic; - -namespace NzbDrone.Common.Expansive -{ - internal class TreeNodeList : List> - { - public TreeNode Parent; - - public TreeNodeList(TreeNode parent) - { - Parent = parent; - } - - public new TreeNode Add(TreeNode node) - { - base.Add(node); - node.Parent = Parent; - return node; - } - - public TreeNode Add(T value) - { - return Add(new TreeNode(value)); - } - - public override string ToString() - { - return "Count=" + Count.ToString(); - } - } -} diff --git a/src/NzbDrone.Common/Expansive/license.txt b/src/NzbDrone.Common/Expansive/license.txt deleted file mode 100644 index b48f7ea2f..000000000 --- a/src/NzbDrone.Common/Expansive/license.txt +++ /dev/null @@ -1,2 +0,0 @@ -Source: https://github.com/anderly/Expansive -Microsoft Public License (MS-PL): http://opensource.org/licenses/MS-PL \ No newline at end of file diff --git a/src/NzbDrone.Common/Instrumentation/LogEventExtensions.cs b/src/NzbDrone.Common/Instrumentation/LogEventExtensions.cs deleted file mode 100644 index 2c8e7c8a0..000000000 --- a/src/NzbDrone.Common/Instrumentation/LogEventExtensions.cs +++ /dev/null @@ -1,37 +0,0 @@ -using NLog; -using NzbDrone.Common.Serializer; - -namespace NzbDrone.Common.Instrumentation -{ - public static class LogEventExtensions - { - public static string GetHash(this LogEventInfo logEvent) - { - var stackString = logEvent.StackTrace.ToJson(); - var hashSeed = string.Concat(logEvent.LoggerName, logEvent.Exception.GetType().ToString(), stackString, logEvent.Level); - return HashUtil.CalculateCrc(hashSeed); - } - - public static string GetFormattedMessage(this LogEventInfo logEvent) - { - var message = logEvent.FormattedMessage; - - if (logEvent.Exception != null) - { - if (logEvent.Exception != null) - { - if (string.IsNullOrWhiteSpace(message)) - { - message = logEvent.Exception.Message; - } - else - { - message += ": " + logEvent.Exception.Message; - } - } - } - - return message; - } - } -}