diff --git a/internal/api/friend.go b/internal/api/friend.go index fd4369e41..8fe9033d5 100644 --- a/internal/api/friend.go +++ b/internal/api/friend.go @@ -48,9 +48,7 @@ func (o *FriendApi) AddBlack(c *gin.Context) { } func (o *FriendApi) GetPaginationBlacks(c *gin.Context) { - a2r.Call(friend.FriendClient.GetPaginationBlacks, o.Client, c, func(resp *friend.GetPaginationBlacksResp) { - a2r.List(&resp.Blacks) - }) + a2r.Call(friend.FriendClient.GetPaginationBlacks, o.Client, c) } func (o *FriendApi) RemoveBlack(c *gin.Context) { diff --git a/pkg/a2r/api2rpc.go b/pkg/a2r/api2rpc.go index 9cd1c5c17..50d5b2ac3 100644 --- a/pkg/a2r/api2rpc.go +++ b/pkg/a2r/api2rpc.go @@ -11,17 +11,10 @@ import ( "google.golang.org/grpc" ) -func List[T any](val *[]T) { - if val != nil && *val == nil { - *val = []T{} - } -} - func Call[A, B, C any]( rpc func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), client C, c *gin.Context, - after ...func(resp *B), ) { var req A if err := c.BindJSON(&req); err != nil { @@ -38,8 +31,5 @@ func Call[A, B, C any]( apiresp.GinError(c, err) // RPC调用失败 return } - for _, fn := range after { - fn(data) - } apiresp.GinSuccess(c, data) // 成功 } diff --git a/pkg/apiresp/format.go b/pkg/apiresp/format.go new file mode 100644 index 000000000..34fb85464 --- /dev/null +++ b/pkg/apiresp/format.go @@ -0,0 +1,5 @@ +package apiresp + +type ApiFormat interface { + ApiFormat() +} diff --git a/pkg/apiresp/resp.go b/pkg/apiresp/resp.go index cd215a984..cd4b45b54 100644 --- a/pkg/apiresp/resp.go +++ b/pkg/apiresp/resp.go @@ -34,6 +34,9 @@ func isAllFieldsPrivate(v any) bool { } func ApiSuccess(data any) *ApiResponse { + if format, ok := data.(ApiFormat); ok { + format.ApiFormat() + } if isAllFieldsPrivate(data) { return &ApiResponse{} } diff --git a/pkg/utils/utils_v2.go b/pkg/utils/utils_v2.go index 777308fd7..ee5a3453c 100644 --- a/pkg/utils/utils_v2.go +++ b/pkg/utils/utils_v2.go @@ -529,3 +529,15 @@ func Batch[T any, V any](fn func(T) V, ts []T) []V { } return res } + +func InitSlice[T any](val *[]T) { + if val != nil && *val == nil { + *val = []T{} + } +} + +func InitMap[K comparable, V any](val *map[K]V) { + if val != nil && *val == nil { + *val = map[K]V{} + } +}