using System;
namespace RestEase
{
///
/// Marks a parameter as being a query param
///
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class QueryAttribute : Attribute
{
private string? _name;
///
/// Gets or sets the name of the query param. Will use the parameter / property name if unset.
///
public string? Name
{
get => this._name;
set
{
this._name = value;
this.HasName = true;
}
}
///
/// Gets a value indicating whether the user has set the name attribute
///
public bool HasName { get; private set; }
///
/// Gets the serialization method to use to serialize the value. Defaults to QuerySerializationMethod.ToString
///
public QuerySerializationMethod SerializationMethod { get; set; }
///
/// Gets or sets the format string used to format the value
///
///
/// If is , this is passed to the serializer
/// as .
/// Otherwise, if this looks like a format string which can be passed to ,
/// (i.e. it contains at least one format placeholder), then this happens with the value passed as the first arg.
/// Otherwise, if the value implements , this is passed to the value's
/// method. Otherwise this is ignored.
/// Example values: "X2", "{0:X2}", "test{0}".
///
public string? Format { get; set; }
///
/// Initialises a new instance of the class
///
public QueryAttribute()
: this(QuerySerializationMethod.Default)
{
}
///
/// Initialises a new instance of the class, with the given serialization method
///
/// Serialization method to use to serialize the value
public QueryAttribute(QuerySerializationMethod serializationMethod)
{
// Don't set this.Name
this.SerializationMethod = serializationMethod;
}
///
/// Initialises a new instance of the class, with the given name
///
/// Name of the query parameter
public QueryAttribute(string? name)
: this(name, QuerySerializationMethod.Default)
{
}
///
/// Initialises a new instance of the class, with the given name and serialization method
///
/// Name of the query parameter
/// Serialization method to use to serialize the value
public QueryAttribute(string? name, QuerySerializationMethod serializationMethod)
{
this.Name = name;
this.SerializationMethod = serializationMethod;
}
}
}