Skip to content

Commit

Permalink
😊 改进 新流变(粘土)对象 LambdaLinq 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Jan 13, 2025
1 parent 10be2f0 commit 5f90c13
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 198 deletions.
134 changes: 134 additions & 0 deletions framework/Furion.Pure/V5_Experience/Shapeless/Clay/Clay.Enumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// ------------------------------------------------------------------------
// 版权信息
// 版权归百小僧及百签科技(广东)有限公司所有。
// 所有权利保留。
// 官方网站:https://baiqian.com
//
// 许可证信息
// Furion 项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。
// 许可证的完整文本可以在源代码树根目录中的 LICENSE-APACHE 和 LICENSE-MIT 文件中找到。
// 官方网站:https://furion.net
//
// 使用条款
// 使用本代码应遵守相关法律法规和许可证的要求。
//
// 免责声明
// 对于因使用本代码而产生的任何直接、间接、偶然、特殊或后果性损害,我们不承担任何责任。
//
// 其他重要信息
// Furion 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。
// 有关 Furion 项目的其他详细信息,请参阅位于源代码树根目录中的 COPYRIGHT 和 DISCLAIMER 文件。
//
// 更多信息
// 请访问 https://gitee.com/dotnetchina/Furion 获取更多关于 Furion 项目的许可证和版权信息。
// ------------------------------------------------------------------------

namespace Furion.Shapeless;

/// <summary>
/// 流变对象
/// </summary>
public partial class Clay
{
/// <summary>
/// 元素数量
/// </summary>
public int Count => IsObject ? JsonCanvas.AsObject().Count : JsonCanvas.AsArray().Count;

/// <summary>
/// 元素数量
/// </summary>
public int Length => Count;

/// <summary>
/// 是否为空元素
/// </summary>
public bool IsEmpty => Count == 0;

/// <summary>
/// 获取键或索引集合
/// </summary>
public IEnumerable<object> Indexes => AsEnumerable().Select(u => u.Key);

/// <summary>
/// 获取键或索引集合
/// </summary>
public IEnumerable<object> Keys => Indexes;

/// <summary>
/// 获取值集合
/// </summary>
public IEnumerable<dynamic?> Values => AsEnumerable().Select(u => u.Value);

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// 返回循环访问元素的枚举数
/// </summary>
/// <returns>
/// <see cref="IEnumerator{T}" />
/// </returns>
public IEnumerator<KeyValuePair<object, dynamic?>> GetEnumerator() => AsEnumerable().GetEnumerator();

/// <summary>
/// 返回类型化为 <see cref="IEnumerable{T}" /> 的输入
/// </summary>
/// <returns>
/// <see cref="IEnumerable{T}" />
/// </returns>
public IEnumerable<KeyValuePair<object, dynamic?>> AsEnumerable() => IsObject
? AsEnumerableObject().Select(u => new KeyValuePair<object, dynamic?>(u.Key, u.Value))
: AsEnumerableArray().Select(u => new KeyValuePair<object, dynamic?>(u.Key, u.Value));

/// <summary>
/// 枚举 <see cref="JsonCanvas" /> 作为对象时的键值对
/// </summary>
/// <returns>
/// <see cref="IEnumerable{T}" />
/// </returns>
public IEnumerable<KeyValuePair<string, dynamic?>> AsEnumerableObject()
{
// 检查是否是集合(数组)实例调用
ThrowIfMethodCalledOnArrayCollection(nameof(AsEnumerableObject));

// 获取循环访问 JsonObject 的枚举数
using var enumerator = JsonCanvas.AsObject().GetEnumerator();

// 遍历 JsonObject 每个键值对
while (enumerator.MoveNext())
{
// 获取当前的键值对
var current = enumerator.Current;

yield return new KeyValuePair<string, dynamic?>(current.Key, DeserializeNode(current.Value, Options));
}
}

/// <summary>
/// 枚举 <see cref="JsonCanvas" /> 作为数组时的元素
/// </summary>
/// <returns>
/// <see cref="IEnumerable{T}" />
/// </returns>
public IEnumerable<KeyValuePair<int, dynamic?>> AsEnumerableArray()
{
// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(AsEnumerableArray));

// 获取循环访问 JsonArray 的枚举数
using var enumerator = JsonCanvas.AsArray().GetEnumerator();

// 定义索引变量用于记录数组中元素的位置
var index = 0;

// 遍历 JsonArray 每个元素
while (enumerator.MoveNext())
{
// 获取当前的元素
var current = enumerator.Current;

yield return new KeyValuePair<int, dynamic?>(index++, DeserializeNode(current, Options));
}
}
}
47 changes: 2 additions & 45 deletions framework/Furion.Pure/V5_Experience/Shapeless/Clay/Clay.Exports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,45 +112,12 @@ public object? this[int index]
/// </summary>
public bool IsArray { get; }

/// <summary>
/// 元素数量
/// </summary>
public int Count => IsObject ? JsonCanvas.AsObject().Count : JsonCanvas.AsArray().Count;

