Coding style

This commit is contained in:
2019-12-04 17:10:06 +01:00
parent c1e8637516
commit 2f74732924
72 changed files with 12543 additions and 13087 deletions
@@ -1,102 +1,105 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// Models an option specification.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class ArgumentOptionAttribute
: Attribute {
/// <summary>
/// Models an option specification.
/// Based on CommandLine (Copyright 2005-2015 Giacomo Stelluti Scala and Contributors.).
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// The default long name will be inferred from target property.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class ArgumentOptionAttribute
: Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// The default long name will be inferred from target property.
/// </summary>
public ArgumentOptionAttribute()
: this(string.Empty, string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="longName">The long name of the option.</param>
public ArgumentOptionAttribute(string longName)
: this(string.Empty, longName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option.</param>
/// <param name="longName">The long name of the option or null if not used.</param>
public ArgumentOptionAttribute(char shortName, string longName)
: this(new string(shortName, 1), longName)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option..</param>
public ArgumentOptionAttribute(char shortName)
: this(new string(shortName, 1), string.Empty)
{
}
private ArgumentOptionAttribute(string shortName, string longName)
{
ShortName = shortName ?? throw new ArgumentNullException(nameof(shortName));
LongName = longName ?? throw new ArgumentNullException(nameof(longName));
}
/// <summary>
/// Gets long name of this command line option. This name is usually a single English word.
/// </summary>
/// <value>
/// The long name.
/// </value>
public string LongName { get; }
/// <summary>
/// Gets a short name of this command line option, made of one character.
/// </summary>
/// <value>
/// The short name.
/// </value>
public string ShortName { get; }
/// <summary>
/// When applying attribute to <see cref="System.Collections.Generic.IEnumerable{T}"/> target properties,
/// it allows you to split an argument and consume its content as a sequence.
/// </summary>
public char Separator { get; set; } = '\0';
/// <summary>
/// Gets or sets mapped property default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a command line option is required.
/// </summary>
/// <value>
/// <c>true</c> if required; otherwise, <c>false</c>.
/// </value>
public bool Required { get; set; }
/// <summary>
/// Gets or sets a short description of this command line option. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public string HelpText { get; set; }
}
public ArgumentOptionAttribute()
: this(String.Empty, String.Empty) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="longName">The long name of the option.</param>
public ArgumentOptionAttribute(String longName)
: this(String.Empty, longName) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option.</param>
/// <param name="longName">The long name of the option or null if not used.</param>
public ArgumentOptionAttribute(Char shortName, String longName)
: this(new String(shortName, 1), longName) {
}
/// <summary>
/// Initializes a new instance of the <see cref="ArgumentOptionAttribute"/> class.
/// </summary>
/// <param name="shortName">The short name of the option..</param>
public ArgumentOptionAttribute(Char shortName)
: this(new String(shortName, 1), String.Empty) {
}
private ArgumentOptionAttribute(String shortName, String longName) {
this.ShortName = shortName ?? throw new ArgumentNullException(nameof(shortName));
this.LongName = longName ?? throw new ArgumentNullException(nameof(longName));
}
/// <summary>
/// Gets long name of this command line option. This name is usually a single English word.
/// </summary>
/// <value>
/// The long name.
/// </value>
public String LongName {
get;
}
/// <summary>
/// Gets a short name of this command line option, made of one character.
/// </summary>
/// <value>
/// The short name.
/// </value>
public String ShortName {
get;
}
/// <summary>
/// When applying attribute to <see cref="System.Collections.Generic.IEnumerable{T}"/> target properties,
/// it allows you to split an argument and consume its content as a sequence.
/// </summary>
public Char Separator { get; set; } = '\0';
/// <summary>
/// Gets or sets mapped property default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public Object DefaultValue {
get; set;
}
/// <summary>
/// Gets or sets a value indicating whether a command line option is required.
/// </summary>
/// <value>
/// <c>true</c> if required; otherwise, <c>false</c>.
/// </value>
public Boolean Required {
get; set;
}
/// <summary>
/// Gets or sets a short description of this command line option. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public String HelpText {
get; set;
}
}
}
@@ -1,13 +1,11 @@
namespace Unosquare.Swan.Attributes
{
using System;
/// <summary>
/// Represents an attribute to select which properties are copyable between objects.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public class CopyableAttribute : Attribute
{
}
using System;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// Represents an attribute to select which properties are copyable between objects.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public class CopyableAttribute : Attribute {
}
}
@@ -1,39 +1,40 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// An attribute used to help setup a property behavior when serialize/deserialize JSON.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class JsonPropertyAttribute : Attribute {
/// <summary>
/// An attribute used to help setup a property behavior when serialize/deserialize JSON.
/// Initializes a new instance of the <see cref="JsonPropertyAttribute" /> class.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class JsonPropertyAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="JsonPropertyAttribute" /> class.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="ignored">if set to <c>true</c> [ignored].</param>
public JsonPropertyAttribute(string propertyName, bool ignored = false)
{
PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
Ignored = ignored;
}
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public string PropertyName { get; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="JsonPropertyAttribute" /> is ignored.
/// </summary>
/// <value>
/// <c>true</c> if ignored; otherwise, <c>false</c>.
/// </value>
public bool Ignored { get; }
}
/// <param name="propertyName">Name of the property.</param>
/// <param name="ignored">if set to <c>true</c> [ignored].</param>
public JsonPropertyAttribute(String propertyName, Boolean ignored = false) {
this.PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
this.Ignored = ignored;
}
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
/// <value>
/// The name of the property.
/// </value>
public String PropertyName {
get;
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="JsonPropertyAttribute" /> is ignored.
/// </summary>
/// <value>
/// <c>true</c> if ignored; otherwise, <c>false</c>.
/// </value>
public Boolean Ignored {
get;
}
}
}
@@ -1,54 +1,62 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// An attribute used to include additional information to a Property for serialization.
///
/// Previously we used DisplayAttribute from DataAnnotation.
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class PropertyDisplayAttribute : Attribute {
/// <summary>
/// An attribute used to include additional information to a Property for serialization.
///
/// Previously we used DisplayAttribute from DataAnnotation.
/// Gets or sets the name.
/// </summary>
/// <seealso cref="System.Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class PropertyDisplayAttribute : Attribute
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description { get; set; }
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public string GroupName { get; set; }
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public object DefaultValue { get; set; }
/// <summary>
/// Gets or sets the format string to call with method <c>ToString</c>.
/// </summary>
/// <value>
/// The format.
/// </value>
public string Format { get; set; }
}
/// <value>
/// The name.
/// </value>
public String Name {
get; set;
}
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public String Description {
get; set;
}
/// <summary>
/// Gets or sets the name of the group.
/// </summary>
/// <value>
/// The name of the group.
/// </value>
public String GroupName {
get; set;
}
/// <summary>
/// Gets or sets the default value.
/// </summary>
/// <value>
/// The default value.
/// </value>
public Object DefaultValue {
get; set;
}
/// <summary>
/// Gets or sets the format string to call with method <c>ToString</c>.
/// </summary>
/// <value>
/// The format.
/// </value>
public String Format {
get; set;
}
}
}
@@ -1,30 +1,27 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// An attribute used to help conversion structs back and forth into arrays of bytes via
/// extension methods included in this library ToStruct and ToBytes.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Struct)]
public class StructEndiannessAttribute : Attribute {
/// <summary>
/// An attribute used to help conversion structs back and forth into arrays of bytes via
/// extension methods included in this library ToStruct and ToBytes.
/// Initializes a new instance of the <see cref="StructEndiannessAttribute"/> class.
/// </summary>
/// <seealso cref="Attribute" />
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Struct)]
public class StructEndiannessAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="StructEndiannessAttribute"/> class.
/// </summary>
/// <param name="endianness">The endianness.</param>
public StructEndiannessAttribute(Endianness endianness)
{
Endianness = endianness;
}
/// <summary>
/// Gets the endianness.
/// </summary>
/// <value>
/// The endianness.
/// </value>
public Endianness Endianness { get; }
}
/// <param name="endianness">The endianness.</param>
public StructEndiannessAttribute(Endianness endianness) => this.Endianness = endianness;
/// <summary>
/// Gets the endianness.
/// </summary>
/// <value>
/// The endianness.
/// </value>
public Endianness Endianness {
get;
}
}
}
+120 -123
View File
@@ -1,133 +1,130 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System.Text.RegularExpressions;
using Abstractions;
using System;
using System.Text.RegularExpressions;
using Unosquare.Swan.Abstractions;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// Regex validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class MatchAttribute : Attribute, IValidator {
/// <summary>
/// Regex validator.
/// Initializes a new instance of the <see cref="MatchAttribute" /> class.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class MatchAttribute : Attribute, IValidator
{
/// <summary>
/// Initializes a new instance of the <see cref="MatchAttribute" /> class.
/// </summary>
/// <param name="regex">A regex string.</param>
/// <param name="errorMessage">The error message.</param>
/// <exception cref="ArgumentNullException">Expression.</exception>
public MatchAttribute(string regex, string errorMessage = null)
{
Expression = regex ?? throw new ArgumentNullException(nameof(Expression));
ErrorMessage = errorMessage ?? "String does not match the specified regular expression";
}
/// <summary>
/// The string regex used to find a match.
/// </summary>
public string Expression { get; }
/// <inheritdoc/>
public string ErrorMessage { get; internal set; }
/// <inheritdoc/>
public bool IsValid<T>(T value)
{
if (Equals(value, default(T)))
return false;
return !(value is string)
/// <param name="regex">A regex string.</param>
/// <param name="errorMessage">The error message.</param>
/// <exception cref="ArgumentNullException">Expression.</exception>
public MatchAttribute(String regex, String errorMessage = null) {
this.Expression = regex ?? throw new ArgumentNullException(nameof(this.Expression));
this.ErrorMessage = errorMessage ?? "String does not match the specified regular expression";
}
/// <summary>
/// The string regex used to find a match.
/// </summary>
public String Expression {
get;
}
/// <inheritdoc/>
public String ErrorMessage {
get; internal set;
}
/// <inheritdoc/>
public Boolean IsValid<T>(T value) => Equals(value, default(T))
? false
: !(value is String)
? throw new ArgumentException("Property is not a string")
: Regex.IsMatch(value.ToString(), Expression);
}
}
: Regex.IsMatch(value.ToString(), this.Expression);
}
/// <summary>
/// Email validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class EmailAttribute : MatchAttribute {
private const String EmailRegExp =
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
/// <summary>
/// Email validator.
/// Initializes a new instance of the <see cref="EmailAttribute"/> class.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class EmailAttribute : MatchAttribute
{
private const string EmailRegExp =
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";
/// <summary>
/// Initializes a new instance of the <see cref="EmailAttribute"/> class.
/// </summary>
/// <param name="errorMessage">The error message.</param>
public EmailAttribute(string errorMessage = null)
: base(EmailRegExp, errorMessage ?? "String is not an email")
{
}
}
/// <param name="errorMessage">The error message.</param>
public EmailAttribute(String errorMessage = null)
: base(EmailRegExp, errorMessage ?? "String is not an email") {
}
}
/// <summary>
/// A not null validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute, IValidator {
/// <inheritdoc/>
public String ErrorMessage => "Value is null";
/// <inheritdoc/>
public Boolean IsValid<T>(T value) => !Equals(default(T), value);
}
/// <summary>
/// A range constraint validator.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class RangeAttribute : Attribute, IValidator {
/// <summary>
/// A not null validator.
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
/// Constructor that takes integer minimum and maximum values.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute, IValidator
{
/// <inheritdoc/>
public string ErrorMessage => "Value is null";
/// <inheritdoc/>
public bool IsValid<T>(T value) => !Equals(default(T), value);
}
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public RangeAttribute(Int32 min, Int32 max) {
if(min >= max) {
throw new InvalidOperationException("Maximum value must be greater than minimum");
}
this.Maximum = max;
this.Minimum = min;
}
/// <summary>
/// A range constraint validator.
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
/// Constructor that takes double minimum and maximum values.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class RangeAttribute : Attribute, IValidator
{
/// <summary>
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
/// Constructor that takes integer minimum and maximum values.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public RangeAttribute(int min, int max)
{
if (min >= max)
throw new InvalidOperationException("Maximum value must be greater than minimum");
Maximum = max;
Minimum = min;
}
/// <summary>
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
/// Constructor that takes double minimum and maximum values.
/// </summary>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public RangeAttribute(double min, double max)
{
if (min >= max)
throw new InvalidOperationException("Maximum value must be greater than minimum");
Maximum = max;
Minimum = min;
}
/// <inheritdoc/>
public string ErrorMessage => "Value is not within the specified range";
/// <summary>
/// Maximum value for the range.
/// </summary>
public IComparable Maximum { get; }
/// <summary>
/// Minimum value for the range.
/// </summary>
public IComparable Minimum { get; }
/// <inheritdoc/>
public bool IsValid<T>(T value)
=> value is IComparable comparable
? comparable.CompareTo(Minimum) >= 0 && comparable.CompareTo(Maximum) <= 0
: throw new ArgumentException(nameof(value));
}
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
public RangeAttribute(Double min, Double max) {
if(min >= max) {
throw new InvalidOperationException("Maximum value must be greater than minimum");
}
this.Maximum = max;
this.Minimum = min;
}
/// <inheritdoc/>
public String ErrorMessage => "Value is not within the specified range";
/// <summary>
/// Maximum value for the range.
/// </summary>
public IComparable Maximum {
get;
}
/// <summary>
/// Minimum value for the range.
/// </summary>
public IComparable Minimum {
get;
}
/// <inheritdoc/>
public Boolean IsValid<T>(T value)
=> value is IComparable comparable
? comparable.CompareTo(this.Minimum) >= 0 && comparable.CompareTo(this.Maximum) <= 0
: throw new ArgumentException(nameof(value));
}
}
@@ -1,40 +1,39 @@
namespace Unosquare.Swan.Attributes
{
using System;
using System;
namespace Unosquare.Swan.Attributes {
/// <summary>
/// Models a verb option.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class VerbOptionAttribute : Attribute {
/// <summary>
/// Models a verb option.
/// Initializes a new instance of the <see cref="VerbOptionAttribute" /> class.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class VerbOptionAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="VerbOptionAttribute" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <exception cref="ArgumentNullException">name.</exception>
public VerbOptionAttribute(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
}
/// <summary>
/// Gets the name of the verb option.
/// </summary>
/// <value>
/// Name.
/// </value>
public string Name { get; }
/// <summary>
/// Gets or sets a short description of this command line verb. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public string HelpText { get; set; }
/// <inheritdoc />
public override string ToString() => $" {Name}\t\t{HelpText}";
}
/// <param name="name">The name.</param>
/// <exception cref="ArgumentNullException">name.</exception>
public VerbOptionAttribute(String name) => this.Name = name ?? throw new ArgumentNullException(nameof(name));
/// <summary>
/// Gets the name of the verb option.
/// </summary>
/// <value>
/// Name.
/// </value>
public String Name {
get;
}
/// <summary>
/// Gets or sets a short description of this command line verb. Usually a sentence summary.
/// </summary>
/// <value>
/// The help text.
/// </value>
public String HelpText {
get; set;
}
/// <inheritdoc />
public override String ToString() => $" {this.Name}\t\t{this.HelpText}";
}
}