-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom props attribute (#1868)
* Added custom props attribute * Updated the docs * Values must match
- Loading branch information
1 parent
ec61745
commit f3af0ce
Showing
4 changed files
with
129 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
using System.Reflection; | ||
|
||
namespace RestSharp; | ||
|
||
static class ObjectParser { | ||
public static IEnumerable<(string Name, string? Value)> GetProperties(this object obj, params string[] includedProperties) { | ||
// automatically create parameters from object props | ||
var type = obj.GetType(); | ||
var props = type.GetProperties(); | ||
|
||
foreach (var prop in props.Where(x => IsAllowedProperty(x.Name))) { | ||
var val = prop.GetValue(obj, null); | ||
|
||
if (val == null) continue; | ||
|
||
yield return prop.PropertyType.IsArray | ||
? GetArray(prop, val) | ||
: GetValue(prop, val); | ||
} | ||
|
||
string? ParseValue(string? format, object? value) => format == null ? value?.ToString() : string.Format($"{{0:{format}}}", value); | ||
|
||
(string, string?) GetArray(PropertyInfo propertyInfo, object? value) { | ||
var elementType = propertyInfo.PropertyType.GetElementType(); | ||
var array = (Array)value!; | ||
|
||
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>(); | ||
var name = attribute?.Name ?? propertyInfo.Name; | ||
|
||
if (array.Length > 0 && elementType != null) { | ||
// convert the array to an array of strings | ||
var values = array | ||
.Cast<object>() | ||
.Select(item => ParseValue(attribute?.Format, item)); | ||
return (name, string.Join(",", values)); | ||
} | ||
|
||
return (name, null); | ||
} | ||
|
||
(string, string?) GetValue(PropertyInfo propertyInfo, object? value) { | ||
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>(); | ||
var name = attribute?.Name ?? propertyInfo.Name; | ||
var val = ParseValue(attribute?.Format, value); | ||
return (name, val); | ||
} | ||
|
||
bool IsAllowedProperty(string propertyName) | ||
=> includedProperties.Length == 0 || includedProperties.Length > 0 && includedProperties.Contains(propertyName); | ||
} | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Property)] | ||
public class RequestPropertyAttribute : Attribute { | ||
public string? Name { get; set; } | ||
|
||
public string? Format { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Globalization; | ||
|
||
namespace RestSharp.Tests; | ||
|
||
public class ObjectParserTests { | ||
[Fact] | ||
public void ShouldUseRequestProperty() { | ||
var now = DateTime.Now; | ||
var dates = new[] { now, now.AddDays(1), now.AddDays(2) }; | ||
var request = new TestObject { | ||
SomeData = "test", | ||
SomeDate = now, | ||
Plain = 123, | ||
PlainArray = dates, | ||
DatesArray = dates | ||
}; | ||
|
||
var parsed = request.GetProperties().ToDictionary(x => x.Name, x => x.Value); | ||
parsed["some_data"].Should().Be(request.SomeData); | ||
parsed["SomeDate"].Should().Be(request.SomeDate.ToString("d")); | ||
parsed["Plain"].Should().Be(request.Plain.ToString()); | ||
// ReSharper disable once SpecifyACultureInStringConversionExplicitly | ||
parsed["PlainArray"].Should().Be(string.Join(",", dates.Select(x => x.ToString()))); | ||
parsed["dates"].Should().Be(string.Join(",", dates.Select(x => x.ToString("d")))); | ||
} | ||
|
||
class TestObject { | ||
[RequestProperty(Name = "some_data")] | ||
public string SomeData { get; set; } | ||
|
||
[RequestProperty(Format = "d")] | ||
public DateTime SomeDate { get; set; } | ||
|
||
[RequestProperty(Name = "dates", Format = "d")] | ||
public DateTime[] DatesArray { get; set; } | ||
|
||
public int Plain { get; set; } | ||
public DateTime[] PlainArray { get; set; } | ||
} | ||
} |