You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace IPA.Config.Stores.Attributes
  7. {
  8. /// <summary>
  9. /// Causes a field or property in an object being wrapped by <see cref="GeneratedExtension.Generated{T}(Config, bool)"/> to be
  10. /// ignored during serialization and deserialization.
  11. /// </summary>
  12. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  13. public sealed class IgnoreAttribute : Attribute { }
  14. /// <summary>
  15. /// Indicates that a field or property in an object being wrapped by <see cref="GeneratedExtension.Generated{T}(Config, bool)"/>
  16. /// that would otherwise be nullable (i.e. a reference type or a <see cref="Nullable{T}"/> type) should never be null, and the
  17. /// member will be ignored if the deserialized value is <see langword="null"/>.
  18. /// </summary>
  19. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  20. public sealed class NonNullableAttribute : Attribute { }
  21. /// <summary>
  22. /// Indicates that a given field or property in an object being wrapped by <see cref="GeneratedExtension.Generated{T}(Config, bool)"/>
  23. /// should be serialized and deserialized using the provided converter instead of the default mechanism.
  24. /// </summary>
  25. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  26. public sealed class UseConverterAttribute : Attribute
  27. {
  28. /// <summary>
  29. /// Gets the type of the converter to use.
  30. /// </summary>
  31. public Type ConverterType { get; private set; }
  32. /// <summary>
  33. /// Gets the target type of the converter if it is avaliable at instantiation time, otherwise
  34. /// <see langword="null"/>.
  35. /// </summary>
  36. public Type ConverterTargetType => ConverterType.BaseType.IsGenericType ?
  37. ConverterType.BaseType.GetGenericArguments()[0] :
  38. null;
  39. /// <summary>
  40. /// Creates a new <see cref="UseConverterAttribute"/> with a given <see cref="ConverterType"/>.
  41. /// </summary>
  42. /// <param name="converterType">tpy type to assign to <see cref="ConverterType"/></param>
  43. public UseConverterAttribute(Type converterType)
  44. {
  45. ConverterType = converterType;
  46. var implInterface = ConverterType.GetInterfaces().Contains(typeof(IValueConverter));
  47. var inheritGeneric = ConverterType.BaseType.IsGenericType &&
  48. ConverterType.BaseType.GetGenericTypeDefinition() == typeof(ValueConverter<>);
  49. if (!implInterface && !inheritGeneric) throw new ArgumentException("Type is not a value converter!");
  50. }
  51. }
  52. /// <summary>
  53. /// Specifies a name for the serialized field or property in an object being wrapped by
  54. /// <see cref="GeneratedExtension.Generated{T}(Config, bool)"/> that is different from the member name itself.
  55. /// </summary>
  56. /// <example>
  57. /// <para>
  58. /// When serializing the following object, we might get the JSON that follows.
  59. /// <code>
  60. /// public class PluginConfig
  61. /// {
  62. /// public virtual bool BooleanField { get; set; } = true;
  63. /// }
  64. /// </code>
  65. /// <code>
  66. /// {
  67. /// "BooleanField": true
  68. /// }
  69. /// </code>
  70. /// </para>
  71. /// <para>
  72. /// However, if we were to add a <see cref="SerializedNameAttribute"/> to that field, we would get the following.
  73. /// <code>
  74. /// public class PluginConfig
  75. /// {
  76. /// [SerializedName("bool")]
  77. /// public virtual bool BooleanField { get; set; } = true;
  78. /// }
  79. /// </code>
  80. /// <code>
  81. /// {
  82. /// "bool": true
  83. /// }
  84. /// </code>
  85. /// </para>
  86. /// </example>
  87. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  88. public sealed class SerializedNameAttribute : Attribute
  89. {
  90. /// <summary>
  91. /// Gets the name to replace the member name with.
  92. /// </summary>
  93. public string Name { get; private set; }
  94. /// <summary>
  95. /// Creates a new <see cref="SerializedNameAttribute"/> with the given <see cref="Name"/>.
  96. /// </summary>
  97. /// <param name="name">the value to assign to <see cref="Name"/></param>
  98. public SerializedNameAttribute(string name)
  99. {
  100. Name = name;
  101. }
  102. }
  103. }