in tools/go-agent/instrument/logger/instrument.go [299:342]
func (i *Instrument) addAutomaticFuncDelegators(f *dst.File, importDecl *dst.GenDecl) {
packageName := filepath.Base(i.compileOpts.Package)
for _, fun := range i.automaticFunc {
fun.ImportAnalyzer.AppendUsedImports(importDecl)
delegatorFunc := &dst.FuncDecl{
Name: dst.NewIdent(fun.FuncName),
Type: &dst.FuncType{
Params: &dst.FieldList{},
},
}
for i, recv := range tools.EnhanceParameterNamesWithPackagePrefix(packageName, fun.Func.Recv, tools.FieldListTypeRecv) {
delegatorFunc.Type.Params.List = append(delegatorFunc.Type.Params.List, &dst.Field{
Names: []*dst.Ident{dst.NewIdent(fmt.Sprintf("recv_%d", i))},
Type: &dst.StarExpr{X: recv.PackagedType()},
})
}
for i, parameter := range tools.EnhanceParameterNamesWithPackagePrefix(packageName, fun.Func.Type.Params, tools.FieldListTypeParam) {
packagedType := parameter.PackagedType()
// if the parameter is dynamic list, then change it to the array type
if el, ok := packagedType.(*dst.Ellipsis); ok {
packagedType = &dst.ArrayType{Elt: el.Elt}
}
delegatorFunc.Type.Params.List = append(delegatorFunc.Type.Params.List, &dst.Field{
Names: []*dst.Ident{dst.NewIdent(fmt.Sprintf("param_%d", i))},
Type: &dst.StarExpr{X: packagedType},
})
}
for i, result := range tools.EnhanceParameterNamesWithPackagePrefix(packageName, fun.Func.Type.Results, tools.FieldListTypeResult) {
delegatorFunc.Type.Params.List = append(delegatorFunc.Type.Params.List, &dst.Field{
Names: []*dst.Ident{dst.NewIdent(fmt.Sprintf("ret_%d", i))},
Type: &dst.StarExpr{X: result.PackagedType()},
})
}
delegatorFunc.Body = &dst.BlockStmt{
List: tools.GoStringToStats(fun.DelegateBody),
}
f.Decls = append(f.Decls, delegatorFunc)
}
}