hotfix icon indicating copy to clipboard operation
hotfix copied to clipboard

被替换的函数中如果包含第三方依赖怎么处理?

Open hhq163 opened this issue 7 months ago • 1 comments

phantacix,你好: 想要替换的是业务处理的函数,它会有一些第三方的依赖,如: github.com/golang/protobuf/proto xxx.com/root/DawnCore/basic //自建的项目公共库,里面包含日志类 。。。 而且还用到一些全局变量,如日志,连接器对象等,xxx.com是公司域名,有点敏感,就用xxx.com代替了

在项目中写一个例子尝试了下,会报错: [2023-11-09 11:21:00,222][0 0 Error 7 1]hotfixdo.go:19 hotfix.ApplyFunc(),err=./_patch_files/patch/patch.go:8:2: import "xxx.com/root/DawnCore/basic" error: unable to find source related to: "xxx.com/root/DawnCore/basic". Either the GOPATH environment variable, or the Interpreter.Options.GoPath needs to be set

`package patch

import ( "fmt" "reflect"

"github.com/golang/protobuf/proto"
"xxx.com/root/DawnCore/basic"
"xxx.com/root/DawnFriend/hotfix"
"xxx.com/root/DawnFriend/impl"
"xxx.com/root/DawnMicroHub"
"xxx.com/root/DawnMicroHub/econnector"
"xxx.com/root/DawnMicroHub/proto/csmsg"

)

func GetPatch() *hotfix.FuncPatch { fmt.Println("invoke GetPatch()")

fn := func(handler *impl.MSG_FRIEND_APPLY_AGREE_ALL_REQ, ec econnector.IExchangeConnector, req *DawnMicroHub.Packet) {
	msg := &csmsg.TMSG_FRIEND_APPLY_AGREE_ALL_REQ{}
	err := proto.Unmarshal(req.Data, msg)
	if err != nil {
		basic.Log.Error("roldId=", req.RoleID, ",err=", err.Error())
		return
	}
	basic.Log.Debug("new func(), roldId=", req.RoleID, ",HandleFriendApplyAgreeAllReq() req msg=", msg.String())

	return
}

return &hotfix.FuncPatch{
	StructType: reflect.TypeOf(&impl.TMSG_FRIEND_APPLY_AGREE_ALL_REQ{}),
	FuncName:   "HandleFriendApplyAgreeAllReq",
	FuncValue:  reflect.ValueOf(fn),
}

} ` 是我的写法不对吗?还是需要把整个xxx.com/root/DawnCore的源码都要放到patch文件夹?这样不太可取,有泄露源代码的风险

hhq163 avatar Nov 09 '23 04:11 hhq163

在symbols.go中设置各第三方依赖后,

//go:generate yaegi extract xxx.com/root/DawnCore/basic //go:generate yaegi extract xxx.com/root/DawnCore/chanx //go:generate yaegi extract xxx.com/root/DawnCore/util //go:generate yaegi extract xxx.com/root/DawnMicroHub //go:generate yaegi extract xxx.com/root/DawnMicroHub/econnector //go:generate yaegi extract xxx.com/root/DawnMicroHub/proto/csmsg

动态替换函数时,报错信息如下: hotfixdo.go:19 hotfix.ApplyFunc(),err=./_patch_files/patch/patch.go:32:31: undefined type

hhq163 avatar Nov 09 '23 07:11 hhq163