Skip to content

Commit

Permalink
😊 新增 新流变(粘土)对象支持嵌套类型方式初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Jan 11, 2025
1 parent eea8ab0 commit 81ad023
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay
public partial class Clay
{
/// <summary>
/// 值变更之前事件
Expand Down
52 changes: 49 additions & 3 deletions framework/Furion.Pure/V5_Experience/Shapeless/Clay/Clay.Exports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay
public partial class Clay
{
/// <summary>
/// <inheritdoc cref="Clay" />
Expand Down Expand Up @@ -339,12 +339,26 @@ public bool Contains(object keyOrIndex)
/// <returns>
/// <see cref="object" />
/// </returns>
/// <exception cref="InvalidCastException"></exception>
public object? Get(object keyOrIndex, Type resultType, JsonSerializerOptions? jsonSerializerOptions = null)
{
// 尝试根据键获取委托
if (TryGetDelegate(keyOrIndex, out var @delegate))
{
// 空检查或检查目标委托类型是否一致
if (@delegate is null || @delegate.GetType() == resultType)
{
return @delegate;
}

throw new InvalidCastException(
$"The delegate type `{@delegate.GetType().FullName}` cannot be cast to the target type `{resultType.FullName}`.");
}

// 根据键或索引查找 JsonNode 节点
var jsonNode = FindNode(keyOrIndex);

return resultType == typeof(Clay)
return IsClay(resultType)
? new Clay(jsonNode, Options)
: jsonNode.As(resultType, jsonSerializerOptions ?? Options.JsonSerializerOptions);
}
Expand Down Expand Up @@ -438,7 +452,7 @@ public bool Add(object? value)
/// <see cref="object" />
/// </returns>
public object? As(Type resultType, JsonSerializerOptions? jsonSerializerOptions = null) =>
resultType == typeof(Clay)
IsClay(resultType)
? this
: JsonCanvas.As(resultType, jsonSerializerOptions ?? Options.JsonSerializerOptions);

Expand Down Expand Up @@ -721,4 +735,36 @@ public string ToXmlString(XmlWriterSettings? xmlWriterSettings = null)

return stringWriter.ToString();
}

/// <summary>
/// 单一对象
/// </summary>
public sealed class Object : Clay
{
/// <summary>
/// <inheritdoc cref="Object" />
/// </summary>
/// <param name="options">
/// <see cref="ClayOptions" />
/// </param>
public Object(ClayOptions? options = null) : base(options)
{
}
}

/// <summary>
/// 集合/数组
/// </summary>
public sealed class Array : Clay
{
/// <summary>
/// <inheritdoc cref="Array" />
/// </summary>
/// <param name="options">
/// <see cref="ClayOptions" />
/// </param>
public Array(ClayOptions? options = null) : base(ClayType.Array, options)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay
public partial class Clay
{
/// <summary>
/// 获取 <see cref="InvokeMemberBinder" /> 类型的 <c>TypeArguments</c> 属性访问器
Expand Down
13 changes: 12 additions & 1 deletion framework/Furion.Pure/V5_Experience/Shapeless/Clay/Clay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay : DynamicObject, IEnumerable<KeyValuePair<object, object?>>, IFormattable
public partial class Clay : DynamicObject, IEnumerable<KeyValuePair<object, object?>>, IFormattable
{
/// <summary>
/// <inheritdoc cref="Clay" />
Expand Down Expand Up @@ -729,4 +729,15 @@ internal void ThrowIfMethodCalledOnArrayCollection(string method)
$"`{method}` method can only be used for single object operations.");
}
}

/// <summary>
/// 检查类型是否是 <see cref="Clay" /> 类型
/// </summary>
/// <param name="type">
/// <see cref="Type" />
/// </param>
/// <returns>
/// <see cref="bool" />
/// </returns>
internal static bool IsClay(Type type) => type == typeof(Clay) || typeof(Clay).IsAssignableFrom(type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay
public partial class Clay
{
/// <summary>
/// 值变更之前事件
Expand Down
52 changes: 49 additions & 3 deletions framework/Furion/V5_Experience/Shapeless/Clay/Clay.Exports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay
public partial class Clay
{
/// <summary>
/// <inheritdoc cref="Clay" />
Expand Down Expand Up @@ -339,12 +339,26 @@ public bool Contains(object keyOrIndex)
/// <returns>
/// <see cref="object" />
/// </returns>
/// <exception cref="InvalidCastException"></exception>
public object? Get(object keyOrIndex, Type resultType, JsonSerializerOptions? jsonSerializerOptions = null)
{
// 尝试根据键获取委托
if (TryGetDelegate(keyOrIndex, out var @delegate))
{
// 空检查或检查目标委托类型是否一致
if (@delegate is null || @delegate.GetType() == resultType)
{
return @delegate;
}

throw new InvalidCastException(
$"The delegate type `{@delegate.GetType().FullName}` cannot be cast to the target type `{resultType.FullName}`.");
}

// 根据键或索引查找 JsonNode 节点
var jsonNode = FindNode(keyOrIndex);

return resultType == typeof(Clay)
return IsClay(resultType)
? new Clay(jsonNode, Options)
: jsonNode.As(resultType, jsonSerializerOptions ?? Options.JsonSerializerOptions);
}
Expand Down Expand Up @@ -438,7 +452,7 @@ public bool Add(object? value)
/// <see cref="object" />
/// </returns>
public object? As(Type resultType, JsonSerializerOptions? jsonSerializerOptions = null) =>
resultType == typeof(Clay)
IsClay(resultType)
? this
: JsonCanvas.As(resultType, jsonSerializerOptions ?? Options.JsonSerializerOptions);

Expand Down Expand Up @@ -721,4 +735,36 @@ public string ToXmlString(XmlWriterSettings? xmlWriterSettings = null)

return stringWriter.ToString();
}

/// <summary>
/// 单一对象
/// </summary>
public sealed class Object : Clay
{
/// <summary>
/// <inheritdoc cref="Object" />
/// </summary>
/// <param name="options">
/// <see cref="ClayOptions" />
/// </param>
public Object(ClayOptions? options = null) : base(options)
{
}
}

/// <summary>
/// 集合/数组
/// </summary>
public sealed class Array : Clay
{
/// <summary>
/// <inheritdoc cref="Array" />
/// </summary>
/// <param name="options">
/// <see cref="ClayOptions" />
/// </param>
public Array(ClayOptions? options = null) : base(ClayType.Array, options)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay
public partial class Clay
{
/// <summary>
/// 获取 <see cref="InvokeMemberBinder" /> 类型的 <c>TypeArguments</c> 属性访问器
Expand Down
13 changes: 12 additions & 1 deletion framework/Furion/V5_Experience/Shapeless/Clay/Clay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Furion.Shapeless;
/// <summary>
/// 流变对象
/// </summary>
public sealed partial class Clay : DynamicObject, IEnumerable<KeyValuePair<object, object?>>, IFormattable
public partial class Clay : DynamicObject, IEnumerable<KeyValuePair<object, object?>>, IFormattable
{
/// <summary>
/// <inheritdoc cref="Clay" />
Expand Down Expand Up @@ -729,4 +729,15 @@ internal void ThrowIfMethodCalledOnArrayCollection(string method)
$"`{method}` method can only be used for single object operations.");
}
}

/// <summary>
/// 检查类型是否是 <see cref="Clay" /> 类型
/// </summary>
/// <param name="type">
/// <see cref="Type" />
/// </param>
/// <returns>
/// <see cref="bool" />
/// </returns>
internal static bool IsClay(Type type) => type == typeof(Clay) || typeof(Clay).IsAssignableFrom(type);
}

0 comments on commit 81ad023

Please sign in to comment.