Skip to content

Commit

Permalink
Add support for list value in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Oct 27, 2020
1 parent 7962768 commit 008d0af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions Shared.Tests/Shared.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Asset\SignatureTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Transformations\Common\TransformationTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Asset\UrlSuffixTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\StringDictionaryTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\UtilsTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Transformations\Video\VideoCodecTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tag\VideoTagTest.cs" />
Expand Down
31 changes: 31 additions & 0 deletions Shared.Tests/Util/StringDictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -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<string>{"v11", "v12"});
sd.Add("k2", new List<string>{"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<string>{"v11|=\"'!@#$%^*({}[]"});

Assert.AreEqual(@"k1=[""v11\|\=\""'!@#$%^*({}[]""]", Utils.SafeJoin("|", sd.SafePairs));
}
}
}
13 changes: 12 additions & 1 deletion Shared/StringDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

/// <summary>
/// This class is based on list so is very slow but allows not unique keys.
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -160,6 +161,16 @@ public void Add(string key, string value)
m_list.Add(newItem);
}

/// <summary>
/// Add a new pair of key and a list of strings that is json encoded to a string value.
/// </summary>
/// <param name="key">The key to add.</param>
/// <param name="value">The value to add.</param>
public void Add(string key, List<string> value)
{
Add(key, JsonConvert.SerializeObject(value));
}

/// <summary>
/// Removes the specified key.
/// </summary>
Expand Down

0 comments on commit 008d0af

Please sign in to comment.