using System; using System.Collections.Generic; using System.Linq; namespace RestEase.Implementation { /// /// Class containing information about a desired query parameter /// public abstract class QueryParameterInfo { /// /// Gets or sets the method to use to serialize the query parameter /// public QuerySerializationMethod SerializationMethod { get; protected set; } /// /// Serialize the (typed) value into a collection of name -> value pairs using the given serializer /// /// Serializer to use /// RequestInfo representing the request /// given to the , if any /// Serialized value public abstract IEnumerable> SerializeValue(RequestQueryParamSerializer serializer, IRequestInfo requestInfo, IFormatProvider? formatProvider); /// /// Serialize the value into a collection of name -> value pairs using its ToString method /// /// given to the , if any /// Serialized value public abstract IEnumerable> SerializeToString(IFormatProvider? formatProvider); } /// /// Class containing information about a desired query parameter /// /// Type of the value public class QueryParameterInfo : QueryParameterInfo { private readonly string name; private readonly T value; private readonly string? format; /// /// Initialises a new instance of the class /// /// Method to use the serialize the query value /// Name of the name/value pair /// Value of the name/value pair /// Format string to use public QueryParameterInfo(QuerySerializationMethod serializationMethod, string name, T value, string? format) { this.SerializationMethod = serializationMethod; this.name = name; this.value = value; this.format = format; } /// /// Serialize the (typed) value into a collection of name -> value pairs using the given serializer /// /// Serializer to use /// RequestInfo representing the request /// given to the , if any /// Serialized value public override IEnumerable> SerializeValue(RequestQueryParamSerializer serializer, IRequestInfo requestInfo, IFormatProvider? formatProvider) { if (serializer == null) throw new ArgumentNullException(nameof(serializer)); if (requestInfo == null) throw new ArgumentNullException(nameof(requestInfo)); return serializer.SerializeQueryParam(this.name, this.value, new RequestQueryParamSerializerInfo(requestInfo, this.format, formatProvider)); } /// /// Serialize the value into a collection of name -> value pairs using its ToString method /// /// given to the , if any /// Serialized value public override IEnumerable> SerializeToString(IFormatProvider? formatProvider) { if (this.value == null) return Enumerable.Empty>(); return new[] { new KeyValuePair(this.name, ToStringHelper.ToString(this.value, this.format, formatProvider)) }; } } /// /// Class containing information about collection of a desired query parameters /// /// Element type of the value public class QueryCollectionParameterInfo : QueryParameterInfo { private readonly string name; private readonly IEnumerable values; private readonly string? format; /// /// Initialises a new instance of the class /// /// Method to use the serialize the query values /// Name of the name/values pair /// Values of the name/values pair /// Format string to use public QueryCollectionParameterInfo(QuerySerializationMethod serializationMethod, string name, IEnumerable values, string? format) { this.SerializationMethod = serializationMethod; this.name = name; this.values = values; this.format = format; } /// /// Serialize the (typed) value into a collection of name -> value pairs using the given serializer /// /// Serializer to use /// RequestInfo representing the request /// given to the , if any /// Serialized value public override IEnumerable> SerializeValue(RequestQueryParamSerializer serializer, IRequestInfo requestInfo, IFormatProvider? formatProvider) { if (serializer == null) throw new ArgumentNullException(nameof(serializer)); return serializer.SerializeQueryCollectionParam(this.name, this.values, new RequestQueryParamSerializerInfo(requestInfo, this.format, formatProvider)); } /// /// Serialize the value into a collection of name -> value pairs using its ToString method /// /// given to the , if any /// Serialized value public override IEnumerable> SerializeToString(IFormatProvider? formatProvider) { if (this.values == null) yield break; foreach (var value in this.values) { if (value == null) continue; yield return new KeyValuePair(this.name, ToStringHelper.ToString(value, this.format, formatProvider)); } } } }