From 4ffa4bca6c1411b288f679018f742e8102bd926a Mon Sep 17 00:00:00 2001 From: glucaci Date: Wed, 7 Dec 2016 10:34:55 +0100 Subject: [PATCH] Formatting output responsability of command. Commands: disableoutput, enableoutput, outputs, tagtypes, update implemented. --- LibMpc/Commands/Commands.Database.cs | 16 +++++++------- LibMpc/Commands/Commands.Output.cs | 29 +++++++++++++++----------- LibMpc/Commands/Commands.Reflection.cs | 12 ++++++++--- LibMpc/Commands/IMpcCommand.cs | 11 +++++++++- LibMpc/Message/MpdMessage.cs | 13 +++--------- LibMpc/Message/MpdResponse.cs | 6 +++--- 6 files changed, 50 insertions(+), 37 deletions(-) diff --git a/LibMpc/Commands/Commands.Database.cs b/LibMpc/Commands/Commands.Database.cs index 9862681..a84e0bf 100644 --- a/LibMpc/Commands/Commands.Database.cs +++ b/LibMpc/Commands/Commands.Database.cs @@ -24,9 +24,9 @@ namespace LibMpc public string Value => string.Join(" ", "find", _tag.Value, _searchText); - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + public IDictionary FormatResponse(IList> response) { - return response; + return response.ToDefaultDictionary(); } } @@ -41,9 +41,9 @@ namespace LibMpc public string Value => string.Join(" ", "list", _tag); - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + public IDictionary FormatResponse(IList> response) { - return response; + return response.ToDefaultDictionary(); } } @@ -60,9 +60,9 @@ namespace LibMpc public string Value => string.Join(" ", "listall", _path); - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + public IDictionary FormatResponse(IList> response) { - return response; + return response.ToDefaultDictionary(); } } @@ -79,9 +79,9 @@ namespace LibMpc // TODO: Extend command: < update [URI] > public string Value => "update"; - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + public IDictionary FormatResponse(IList> response) { - return response; + return response.ToDefaultDictionary(); } } diff --git a/LibMpc/Commands/Commands.Output.cs b/LibMpc/Commands/Commands.Output.cs index 203f234..490428b 100644 --- a/LibMpc/Commands/Commands.Output.cs +++ b/LibMpc/Commands/Commands.Output.cs @@ -23,9 +23,9 @@ namespace LibMpc public string Value => string.Join(" ", "disableoutput", _outputId); - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + public IDictionary FormatResponse(IList> response) { - return response; + return response.ToDefaultDictionary(); } } @@ -43,9 +43,9 @@ namespace LibMpc public string Value => string.Join(" ", "enableoutput", _outputId); - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + public IDictionary FormatResponse(IList> response) { - return response; + return response.ToDefaultDictionary(); } } @@ -54,23 +54,28 @@ namespace LibMpc /// /// Shows information about all outputs. /// - public class Outputs : IMpcCommand> + public class Outputs : IMpcCommand>> { public string Value => "outputs"; - public IReadOnlyDictionary>> FormatResponse(IReadOnlyDictionary> response) + + public IDictionary>> FormatResponse(IList> response) { - var result = new Dictionary>> + var result = new Dictionary>> { - {"outputs", new List>()} + { "outputs", new List>() } }; - for (var i = 0; i < response["outputid"].Count; i++) + for (var i = 0; i < response.Count; i += 3) { + var outputId = response[i].Value; + var outputName = response[i + 1].Value; + var outputEnabled = response[i + 2].Value; + result["outputs"].Add(new Dictionary { - { "id", response["outputid"][i] }, - { "name", response["outputname"][i] }, - { "enabled", response["outputenabled"][i] } + {"id", outputId}, + {"name", outputName}, + {"enabled", outputEnabled} }); } diff --git a/LibMpc/Commands/Commands.Reflection.cs b/LibMpc/Commands/Commands.Reflection.cs index f05dc17..68ed3c5 100644 --- a/LibMpc/Commands/Commands.Reflection.cs +++ b/LibMpc/Commands/Commands.Reflection.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace LibMpc { @@ -13,13 +14,18 @@ namespace LibMpc // TODO: commands // TODO: notcommands - public class TagTypes : IMpcCommand + public class TagTypes : IMpcCommand> { public string Value => "tagtypes"; - public IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response) + IDictionary> IMpcCommand>.FormatResponse(IList> response) { - return response; + var result = new Dictionary> + { + { "tagtypes", response.Where(item => item.Key.Equals("tagtype")).Select(item => item.Value).ToList() } + }; + + return result; } } diff --git a/LibMpc/Commands/IMpcCommand.cs b/LibMpc/Commands/IMpcCommand.cs index 0db8d55..008b1f2 100644 --- a/LibMpc/Commands/IMpcCommand.cs +++ b/LibMpc/Commands/IMpcCommand.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; namespace LibMpc { @@ -6,6 +7,14 @@ namespace LibMpc { string Value { get; } - IReadOnlyDictionary> FormatResponse(IReadOnlyDictionary> response); + IDictionary FormatResponse(IList> response); + } + + internal static class MpdCommandExtensions + { + public static IDictionary ToDefaultDictionary(this IList> items) + { + return items.ToDictionary(item => item.Key, item => item.Value); + } } } \ No newline at end of file diff --git a/LibMpc/Message/MpdMessage.cs b/LibMpc/Message/MpdMessage.cs index 767c16a..3341d67 100644 --- a/LibMpc/Message/MpdMessage.cs +++ b/LibMpc/Message/MpdMessage.cs @@ -30,9 +30,9 @@ namespace LibMpc public IMpdRequest Request { get; } public IMpdResponse Response { get; } - private IReadOnlyDictionary> GetValuesFromResponse() + private IList> GetValuesFromResponse() { - var result = new Dictionary>(); + var result = new List>(); foreach (var line in _rawResponse) { @@ -45,14 +45,7 @@ namespace LibMpc var mpdValue = match.Result("${value}"); if (!string.IsNullOrEmpty(mpdValue)) { - if (!result.ContainsKey(mpdKey)) - { - result.Add(mpdKey, new List() { mpdValue }); - } - else - { - result[mpdKey].Add(mpdValue); - } + result.Add(new KeyValuePair(mpdKey, mpdValue)); } } } diff --git a/LibMpc/Message/MpdResponse.cs b/LibMpc/Message/MpdResponse.cs index 9ae53aa..9631d44 100644 --- a/LibMpc/Message/MpdResponse.cs +++ b/LibMpc/Message/MpdResponse.cs @@ -6,19 +6,19 @@ namespace LibMpc public interface IMpdResponse { IMpdResponseState State { get; } - IReadOnlyDictionary> Body { get; } + IDictionary Body { get; } } public class MpdResponse : IMpdResponse { - public MpdResponse(string endLine, IReadOnlyDictionary> body, bool connected) + public MpdResponse(string endLine, IDictionary body, bool connected) { State = new MpdResponseState(endLine, connected); Body = body; } public IMpdResponseState State { get; } - public IReadOnlyDictionary> Body { get; } + public IDictionary Body { get; } } public static class CheckNotNullExtension