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.

151 lines
6.9 KiB

  1. #nullable enable
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace IPA.Config.Data
  10. {
  11. /// <summary>
  12. /// A base value type for config data abstract representations, to be serialized with an
  13. /// <see cref="IConfigProvider"/>. If a <see cref="Value"/> is <see langword="null"/>, then
  14. /// that represents just that: a <c>null</c> in whatever serialization is being used.
  15. /// Also contains factory functions for all derived types.
  16. /// </summary>
  17. public abstract class Value
  18. {
  19. /// <summary>
  20. /// Converts this <see cref="Value"/> into a human-readable format.
  21. /// </summary>
  22. /// <returns>a human-readable string containing the value provided</returns>
  23. public abstract override string ToString();
  24. /// <summary>
  25. /// Creates a Null <see cref="Value"/>.
  26. /// </summary>
  27. /// <returns><see langword="null"/></returns>
  28. public static Value? Null() => null;
  29. /// <summary>
  30. /// Creates an empty <see cref="List"/>.
  31. /// </summary>
  32. /// <returns>an empty <see cref="List"/></returns>
  33. /// <seealso cref="From(IEnumerable{Value})"/>
  34. public static List List() => new();
  35. /// <summary>
  36. /// Creates an empty <see cref="Map"/>.
  37. /// </summary>
  38. /// <returns>an empty <see cref="Map"/></returns>
  39. /// <seealso cref="From(IDictionary{string, Value})"/>
  40. /// <seealso cref="From(IEnumerable{KeyValuePair{string, Value}})"/>
  41. public static Map Map() => new();
  42. /// <summary>
  43. /// Creates a new <see cref="Value"/> representing a <see cref="string"/>.
  44. /// </summary>
  45. /// <param name="val">the value to wrap</param>
  46. /// <returns>a <see cref="Data.Text"/> wrapping <paramref name="val"/></returns>
  47. /// <seealso cref="Text(string)"/>
  48. [return: NotNullIfNotNull("val")]
  49. public static Text? From(string? val) => Text(val);
  50. /// <summary>
  51. /// Creates a new <see cref="Data.Text"/> object wrapping a <see cref="string"/>.
  52. /// </summary>
  53. /// <param name="val">the value to wrap</param>
  54. /// <returns>a <see cref="Data.Text"/> wrapping <paramref name="val"/></returns>
  55. /// <seealso cref="From(string)"/>
  56. [return: NotNullIfNotNull("val")]
  57. public static Text? Text(string? val) => val == null ? null : new(val);
  58. /// <summary>
  59. /// Creates a new <see cref="Value"/> wrapping a <see cref="long"/>.
  60. /// </summary>
  61. /// <param name="val">the value to wrap</param>
  62. /// <returns>a <see cref="Data.Integer"/> wrapping <paramref name="val"/></returns>
  63. /// <seealso cref="Integer(long)"/>
  64. public static Integer From(long val) => Integer(val);
  65. /// <summary>
  66. /// Creates a new <see cref="Data.Integer"/> wrapping a <see cref="long"/>.
  67. /// </summary>
  68. /// <param name="val">the value to wrap</param>
  69. /// <returns>a <see cref="Data.Integer"/> wrapping <paramref name="val"/></returns>
  70. /// <seealso cref="From(long)"/>
  71. public static Integer Integer(long val) => new(val);
  72. /// <summary>
  73. /// Creates a new <see cref="Value"/> wrapping a <see cref="double"/>.
  74. /// </summary>
  75. /// <param name="val">the value to wrap</param>
  76. /// <returns>a <see cref="FloatingPoint"/> wrapping <paramref name="val"/></returns>
  77. /// <seealso cref="Float(decimal)"/>
  78. public static FloatingPoint From(decimal val) => Float(val);
  79. /// <summary>
  80. /// Creates a new <see cref="FloatingPoint"/> wrapping a <see cref="decimal"/>.
  81. /// </summary>
  82. /// <param name="val">the value to wrap</param>
  83. /// <returns>a <see cref="FloatingPoint"/> wrapping <paramref name="val"/></returns>
  84. /// <seealso cref="From(decimal)"/>
  85. public static FloatingPoint Float(decimal val) => new(val);
  86. /// <summary>
  87. /// Creates a new <see cref="Value"/> wrapping a <see cref="bool"/>.
  88. /// </summary>
  89. /// <param name="val">the value to wrap</param>
  90. /// <returns>a <see cref="Boolean"/> wrapping <paramref name="val"/></returns>
  91. /// <seealso cref="Bool(bool)"/>
  92. public static Boolean From(bool val) => Bool(val);
  93. /// <summary>
  94. /// Creates a new <see cref="Boolean"/> wrapping a <see cref="bool"/>.
  95. /// </summary>
  96. /// <param name="val">the value to wrap</param>
  97. /// <returns>a <see cref="Boolean"/> wrapping <paramref name="val"/></returns>
  98. /// <seealso cref="From(bool)"/>
  99. public static Boolean Bool(bool val) => new(val);
  100. /// <summary>
  101. /// Creates a new <see cref="Data.List"/> holding the content of an <see cref="IEnumerable{T}"/>
  102. /// of <see cref="Value"/>.
  103. /// </summary>
  104. /// <param name="vals">the <see cref="Value"/>s to initialize the <see cref="Data.List"/> with</param>
  105. /// <returns>a <see cref="Data.List"/> containing the content of <paramref name="vals"/></returns>
  106. /// <seealso cref="List"/>
  107. [return: NotNullIfNotNull("vals")]
  108. public static List? From(IEnumerable<Value?>? vals)
  109. {
  110. if (vals is null) return null;
  111. var l = List();
  112. l.AddRange(vals);
  113. return l;
  114. }
  115. /// <summary>
  116. /// Creates a new <see cref="Data.Map"/> holding the content of an <see cref="IDictionary{TKey, TValue}"/>
  117. /// of <see cref="string"/> to <see cref="Value"/>.
  118. /// </summary>
  119. /// <param name="vals">the dictionary of <see cref="Value"/>s to initialize the <see cref="Data.Map"/> wtih</param>
  120. /// <returns>a <see cref="Data.Map"/> containing the content of <paramref name="vals"/></returns>
  121. /// <seealso cref="Map"/>
  122. /// <seealso cref="From(IEnumerable{KeyValuePair{string, Value}})"/>
  123. public static Map From(IDictionary<string, Value?> vals) => From(vals as IEnumerable<KeyValuePair<string, Value?>>);
  124. /// <summary>
  125. /// Creates a new <see cref="Data.Map"/> holding the content of an <see cref="IEnumerable{T}"/>
  126. /// of <see cref="KeyValuePair{TKey, TValue}"/> of <see cref="string"/> to <see cref="Value"/>.
  127. /// </summary>
  128. /// <param name="vals">the enumerable of <see cref="KeyValuePair{TKey, TValue}"/> of name to <see cref="Value"/></param>
  129. /// <returns>a <see cref="Data.Map"/> containing the content of <paramref name="vals"/></returns>
  130. /// <seealso cref="Map"/>
  131. /// <seealso cref="From(IDictionary{string, Value})"/>
  132. [return: NotNullIfNotNull("vals")]
  133. public static Map? From(IEnumerable<KeyValuePair<string, Value?>>? vals)
  134. {
  135. if (vals is null) return null;
  136. var m = Map();
  137. foreach (var v in vals) m.Add(v.Key, v.Value);
  138. return m;
  139. }
  140. }
  141. }