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.

146 lines
7.0 KiB

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