Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Mar 30, 2024
1 parent 64418a9 commit 91fa64a
Show file tree
Hide file tree
Showing 36 changed files with 631 additions and 561 deletions.
4 changes: 2 additions & 2 deletions src/Entity/CrossOriginResourceSharingHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace Sisk.Core.Entity
/// Provides a class to provide Cross Origin response headers for when communicating with a browser.
/// </summary>
/// <definition>
/// public class CrossOriginResourceSharingHeaders
/// public sealed class CrossOriginResourceSharingHeaders
/// </definition>
/// <type>
/// Class
/// </type>
public class CrossOriginResourceSharingHeaders
public sealed class CrossOriginResourceSharingHeaders
{
/// <summary>
/// Gets an instance of an empty CrossOriginResourceSharingHeaders.
Expand Down
92 changes: 39 additions & 53 deletions src/Entity/MultipartFormCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,120 +7,106 @@
// File name: MultipartFormCollection.cs
// Repository: https://github.com/sisk-http/core

using Sisk.Core.Internal;
using System.Collections;
using System.Collections.Immutable;
using System.Text;

namespace Sisk.Core.Entity;

/// <summary>
/// Represents an class which hosts an multipart form data contents.
/// </summary>
/// <definition>
/// public class MultipartFormCollection : IEnumerable{{MultipartObject}}
/// public class MultipartFormCollection : IEnumerable{{MultipartObject}}, IReadOnlyList{{MultipartObject}},
/// IReadOnlyCollection{{MultipartObject}}
/// </definition>
/// <type>
/// Class
/// </type>
public class MultipartFormCollection : IEnumerable<MultipartObject>
public class MultipartFormCollection : IEnumerable<MultipartObject>, IReadOnlyList<MultipartObject>, IReadOnlyCollection<MultipartObject>
{
private List<MultipartObject> _items;
private readonly IList<MultipartObject> _items;

internal MultipartFormCollection(List<MultipartObject> items)
internal MultipartFormCollection(IEnumerable<MultipartObject> items)
{
_items = items ?? throw new ArgumentNullException(nameof(items));
_items = items.ToImmutableList() ?? throw new ArgumentNullException(nameof(items));
}

/// <summary>
/// Reads an form item by it's name and return an string representation
/// of it's value.
/// Reads an form item contents by it's name.
/// </summary>
/// <param name="name">The form item name.</param>
/// <param name="ignoreCase">Optional. Determines if this method should use an case-insensitive search to find the specified item.</param>
/// <definition>
/// public string? GetString(string name)
/// public string? GetItem(string name, bool ignoreCase = false)
/// </definition>
/// <type>
/// Method
/// </type>
public string? GetString(string name)
public MultipartObject? GetItem(string name, bool ignoreCase = false)
{
return GetItem(name)?.ReadContentAsString();
return _items.FirstOrDefault(i => string.Compare(name, i.Name, ignoreCase) == 0);
}

/// <summary>
/// Reads an form item by it's name.
/// Reads an form item contents by it's name and returns their content as string.
/// </summary>
/// <param name="name">The form item name.</param>
/// <param name="ignoreCase">Optional. Determines if this method should use an case-insensitive search to find the specified item.</param>
/// <param name="encoding">Optional. Specifies the <see cref="Encoding"/> used to read the content.</param>
/// <definition>
/// public MultipartObject? GetItem(string name)
/// public string? GetString(string name, bool ignoreCase = false, Encoding? encoding = null)
/// </definition>
/// <type>
/// Method
/// </type>
public MultipartObject? GetItem(string name)
public string? GetString(string name, bool ignoreCase = false, Encoding? encoding = null)
{
return _items
.FirstOrDefault(i => string.Compare(name, i.Name, true) == 0);
Encoding _enc = encoding ?? Encoding.UTF8;
return _items.FirstOrDefault(i => string.Compare(name, i.Name, ignoreCase) == 0)?.ReadContentAsString(_enc);
}

/// <summary>
/// Reads an form item by it's name and casts it into an <typeparamref name="T"/>.
/// Gets an <see cref="StringValue"/> object from the form item content string.
/// </summary>
/// <typeparam name="T">The supported structure type which will be converted to.</typeparam>
/// <param name="name">The form item name.</param>
/// <param name="defaultValue">The default value if the item is not found.</param>
/// <definition>
/// .NET 6:
/// public T GetItem{{T}}(string name, T defaultValue = default) where T : struct
///
/// .NET 7 and above:
/// public T GetItem{{T}}(string name, T defaultValue = default) where T : struct, IParsable{{T}}
/// public StringValue GetStringValue(string name)
/// </definition>
/// <type>
/// Method
/// </type>
#if NET6_0
public T GetItem<T>(string name, T defaultValue = default) where T : struct
#elif NET7_0_OR_GREATER
public T GetItem<T>(string name, T defaultValue = default) where T : struct, IParsable<T>
#endif
public StringValue GetStringValue(string name)
{
string? value = _items
.FirstOrDefault(i => string.Compare(name, i.Name, true) == 0)?
.ReadContentAsString();

if (value == null) return defaultValue;

try
{
#if NET6_0
return (T)Parseable.ParseInternal<T>(value);
#elif NET7_0_OR_GREATER
return T.Parse(value, null);
#endif
}
catch (InvalidCastException)
{
throw new InvalidCastException(string.Format(SR.HttpRequest_GetQueryValue_CastException, name, typeof(T).FullName));
}
return new StringValue(name, "multipart form", GetItem(name, true)?.ReadContentAsString());
}

/// <nodocs/>
public static implicit operator MultipartObject[](MultipartFormCollection t)
{
return t._items.ToArray();
}
/// <nodoc/>
/// <inheritdoc/>
public MultipartObject this[int index] => ((IReadOnlyList<MultipartObject>)_items)[index];

/// <nodoc/>
/// <inheritdoc/>
public int Count => ((IReadOnlyCollection<MultipartObject>)_items).Count;

/// <nodoc/>
/// <inheritdoc/>
public IEnumerator<MultipartObject> GetEnumerator()
{
return ((IEnumerable<MultipartObject>)_items).GetEnumerator();
}

/// <inheritdoc/>
/// <nodoc/>
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_items).GetEnumerator();
}

