From 7ce64da8c71050e02b9c0945f9a03a7b50188b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 11 Oct 2023 18:03:47 +0900 Subject: [PATCH] reduce unsafe --- openapi.go | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/openapi.go b/openapi.go index 457d5b8..ecc9ed0 100644 --- a/openapi.go +++ b/openapi.go @@ -11,6 +11,14 @@ import ( "github.com/pkg/errors" ) +func checkrespbaseunsafe(ptr any) error { + respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0))))) + if respbase.C != 0 { + return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName()) + } + return nil +} + //go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel Member RoleMembers GuildRoleList ChannelPermissions Message // GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase @@ -40,11 +48,7 @@ func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error { if reflect.ValueOf(ptr).Elem().Kind() == reflect.Slice { return nil } - respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0))))) - if respbase.C != 0 { - return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName()) - } - return nil + return checkrespbaseunsafe(ptr) } //go:generate go run codegen/putopenapiof/main.go GuildRoleChannelID @@ -76,11 +80,7 @@ func (bot *Bot) PutOpenAPI(ep, contenttype string, ptr any, body io.Reader) erro if reflect.ValueOf(ptr).Elem().Kind() == reflect.Slice { return nil } - respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0))))) - if respbase.C != 0 { - return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName()) - } - return nil + return checkrespbaseunsafe(ptr) } // DeleteOpenAPI 向 ep 发送 DELETE 请求 @@ -132,11 +132,7 @@ func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) err if reflect.ValueOf(ptr).Elem().Kind() == reflect.Slice { return nil } - respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0))))) - if respbase.C != 0 { - return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName()) - } - return nil + return checkrespbaseunsafe(ptr) } //go:generate go run codegen/patchopenapiof/main.go Channel GuildRolePatch @@ -168,9 +164,5 @@ func (bot *Bot) PatchOpenAPI(ep, contenttype string, ptr any, body io.Reader) er if reflect.ValueOf(ptr).Elem().Kind() == reflect.Slice { return nil } - respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0))))) - if respbase.C != 0 { - return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName()) - } - return nil + return checkrespbaseunsafe(ptr) }