/// <summary>
/// 元素数量
/// </summary>
public int Length => Count;

/// <summary>
/// 是否为空元素
/// </summary>
public bool IsEmpty => Count == 0;

/// <summary>
/// 获取键或索引集合
/// </summary>
public IEnumerable<object> Indexes => AsEnumerable().Select(u => u.Key);

/// <summary>
/// 获取值集合
/// </summary>
public IEnumerable<dynamic?> Values => AsEnumerable().Select(u => u.Value);

// /// <summary>
// /// 反序列化时没有匹配的属性字典集合
// /// </summary>
// [JsonExtensionData]
// public Dictionary<object, object?> Extensions { get; set; } = new();

/// <summary>
/// 返回循环访问元素的枚举数
/// </summary>
/// <returns>
/// <see cref="IEnumerator{T}" />
/// </returns>
public IEnumerator<KeyValuePair<object, dynamic?>> GetEnumerator() => AsEnumerable().GetEnumerator();

/// <inheritdoc />
public string ToString(string? format, IFormatProvider? formatProvider)
{
Expand Down Expand Up @@ -200,16 +167,6 @@ public string ToString(string? format, IFormatProvider? formatProvider)
return ToJsonString(jsonSerializerOptions);
}

/// <summary>
/// 返回类型化为 <see cref="IEnumerable{T}" /> 的输入
/// </summary>
/// <returns>
/// <see cref="IEnumerable{T}" />
/// </returns>
public IEnumerable<KeyValuePair<object, dynamic?>> AsEnumerable() => IsObject
? EnumerateObject().Select(u => new KeyValuePair<object, dynamic?>(u.Key, u.Value))
: EnumerateArray().Select(u => new KeyValuePair<object, dynamic?>(u.Key, u.Value));

/// <summary>
/// 创建空的单一对象
/// </summary>
Expand Down Expand Up @@ -564,13 +521,13 @@ public bool Pop()
// 检查是否是 IEnumerable<KeyValuePair<string, object?>> 类型且是单一对象
if (resultType == typeof(IEnumerable<KeyValuePair<string, object?>>) && IsObject)
{
return EnumerateObject();
return AsEnumerableObject();
}

// 检查是否是 IEnumerable<KeyValuePair<int, object?>> 类型且是集合/数组
if (resultType == typeof(IEnumerable<KeyValuePair<int, object?>>) && IsArray)
{
return EnumerateArray();
return AsEnumerableArray();
}

return Helpers.DeserializeNode(JsonCanvas, resultType, jsonSerializerOptions ?? Options.JsonSerializerOptions);
Expand Down
54 changes: 0 additions & 54 deletions framework/Furion.Pure/V5_Experience/Shapeless/Clay/Clay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ internal Clay(JsonNode? jsonNode, ClayOptions? options = null)
/// </summary>
internal IDictionary<string, Delegate?> ObjectMethods { get; } = new Dictionary<string, Delegate?>();

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// 根据键或索引获取值
/// </summary>
Expand Down Expand Up @@ -559,57 +556,6 @@ internal Clay Rebuilt(ClayOptions? options = null)
internal string ProcessNestedNullPropagationIndexKey(string indexKey) =>
!Options.AutoCreateNestedObjects ? indexKey : indexKey.TrimEnd('?');

/// <summary>
/// 枚举 <see cref="JsonCanvas" /> 作为对象时的键值对
/// </summary>
/// <returns>
/// <see cref="IEnumerable{T}" />
/// </returns>
internal IEnumerable<KeyValuePair<string, dynamic?>> EnumerateObject()
{
// 检查是否是集合(数组)实例调用
ThrowIfMethodCalledOnArrayCollection(nameof(EnumerateObject));

// 获取循环访问 JsonObject 的枚举数
using var enumerator = JsonCanvas.AsObject().GetEnumerator();

// 遍历 JsonObject 每个键值对
while (enumerator.MoveNext())
{
// 获取当前的键值对
var current = enumerator.Current;

yield return new KeyValuePair<string, dynamic?>(current.Key, DeserializeNode(current.Value, Options));
}
}

/// <summary>
/// 枚举 <see cref="JsonCanvas" /> 作为数组时的元素
/// </summary>
/// <returns>
/// <see cref="IEnumerable{T}" />
/// </returns>
internal IEnumerable<KeyValuePair<int, dynamic?>> EnumerateArray()
{
// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(EnumerateArray));

// 获取循环访问 JsonArray 的枚举数
using var enumerator = JsonCanvas.AsArray().GetEnumerator();

// 定义索引变量用于记录数组中元素的位置
var index = 0;

// 遍历 JsonArray 每个元素
while (enumerator.MoveNext())
{
// 获取当前的元素
var current = enumerator.Current;

yield return new KeyValuePair<int, dynamic?>(index++, DeserializeNode(current, Options));
}
}

/// <summary>
/// 创建 <see cref="JsonNode" /> 选项
/// </summary>
Expand Down
Loading

0 comments on commit 5f90c13

Please sign in to comment.