#336 fun with [CallerMemberName]

This commit is contained in:
Todd Menier 2019-01-25 22:48:48 -06:00
parent 5a097b5d7d
commit 48740b87d3

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Flurl.Http.Testing; using Flurl.Http.Testing;
namespace Flurl.Http.Configuration namespace Flurl.Http.Configuration
@ -36,8 +37,8 @@ namespace Flurl.Http.Configuration
/// Gets or sets the HTTP request timeout. /// Gets or sets the HTTP request timeout.
/// </summary> /// </summary>
public TimeSpan? Timeout { public TimeSpan? Timeout {
get => Get(() => Timeout); get => Get<TimeSpan?>();
set => Set(() => Timeout, value); set => Set(value);
} }
/// <summary> /// <summary>
@ -46,64 +47,64 @@ namespace Flurl.Http.Configuration
/// 2xx will never throw regardless of this setting. /// 2xx will never throw regardless of this setting.
/// </summary> /// </summary>
public string AllowedHttpStatusRange { public string AllowedHttpStatusRange {
get => Get(() => AllowedHttpStatusRange); get => Get<string>();
set => Set(() => AllowedHttpStatusRange, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether cookies should be sent/received with each HTTP request. /// Gets or sets a value indicating whether cookies should be sent/received with each HTTP request.
/// </summary> /// </summary>
public bool CookiesEnabled { public bool CookiesEnabled {
get => Get(() => CookiesEnabled); get => Get<bool>();
set => Set(() => CookiesEnabled, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets object used to serialize and deserialize JSON. Default implementation uses Newtonsoft Json.NET. /// Gets or sets object used to serialize and deserialize JSON. Default implementation uses Newtonsoft Json.NET.
/// </summary> /// </summary>
public ISerializer JsonSerializer { public ISerializer JsonSerializer {
get => Get(() => JsonSerializer); get => Get<ISerializer>();
set => Set(() => JsonSerializer, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets object used to serialize URL-encoded data. (Deserialization not supported in default implementation.) /// Gets or sets object used to serialize URL-encoded data. (Deserialization not supported in default implementation.)
/// </summary> /// </summary>
public ISerializer UrlEncodedSerializer { public ISerializer UrlEncodedSerializer {
get => Get(() => UrlEncodedSerializer); get => Get<ISerializer>();
set => Set(() => UrlEncodedSerializer, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets a callback that is called immediately before every HTTP request is sent. /// Gets or sets a callback that is called immediately before every HTTP request is sent.
/// </summary> /// </summary>
public Action<HttpCall> BeforeCall { public Action<HttpCall> BeforeCall {
get => Get(() => BeforeCall); get => Get<Action<HttpCall>>();
set => Set(() => BeforeCall, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets a callback that is asynchronously called immediately before every HTTP request is sent. /// Gets or sets a callback that is asynchronously called immediately before every HTTP request is sent.
/// </summary> /// </summary>
public Func<HttpCall, Task> BeforeCallAsync { public Func<HttpCall, Task> BeforeCallAsync {
get => Get(() => BeforeCallAsync); get => Get<Func<HttpCall, Task>>();
set => Set(() => BeforeCallAsync, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets a callback that is called immediately after every HTTP response is received. /// Gets or sets a callback that is called immediately after every HTTP response is received.
/// </summary> /// </summary>
public Action<HttpCall> AfterCall { public Action<HttpCall> AfterCall {
get => Get(() => AfterCall); get => Get<Action<HttpCall>>();
set => Set(() => AfterCall, value); set => Set(value);
} }
/// <summary> /// <summary>
/// Gets or sets a callback that is asynchronously called immediately after every HTTP response is received. /// Gets or sets a callback that is asynchronously called immediately after every HTTP response is received.
/// </summary> /// </summary>
public Func<HttpCall, Task> AfterCallAsync { public Func<HttpCall, Task> AfterCallAsync {
get => Get(() => AfterCallAsync); get => Get<Func<HttpCall, Task>>();
set => Set(() => AfterCallAsync, value); set => Set(value);
} }
/// <summary> /// <summary>
@ -111,8 +112,8 @@ namespace Flurl.Http.Configuration
/// HTTP status code is returned in the response. Response should be null-checked if used in the event handler. /// HTTP status code is returned in the response. Response should be null-checked if used in the event handler.
/// </summary> /// </summary>
public Action<HttpCall> OnError { public Action<HttpCall> OnError {
get => Get(() => OnError); get => Get<Action<HttpCall>>();
set => Set(() => OnError, value); set => Set(value);
} }
/// <summary> /// <summary>
@ -120,8 +121,8 @@ namespace Flurl.Http.Configuration
/// HTTP status code is returned in the response. Response should be null-checked if used in the event handler. /// HTTP status code is returned in the response. Response should be null-checked if used in the event handler.
/// </summary> /// </summary>
public Func<HttpCall, Task> OnErrorAsync { public Func<HttpCall, Task> OnErrorAsync {
get => Get(() => OnErrorAsync); get => Get<Func<HttpCall, Task>>();
set => Set(() => OnErrorAsync, value); set => Set(value);
} }
/// <summary> /// <summary>
@ -135,22 +136,20 @@ namespace Flurl.Http.Configuration
/// <summary> /// <summary>
/// Gets a settings value from this instance if explicitly set, otherwise from the default settings that back this instance. /// Gets a settings value from this instance if explicitly set, otherwise from the default settings that back this instance.
/// </summary> /// </summary>
protected T Get<T>(Expression<Func<T>> property) { protected T Get<T>([CallerMemberName]string propName = null) {
var p = (property.Body as MemberExpression).Member as PropertyInfo;
var testVals = HttpTest.Current?.Settings._vals; var testVals = HttpTest.Current?.Settings._vals;
return return
testVals?.ContainsKey(p.Name) == true ? (T)testVals[p.Name] : testVals?.ContainsKey(propName) == true ? (T)testVals[propName] :
_vals.ContainsKey(p.Name) ? (T)_vals[p.Name] : _vals.ContainsKey(propName) ? (T)_vals[propName] :
Defaults != null ? (T)p.GetValue(Defaults) : Defaults != null ? (T)Defaults.Get<T>(propName) :
default(T); default(T);
} }
/// <summary> /// <summary>
/// Sets a settings value for this instance. /// Sets a settings value for this instance.
/// </summary> /// </summary>
protected void Set<T>(Expression<Func<T>> property, T value) { protected void Set<T>(T value, [CallerMemberName]string propName = null) {
var p = (property.Body as MemberExpression).Member as PropertyInfo; _vals[propName] = value;
_vals[p.Name] = value;
} }
} }
@ -165,8 +164,8 @@ namespace Flurl.Http.Configuration
/// Default is null, effectively disabling the behavior. /// Default is null, effectively disabling the behavior.
/// </summary> /// </summary>
public TimeSpan? ConnectionLeaseTimeout { public TimeSpan? ConnectionLeaseTimeout {
get => Get(() => ConnectionLeaseTimeout); get => Get<TimeSpan?>();
set => Set(() => ConnectionLeaseTimeout, value); set => Set(value);
} }
/// <summary> /// <summary>
@ -175,8 +174,8 @@ namespace Flurl.Http.Configuration
/// only override the method(s) needed, call the base method, and modify the result. /// only override the method(s) needed, call the base method, and modify the result.
/// </summary> /// </summary>
public IHttpClientFactory HttpClientFactory { public IHttpClientFactory HttpClientFactory {
get => Get(() => HttpClientFactory); get => Get<IHttpClientFactory>();
set => Set(() => HttpClientFactory, value); set => Set(value);
} }
} }
@ -202,8 +201,8 @@ namespace Flurl.Http.Configuration
/// by proxy, HttpClient instances. /// by proxy, HttpClient instances.
/// </summary> /// </summary>
public IFlurlClientFactory FlurlClientFactory { public IFlurlClientFactory FlurlClientFactory {
get => Get(() => FlurlClientFactory); get => Get<IFlurlClientFactory>();
set => Set(() => FlurlClientFactory, value); set => Set(value);
} }
/// <summary> /// <summary>