/// <nodoc/>
/// <inheritdoc/>
public static implicit operator MultipartObject[](MultipartFormCollection t)
{
return t._items.ToArray();
}
}
2 changes: 1 addition & 1 deletion src/Entity/MultipartObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Sisk.Core.Entity
/// </type>
public class MultipartObject
{
private Encoding _baseEncoding;
private readonly Encoding _baseEncoding;

/// <summary>
/// The multipart form data object headers.
Expand Down
34 changes: 22 additions & 12 deletions src/Entity/StringValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Sisk.Core.Entity;
/// </type>
public class StringValue
{
private string? _ref;
private string argName;
private string argType;
private readonly string? _ref;
private readonly string argName;
private readonly string argType;

internal StringValue(string name, string type, string? data)
{
Expand Down Expand Up @@ -276,32 +276,42 @@ void ThrowIfNull()
/// <nodocs/>
public static bool operator ==(StringValue i, string? other)
{
return i.Value == other;
return i.Equals(other);
}

/// <inheritdoc/>
/// <nodocs/>
public static bool operator !=(StringValue i, string? other)
{
return i.Value != other;
return !i.Equals(other);
}

/// <inheritdoc/>
/// <nodocs/>
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
if (obj is null)
{
return true;
return IsNull;
}
if (ReferenceEquals(obj, null))
else if (obj is StringValue sv)
{
return false;
return Value?.Equals(sv.Value) == true;
}
if (obj is StringValue val)
else if (obj is string ss)
{
return val.Value?.Equals(this.Value) == true;
return Value?.Equals(ss) == true;
}
return false;
else
{
return false;
}
}

/// <inheritdoc/>
/// <nodocs/>
public override int GetHashCode()
{
return Value?.GetHashCode() ?? 0;
}
}
60 changes: 58 additions & 2 deletions src/Entity/StringValueCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,36 @@

using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;

namespace Sisk.Core.Entity;

/// <summary>
/// Represents an collection of <see cref="StringValue"/>.
/// </summary>
/// <definition>
/// public sealed class StringValueCollection : IEnumerable{{StringValue}}, IEnumerable{{KeyValuePair{{string, string}}}}
/// public sealed class StringValueCollection : IEnumerable{{StringValue}}, IEnumerable{{KeyValuePair{{string, string}}}},
/// IReadOnlyDictionary{{string, StringValue}}
/// </definition>
/// <type>
/// Class
/// </type>
public sealed class StringValueCollection : IEnumerable<StringValue>, IEnumerable<KeyValuePair<string, string>>
public sealed class StringValueCollection : IEnumerable<StringValue>, IEnumerable<KeyValuePair<string, string>>, IReadOnlyDictionary<string, StringValue>
{
internal Dictionary<string, string?> items;
internal string paramName;

/// <summary>
/// Represents an empty <see cref="StringValueCollection"/> field.
/// </summary>
/// <definition>
/// public static readonly StringValueCollection Empty;
/// </definition>
/// <type>
/// Field
/// </type>
public static readonly StringValueCollection Empty = new StringValueCollection("empty");

internal static StringValueCollection FromNameValueCollection(string paramName, NameValueCollection col)
{
StringValueCollection vcol = new StringValueCollection(paramName);
Expand Down Expand Up @@ -105,6 +118,23 @@ public NameValueCollection AsNameValueCollection()
/// </type>
public int Count { get => items.Count; }

/// <inheritdoc/>
/// <nodoc/>
public IEnumerable<string> Keys => items.Keys;

/// <inheritdoc/>
/// <nodoc/>
public IEnumerable<StringValue> Values
{
get
{
foreach (var item in items)
{
yield return new StringValue(item.Key, paramName, item.Value);
}
}
}

/// <summary>
/// Gets an <see cref="StringValue"/> item by their key name.
/// </summary>
Expand Down Expand Up @@ -157,6 +187,32 @@ IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, strin
return items.GetEnumerator();
}

/// <inheritdoc/>
/// <nodoc/>
public bool ContainsKey(string key)
{
return items.ContainsKey(key);
}

/// <inheritdoc/>
/// <nodoc/>
public bool TryGetValue(string key, [NotNull()] out StringValue value)
{
var sv = GetItem(key);
value = sv;
return sv.IsNull;
}

/// <inheritdoc/>
/// <nodoc/>
IEnumerator<KeyValuePair<string, StringValue>> IEnumerable<KeyValuePair<string, StringValue>>.GetEnumerator()
{
foreach (string key in items.Keys)
{
yield return new KeyValuePair<string, StringValue>(key, new StringValue(key, paramName, items[key]));
}
}

/// <inheritdoc/>
/// <nodocs/>
public static implicit operator Dictionary<string, string?>(StringValueCollection vcol)
Expand Down
Loading

0 comments on commit 91fa64a

Please sign in to comment.