|
| 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