Skip to content

Commit c3e09bf

Browse files
committed
Merge with dev
1 parent 507ed87 commit c3e09bf

12 files changed

+1389
-6
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Order;
3+
using System.Globalization;
4+
5+
namespace RestSharp.Benchmarks.Requests {
6+
[MemoryDiagnoser, RankColumn, Orderer(SummaryOrderPolicy.FastestToSlowest)]
7+
public partial class AddObjectToRequestParametersBenchmarks {
8+
Data _data;
9+
10+
[GlobalSetup]
11+
public void GlobalSetup() {
12+
const string @string = "random string";
13+
const int arraySize = 10_000;
14+
var strings = new string[arraySize];
15+
Array.Fill(strings, @string);
16+
var ints = new int[arraySize];
17+
Array.Fill(ints, int.MaxValue);
18+
19+
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
20+
var dateTime = DateTime.Parse("01/01/2013 03:03:12");
21+
22+
_data = new Data(@string, int.MaxValue, strings, ints, dateTime, strings);
23+
}
24+
25+
[Benchmark(Baseline = true)]
26+
public void AddObject() => new RestRequest().AddObject(_data);
27+
28+
[Benchmark]
29+
public void AddObjectStatic() => new RestRequest().AddObjectStatic(_data);
30+
31+
}
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace RestSharp.Benchmarks.Requests {
2+
sealed record Data(
3+
string String,
4+
[property: RequestProperty(Name = "PropertyName")] int Int32,
5+
string[] Strings,
6+
[property: RequestProperty(Format = "00000", ArrayQueryType = RequestArrayQueryType.ArrayParameters)] int[] Ints,
7+
[property: RequestProperty(Name = "DateTime", Format = "hh:mm tt")] object DateTime,
8+
object StringArray);
9+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Reflection;
16+
17+
namespace RestSharp;
18+
19+
static partial class PropertyCache<T> where T : class {
20+
sealed partial class Populator {
21+
sealed record RequestProperty {
22+
/// <summary>
23+
/// Gets or sets the <see cref="RequestPropertyAttribute.Name"/> associated
24+
/// with the property this object represents
25+
/// </summary>
26+
internal string Name { get; init; }
27+
/// <summary>
28+
/// Gets the <see cref="RequestPropertyAttribute.Format"/> associated with
29+
/// the property this object represents
30+
/// </summary>
31+
internal string? Format { get; }
32+
/// <summary>
33+
/// Gets the <see cref="RequestPropertyAttribute.ArrayQueryType"/> associated
34+
/// with the property this object represents
35+
/// </summary>
36+
internal RequestArrayQueryType ArrayQueryType { get; }
37+
/// <summary>
38+
/// Gets the return type of the property this object represents
39+
/// </summary>
40+
internal Type Type { get; }
41+
42+
RequestProperty(string name, string? format, RequestArrayQueryType arrayQueryType, Type type) {
43+
Name = name;
44+
Format = format;
45+
ArrayQueryType = arrayQueryType;
46+
Type = type;
47+
}
48+
49+
/// <summary>
50+
/// Creates a new request property representation of the provided property
51+
/// </summary>
52+
/// <param name="property">The property to turn into a request property</param>
53+
/// <returns></returns>
54+
internal static RequestProperty From(PropertyInfo property) {
55+
var requestPropertyAttribute =
56+
property.GetCustomAttribute<RequestPropertyAttribute>() ??
57+
new RequestPropertyAttribute();
58+
59+
var propertyName = requestPropertyAttribute.Name ?? property.Name;
60+
61+
return new RequestProperty(
62+
propertyName,
63+
requestPropertyAttribute.Format,
64+
requestPropertyAttribute.ArrayQueryType,
65+
property.PropertyType
66+
);
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)