GetFunctionName reads the handle to find the underlying real function name. The function can be an actual function or a struct which implements one.
(fn interface{})
| 243 | // GetFunctionName reads the handle to find the underlying real function name. |
| 244 | // The function can be an actual function or a struct which implements one. |
| 245 | func GetFunctionName(fn interface{}) string { |
| 246 | pc := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()) |
| 247 | if pc == nil { |
| 248 | // This part works for structs, the other parts work for funcs. |
| 249 | t := reflect.TypeOf(fn) |
| 250 | if t.Kind() == reflect.Pointer { |
| 251 | t = t.Elem() |
| 252 | } |
| 253 | |
| 254 | return t.Name() |
| 255 | } |
| 256 | |
| 257 | // if pc.Name() is: github.com/purpleidea/mgmt/lang/core/math.Pow |
| 258 | sp := strings.Split(pc.Name(), "/") |
| 259 | |
| 260 | // ...this will be: math.Pow |
| 261 | s := sp[len(sp)-1] |
| 262 | |
| 263 | ix := strings.LastIndex(s, ".") |
| 264 | if ix == -1 { // standalone |
| 265 | return s |
| 266 | } |
| 267 | |
| 268 | // ... this will be: Pow |
| 269 | return s[ix+1:] |
| 270 | } |
| 271 | |
| 272 | // GetFunctionMetadata builds a metadata struct with everything about this func. |
| 273 | func GetFunctionMetadata(fn interface{}) (*docsUtil.Metadata, error) { |
no test coverage detected