Skip to content

Commit

Permalink
😊 完善 新流变(粘土)对象的数组操作方法
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Jan 12, 2025
1 parent 81ad023 commit 51ef1a5
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 4 deletions.
70 changes: 68 additions & 2 deletions framework/Furion.Pure/V5_Experience/Shapeless/Clay/Clay.Exports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,14 @@ public bool Contains(object keyOrIndex)
public void Set(object keyOrIndex, object? value) => SetValue(keyOrIndex, value);

/// <summary>
/// 在指定索引处插入值
/// 在指定索引处插入项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="index">索引</param>
/// <param name="value">值</param>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Insert(int index, object? value)
{
Expand All @@ -409,10 +412,13 @@ public bool Insert(int index, object? value)
}

/// <summary>
/// 在末尾处添加值
/// 在末尾处添加项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="value">值</param>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Add(object? value)
{
Expand All @@ -422,6 +428,66 @@ public bool Add(object? value)
return SetValue(JsonCanvas.AsArray().Count, value);
}

/// <summary>
/// 在末尾处批量添加项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="values">值集合</param>
/// <exception cref="NotSupportedException"></exception>
public void AddRange(params IEnumerable<object?> values)
{
// 空检查
ArgumentNullException.ThrowIfNull(values);

// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(AddRange));

// 将 JsonCanvas 转换为 JsonArray 实例
var jsonArray = JsonCanvas.AsArray();

// 逐条追加项
foreach (var value in values)
{
SetValue(jsonArray.Count, value);
}
}

/// <summary>
/// 在末尾处添加项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="value">值</param>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Push(object? value)
{
// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(Push));

return SetValue(JsonCanvas.AsArray().Count, value);
}

/// <summary>
/// 移除末尾处的项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Pop()
{
// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(Pop));

// 获取 JsonArray 最大索引
var maxIndex = JsonCanvas.AsArray().Count - 1;

return maxIndex > -1 && RemoveValue(maxIndex);
}

/// <summary>
/// 根据键或索引删除数据
/// </summary>
Expand Down
70 changes: 68 additions & 2 deletions framework/Furion/V5_Experience/Shapeless/Clay/Clay.Exports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,14 @@ public bool Contains(object keyOrIndex)
public void Set(object keyOrIndex, object? value) => SetValue(keyOrIndex, value);

/// <summary>
/// 在指定索引处插入值
/// 在指定索引处插入项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="index">索引</param>
/// <param name="value">值</param>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Insert(int index, object? value)
{
Expand All @@ -409,10 +412,13 @@ public bool Insert(int index, object? value)
}

/// <summary>
/// 在末尾处添加值
/// 在末尾处添加项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="value">值</param>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Add(object? value)
{
Expand All @@ -422,6 +428,66 @@ public bool Add(object? value)
return SetValue(JsonCanvas.AsArray().Count, value);
}

/// <summary>
/// 在末尾处批量添加项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="values">值集合</param>
/// <exception cref="NotSupportedException"></exception>
public void AddRange(params IEnumerable<object?> values)
{
// 空检查
ArgumentNullException.ThrowIfNull(values);

// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(AddRange));

// 将 JsonCanvas 转换为 JsonArray 实例
var jsonArray = JsonCanvas.AsArray();

// 逐条追加项
foreach (var value in values)
{
SetValue(jsonArray.Count, value);
}
}

/// <summary>
/// 在末尾处添加项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <param name="value">值</param>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Push(object? value)
{
// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(Push));

return SetValue(JsonCanvas.AsArray().Count, value);
}

/// <summary>
/// 移除末尾处的项
/// </summary>
/// <remarks>当 <see cref="IsArray" /> 为 <c>true</c> 时有效。</remarks>
/// <returns>
/// <see cref="bool" />
/// </returns>
/// <exception cref="NotSupportedException"></exception>
public bool Pop()
{
// 检查是否是单一对象实例调用
ThrowIfMethodCalledOnSingleObject(nameof(Pop));

// 获取 JsonArray 最大索引
var maxIndex = JsonCanvas.AsArray().Count - 1;

return maxIndex > -1 && RemoveValue(maxIndex);
}

/// <summary>
/// 根据键或索引删除数据
/// </summary>
Expand Down

0 comments on commit 51ef1a5

Please sign in to comment.