Skip to content

fix(container/gmap): added WithError methods for KVMap and ListKVMap#4714

Open
LanceAdd wants to merge 5 commits into
gogf:masterfrom
LanceAdd:fix/gmap
Open

fix(container/gmap): added WithError methods for KVMap and ListKVMap#4714
LanceAdd wants to merge 5 commits into
gogf:masterfrom
LanceAdd:fix/gmap

Conversation

@LanceAdd

Copy link
Copy Markdown
Member

新增内容

KVMapListKVMap 新增支持错误返回的回调方法变体:

方法 说明
GetOrSetFuncWithError 在锁外调用 f(),若 f 返回 error 则不写入
GetOrSetFuncLockWithError 在锁内调用 f(),若 f 返回 error 则不写入
SetIfNotExistFuncWithError 在锁外调用 f(),若 f 返回 error 则不写入
SetIfNotExistFuncLockWithError 在锁内调用 f(),若 f 返回 error 则不写入

以上方法在以下情况均不会向 map 写入值:

  • 回调函数 f() 返回 non-nil error
  • 回调函数 f() 返回 nil 值

同步修复

在实现过程中发现并修复了以下问题:

  1. ListKVMap.SetIfNotExistFunc 原实现在持锁状态下调用 f()
    SetIfNotExistFuncLock 行为完全相同,违反了命名约定
    Func = 锁外,FuncLock = 锁内)。已重构为在锁外调用 f()
    同时补充了缺失的 SetIfNotExistFuncLock 实现。

涉及文件

  • container/gmap/gmap_hash_k_v_map.go
  • container/gmap/gmap_list_k_v_map.go

- 在 KVMap 中新增 GetOrSetFuncWithError 和 GetOrSetFuncLockWithError 方法
- 在 KVMap 中新增 SetIfNotExistFuncWithError 和 SetIfNotExistFuncLockWithError 方法
- 在 ListKVMap 中新增 GetOrSetFuncWithError 和 GetOrSetFuncLockWithError 方法
- 在 ListKVMap 中新增 SetIfNotExistFuncWithError 和 SetIfNotExistFuncLockWithError 方法
- 修改 Instance 函数返回值处理逻辑,使用 GetOrSetFuncLockWithError 替代原有实现
- 所有新方法支持回调函数返回错误并进行相应处理

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds error-returning variants of callback-based methods to KVMap and ListKVMap, refactors the database Instance function to use the new API, and fixes a concurrency bug in ListKVMap.SetIfNotExistFunc.

Changes:

  • Added GetOrSetFuncWithError, GetOrSetFuncLockWithError, SetIfNotExistFuncWithError, and SetIfNotExistFuncLockWithError methods to both KVMap and ListKVMap to support callbacks that can return errors
  • Fixed ListKVMap.SetIfNotExistFunc to call the callback outside the lock (was incorrectly calling it inside the lock, making it identical to SetIfNotExistFuncLock)
  • Added the previously missing ListKVMap.SetIfNotExistFuncLock method
  • Refactored database/gdb/gdb.go Instance function to use GetOrSetFuncLockWithError, simplifying error handling

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
container/gmap/gmap_hash_k_v_map.go Added four new WithError method variants for KVMap to support error-returning callbacks
container/gmap/gmap_list_k_v_map.go Added four new WithError method variants, fixed SetIfNotExistFunc to call callback outside lock, added missing SetIfNotExistFuncLock
database/gdb/gdb.go Simplified Instance function to use GetOrSetFuncLockWithError for cleaner error handling

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread container/gmap/gmap_hash_k_v_map.go
Comment thread container/gmap/gmap_hash_k_v_map.go
Comment thread container/gmap/gmap_hash_k_v_map.go
Comment thread container/gmap/gmap_list_k_v_map.go
LanceAdd and others added 4 commits February 26, 2026 10:27
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants