From 008d0af1a78eb65eb92ecd94fbca4ad6259f295d Mon Sep 17 00:00:00 2001 From: Constantine Nathanson Date: Mon, 26 Oct 2020 14:52:11 -0400 Subject: [PATCH] Add support for list value in metadata --- Shared.Tests/Shared.Tests.projitems | 1 + Shared.Tests/Util/StringDictionaryTest.cs | 31 +++++++++++++++++++++++ Shared/StringDictionary.cs | 13 +++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Shared.Tests/Util/StringDictionaryTest.cs diff --git a/Shared.Tests/Shared.Tests.projitems b/Shared.Tests/Shared.Tests.projitems index 5b2c8bb8..061d138e 100644 --- a/Shared.Tests/Shared.Tests.projitems +++ b/Shared.Tests/Shared.Tests.projitems @@ -32,6 +32,7 @@ + diff --git a/Shared.Tests/Util/StringDictionaryTest.cs b/Shared.Tests/Util/StringDictionaryTest.cs new file mode 100644 index 00000000..923b4e4f --- /dev/null +++ b/Shared.Tests/Util/StringDictionaryTest.cs @@ -0,0 +1,31 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; + +namespace CloudinaryDotNet.Test.Util +{ + [TestFixture] + public class StringDictionaryTest + { + [Test] + public void TestStringDictionaryAddListValue() + { + var sd = new StringDictionary(); + + sd.Add("k1", new List{"v11", "v12"}); + sd.Add("k2", new List{"v21", "v22"}); + + Assert.AreEqual("k1=[\"v11\",\"v12\"]|k2=[\"v21\",\"v22\"]", Utils.SafeJoin("|", sd.SafePairs)); + } + + [Test] + public void TestStringDictionaryAddListValueSpecialCharacters() + { + var sd = new StringDictionary(); + + sd.Add("k1", new List{"v11|=\"'!@#$%^*({}[]"}); + + Assert.AreEqual(@"k1=[""v11\|\=\""'!@#$%^*({}[]""]", Utils.SafeJoin("|", sd.SafePairs)); + } + } +} diff --git a/Shared/StringDictionary.cs b/Shared/StringDictionary.cs index 6d2bfc7b..9e7ae313 100644 --- a/Shared/StringDictionary.cs +++ b/Shared/StringDictionary.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; + using Newtonsoft.Json; /// /// This class is based on list so is very slow but allows not unique keys. @@ -31,7 +32,7 @@ public StringDictionary(params string[] keyValuePairs) var firstEq = pair.IndexOf('='); if (firstEq == -1) { - Add(pair, null); + Add(pair, null as string); } else { @@ -160,6 +161,16 @@ public void Add(string key, string value) m_list.Add(newItem); } + /// + /// Add a new pair of key and a list of strings that is json encoded to a string value. + /// + /// The key to add. + /// The value to add. + public void Add(string key, List value) + { + Add(key, JsonConvert.SerializeObject(value)); + } + /// /// Removes the specified key. ///