Skip to content

Commit

Permalink
!fixup dfdad56
Browse files Browse the repository at this point in the history
- Braces on new lines
- Rename FormValueDictionary -> FormValueMultimap
  • Loading branch information
MariusVolkhart committed Nov 29, 2018
1 parent dfdad56 commit 303a357
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
Expand Up @@ -5,14 +5,14 @@

namespace Refit.Tests
{
public class FormValueDictionaryTests
public class FormValueMultimapTests
{
readonly RefitSettings settings = new RefitSettings();

[Fact]
public void EmptyIfNullPassedIn()
{
var target = new FormValueDictionary(null, settings);
var target = new FormValueMultimap(null, settings);
Assert.Empty(target);
}

Expand All @@ -25,7 +25,7 @@ public void LoadsFromDictionary()
{ "xyz", "123" }
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.Equal(source, target);
}
Expand All @@ -44,7 +44,7 @@ public void LoadsFromObject()
{ "B", "2" },
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand All @@ -71,7 +71,7 @@ public void LoadFromObjectWithCollections()
new KeyValuePair<string, string>("E", "True|False")
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public void ExcludesPropertiesWithInaccessibleGetters()
{ "C", "FooBar" }
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand All @@ -137,7 +137,7 @@ public void LoadsFromAnonymousType()
{ "xyz", "123" }
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);


Assert.Equal(expected, actual);
Expand All @@ -151,7 +151,7 @@ public void UsesAliasAsAttribute()
Foo = "abc"
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Foo", target.Keys);
Assert.Contains("f", target.Keys);
Expand All @@ -166,7 +166,7 @@ public void UsesJsonPropertyAttribute()
Bar = "xyz"
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Bar", target.Keys);
Assert.Contains("b", target.Keys);
Expand All @@ -181,7 +181,7 @@ public void UsesQueryPropertyAttribute()
Frob = 4
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Bar", target.Keys);
Assert.Contains("prefix-fr", target.Keys);
Expand All @@ -197,7 +197,7 @@ public void GivesPrecedenceToAliasAs()
Baz = "123"
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Bar", target.Keys);
Assert.DoesNotContain("z", target.Keys);
Expand All @@ -214,7 +214,7 @@ public void SkipsNullValuesFromDictionary()
{ "xyz", null }
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.Single(target);
Assert.Contains("foo", target.Keys);
Expand All @@ -237,7 +237,7 @@ public void SerializesEnumWithEnumMemberAttribute()
};


var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand Down
17 changes: 10 additions & 7 deletions Refit/FormValueDictionary.cs → Refit/FormValueMultimap.cs
Expand Up @@ -11,16 +11,16 @@ namespace Refit
/// Transforms a form source from a .NET representation to the appropriate HTTP form encoded representation.
/// </summary>
/// <remarks>Performs field renaming and value formatting as specified in <see cref="QueryAttribute"/>s and
/// <see cref="RefitSettings.FormUrlEncodedParameterFormatter"/>. Note that this is not a true dictionary, rather, a
/// form of MultiMap. A given key may appear multiple times with the same or different values.</remarks>
class FormValueDictionary : IEnumerable<KeyValuePair<string, string>>
/// <see cref="RefitSettings.FormUrlEncodedParameterFormatter"/>. A given key may appear multiple times with the
/// same or different values.</remarks>
class FormValueMultimap : IEnumerable<KeyValuePair<string, string>>
{
static readonly Dictionary<Type, PropertyInfo[]> propertyCache
= new Dictionary<Type, PropertyInfo[]>();

private readonly IList<KeyValuePair<string, string>> formEntries = new List<KeyValuePair<string, string>>();

public FormValueDictionary(object source, RefitSettings settings)
public FormValueMultimap(object source, RefitSettings settings)
{
if (source == null) return;

Expand Down Expand Up @@ -56,10 +56,13 @@ public FormValueDictionary(object source, RefitSettings settings)
// see if there's a query attribute
var attrib = property.GetCustomAttribute<QueryAttribute>(true);

if (value is IEnumerable enumerable) {
switch (attrib?.CollectionFormat) {
if (value is IEnumerable enumerable)
{
switch (attrib?.CollectionFormat)
{
case CollectionFormat.Multi:
foreach (var item in enumerable) {
foreach (var item in enumerable)
{
Add(fieldName, settings.FormUrlEncodedParameterFormatter.Format(item, attrib.Format));
}
break;
Expand Down
2 changes: 1 addition & 1 deletion Refit/RequestBuilderImplementation.cs
Expand Up @@ -463,7 +463,7 @@ void AddMultipartItem(MultipartFormDataContent multiPartContent, string fileName
switch (restMethod.BodyParameterInfo.Item1)
{
case BodySerializationMethod.UrlEncoded:
ret.Content = paramList[i] is string str ? (HttpContent)new StringContent(Uri.EscapeDataString(str), Encoding.UTF8, "application/x-www-form-urlencoded") : new FormUrlEncodedContent(new FormValueDictionary(paramList[i], settings));
ret.Content = paramList[i] is string str ? (HttpContent)new StringContent(Uri.EscapeDataString(str), Encoding.UTF8, "application/x-www-form-urlencoded") : new FormUrlEncodedContent(new FormValueMultimap(paramList[i], settings));
break;
case BodySerializationMethod.Default:
case BodySerializationMethod.Json:
Expand Down

0 comments on commit 303a357

Please sign in to comment.