github.com/webx-top/com@v1.2.12/caller.go (about)

     1  /*
     2  
     3     Copyright 2016 Wenhui Shen <www.webx.top>
     4  
     5     Licensed under the Apache License, Version 2.0 (the "License");
     6     you may not use this file except in compliance with the License.
     7     You may obtain a copy of the License at
     8  
     9         http://www.apache.org/licenses/LICENSE-2.0
    10  
    11     Unless required by applicable law or agreed to in writing, software
    12     distributed under the License is distributed on an "AS IS" BASIS,
    13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14     See the License for the specific language governing permissions and
    15     limitations under the License.
    16  
    17  */
    18  
    19  package com
    20  
    21  import (
    22  	"errors"
    23  	"fmt"
    24  	"path"
    25  	"reflect"
    26  	"runtime"
    27  	"strings"
    28  )
    29  
    30  // FuncName 函数名
    31  func FuncName(i interface{}) string {
    32  	return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
    33  }
    34  
    35  // FuncPath 返回包名、实例名和函数名
    36  func FuncPath(i interface{}) (pkgName string, objName string, funcName string) {
    37  	s := FuncName(i)
    38  	_, file := path.Split(s)
    39  	return ParseFuncName(file)
    40  }
    41  
    42  // FuncFullPath 返回完整路径包名、实例名和函数名
    43  func FuncFullPath(i interface{}) (pkgName string, objName string, funcName string) {
    44  	return ParseFuncName(FuncName(i))
    45  }
    46  
    47  // ParseFuncName 解析函数名
    48  func ParseFuncName(funcString string) (pkgName string, objName string, funcName string) {
    49  	if strings.HasSuffix(funcString, `-fm`) {
    50  		funcString = strings.TrimSuffix(funcString, `-fm`)
    51  		part := strings.Split(funcString, `.`)
    52  		switch len(part) {
    53  		case 3:
    54  			funcName = part[2]
    55  			fallthrough
    56  		case 2:
    57  			objName = part[1]
    58  			if objName[0] == '(' {
    59  				objName = objName[1 : len(objName)-1]
    60  				objName = strings.TrimPrefix(objName, `*`)
    61  			}
    62  			fallthrough
    63  		case 1:
    64  			pkgName = part[0]
    65  		}
    66  		return
    67  	}
    68  	part := strings.Split(funcString, `.`)
    69  	switch len(part) {
    70  	case 2:
    71  		funcName = part[1]
    72  		fallthrough
    73  	case 1:
    74  		pkgName = part[0]
    75  	}
    76  	return
    77  }
    78  
    79  //CallFunc 根据名称调用对应函数
    80  func CallFunc(getFuncByName func(string) (interface{}, bool), funcName string, params ...interface{}) ([]reflect.Value, error) {
    81  	fn, ok := getFuncByName(funcName)
    82  	if !ok {
    83  		return nil, errors.New("no func found: " + funcName)
    84  	}
    85  	f := reflect.ValueOf(fn)
    86  	if len(params) != f.Type().NumIn() {
    87  		return nil, errors.New("parameter mismatched")
    88  	}
    89  	in := make([]reflect.Value, len(params))
    90  	for k, param := range params {
    91  		in[k] = reflect.ValueOf(param)
    92  	}
    93  	return f.Call(in), nil
    94  }
    95  
    96  func CalledAtFileLine(skip int) string {
    97  	pc, file, line, ok := runtime.Caller(skip)
    98  	if !ok {
    99  		return ``
   100  	}
   101  
   102  	f := runtime.FuncForPC(pc)
   103  	return file + `:` + fmt.Sprint(line) + ` (` + f.Name() + `)`
   104  }