Browse Source

Formatting

pull/44/head
Anairkoen Schno 4 years ago
parent
commit
598d5622fd
2 changed files with 84 additions and 88 deletions
  1. +9
    -13
      IPA.Loader/Config/Stores/Attributes.cs
  2. +75
    -75
      IPA.Loader/Config/Stores/Converters.cs

+ 9
- 13
IPA.Loader/Config/Stores/Attributes.cs View File

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IPA.Config.Stores.Attributes
{
@ -23,7 +20,7 @@ namespace IPA.Config.Stores.Attributes
/// <summary>
/// Indicates that a field or property in an object being wrapped by <see cref="GeneratedStore.Generated{T}(Config, bool)"/>
/// that would otherwise be nullable (i.e. a reference type or a <see cref="Nullable{T}"/> type) should never be null, and the
/// that would otherwise be nullable (i.e. a reference type or a <see cref="Nullable{T}"/> type) should never be null, and the
/// member will be ignored if the deserialized value is <see langword="null"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
@ -31,7 +28,7 @@ namespace IPA.Config.Stores.Attributes
/// <summary>
/// Indicates that a given field or property in an object being wrapped by <see cref="GeneratedStore.Generated{T}(Config, bool)"/>
/// should be serialized and deserialized using the provided converter instead of the default mechanism.
/// should be serialized and deserialized using the provided converter instead of the default mechanism.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class UseConverterAttribute : Attribute
@ -40,6 +37,7 @@ namespace IPA.Config.Stores.Attributes
/// Gets the type of the converter to use.
/// </summary>
public Type ConverterType { get; }
/// <summary>
/// Gets the target type of the converter if it is avaliable at instantiation time, otherwise
/// <see langword="null"/>.
@ -60,7 +58,7 @@ namespace IPA.Config.Stores.Attributes
ConverterType = converterType;
var baseT = ConverterType.BaseType;
while (baseT != null && baseT != typeof(object) &&
while (baseT != null && baseT != typeof(object) &&
(!baseT.IsGenericType || baseT.GetGenericTypeDefinition() != typeof(ValueConverter<>)))
baseT = baseT.BaseType;
if (baseT == typeof(object)) ConverterTargetType = null;
@ -73,14 +71,14 @@ namespace IPA.Config.Stores.Attributes
}
/// <summary>
/// Specifies a name for the serialized field or property in an object being wrapped by
/// Specifies a name for the serialized field or property in an object being wrapped by
/// <see cref="GeneratedStore.Generated{T}(Config, bool)"/> that is different from the member name itself.
/// </summary>
/// <example>
/// <para>
/// When serializing the following object, we might get the JSON that follows.
/// <code>
/// public class PluginConfig
/// public class PluginConfig
/// {
/// public virtual bool BooleanField { get; set; } = true;
/// }
@ -94,7 +92,7 @@ namespace IPA.Config.Stores.Attributes
/// <para>
/// However, if we were to add a <see cref="SerializedNameAttribute"/> to that field, we would get the following.
/// <code>
/// public class PluginConfig
/// public class PluginConfig
/// {
/// [SerializedName("bool")]
/// public virtual bool BooleanField { get; set; } = true;
@ -108,7 +106,7 @@ namespace IPA.Config.Stores.Attributes
/// </para>
/// </example>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class SerializedNameAttribute : Attribute
public sealed class SerializedNameAttribute : Attribute
{
/// <summary>
/// Gets the name to replace the member name with.
@ -124,6 +122,4 @@ namespace IPA.Config.Stores.Attributes
Name = name;
}
}
}
}

+ 75
- 75
IPA.Loader/Config/Stores/Converters.cs View File

@ -178,91 +178,91 @@ namespace IPA.Config.Stores.Converters
public NullableConverter() : base(new TConverter()) { }
}
/// <summary>
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back.
/// </summary>
/// <summary>
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back.
/// </summary>
/// <typeparam name="T">the enum type</typeparam>
public sealed class EnumConverter<T> : ValueConverter<T>
where T : Enum
{
/// <summary>
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized enum value</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
public override T FromValue(Value value, object parent)
=> value is Text t
? (T)Enum.Parse(typeof(T), t.Value)
: throw new ArgumentException("Value not a string", nameof(value));
/// <summary>
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
/// </summary>
/// <param name="obj">the value to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(T obj, object parent)
=> Value.Text(obj.ToString());
where T : Enum
{
/// <summary>
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized enum value</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
public override T FromValue(Value value, object parent)
=> value is Text t
? (T)Enum.Parse(typeof(T), t.Value)
: throw new ArgumentException("Value not a string", nameof(value));
/// <summary>
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
/// </summary>
/// <param name="obj">the value to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(T obj, object parent)
=> Value.Text(obj.ToString());
}
/// <summary>
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back,
/// ignoring the case of the serialized value for deseiralization.
/// </summary>
/// <summary>
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back,
/// ignoring the case of the serialized value for deseiralization.
/// </summary>
/// <typeparam name="T">the enum type</typeparam>
public sealed class CaseInsensitiveEnumConverter<T> : ValueConverter<T>
where T : Enum
{
/// <summary>
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized enum value</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
public override T FromValue(Value value, object parent)
=> value is Text t
? (T)Enum.Parse(typeof(T), t.Value, true)
: throw new ArgumentException("Value not a string", nameof(value));
/// <summary>
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
/// </summary>
/// <param name="obj">the value to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(T obj, object parent)
=> Value.Text(obj.ToString());
where T : Enum
{
/// <summary>
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized enum value</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
public override T FromValue(Value value, object parent)
=> value is Text t
? (T)Enum.Parse(typeof(T), t.Value, true)
: throw new ArgumentException("Value not a string", nameof(value));
/// <summary>
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
/// </summary>
/// <param name="obj">the value to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(T obj, object parent)
=> Value.Text(obj.ToString());
}
/// <summary>
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its underlying value for serialization.
/// </summary>
/// <summary>
/// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its underlying value for serialization.
/// </summary>
/// <typeparam name="T">the enum type</typeparam>
public sealed class NumericEnumConverter<T> : ValueConverter<T>
where T : Enum
{
/// <summary>
/// Converts a <see cref="Value"/> that is a numeric node to the corresponding enum value.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized enum value</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a numeric node</exception>
public override T FromValue(Value value, object parent)
=> (T)Enum.ToObject(typeof(T), Converter.IntValue(value)
?? throw new ArgumentException("Value not a numeric node", nameof(value)));
/// <summary>
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
/// </summary>
/// <param name="obj">the value to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>an <see cref="Integer"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(T obj, object parent)
=> Value.Integer(Convert.ToInt64(obj));
where T : Enum
{
/// <summary>
/// Converts a <see cref="Value"/> that is a numeric node to the corresponding enum value.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized enum value</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a numeric node</exception>
public override T FromValue(Value value, object parent)
=> (T)Enum.ToObject(typeof(T), Converter.IntValue(value)
?? throw new ArgumentException("Value not a numeric node", nameof(value)));
/// <summary>
/// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
/// </summary>
/// <param name="obj">the value to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>an <see cref="Integer"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(T obj, object parent)
=> Value.Integer(Convert.ToInt64(obj));
}
internal class StringConverter : ValueConverter<string>


Loading…
Cancel
Save