forked from null-luo/btrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_resolver.go
48 lines (39 loc) · 1.22 KB
/
method_resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"encoding/json"
"fmt"
_ "embed"
)
//go:embed methods.json
var methodsJSON []byte
type MethodMappings map[string]map[string]string
var interfaceMethods = make(map[string]map[int]string)
// LoadMethodMappings 从嵌入的 JSON 数据加载方法映射
func LoadMethodMappings(jsonData []byte) error {
var mappings MethodMappings
if err := json.Unmarshal(jsonData, &mappings); err != nil {
return fmt.Errorf("failed to unmarshal JSON: %w", err)
}
// 转换为 interfaceMethods 的格式
for interfaceName, methods := range mappings {
methodMap := make(map[int]string)
for code, methodName := range methods {
var codeInt int
if _, err := fmt.Sscan(code, &codeInt); err != nil {
return fmt.Errorf("invalid method code: %w", err)
}
methodMap[codeInt] = methodName
}
interfaceMethods[interfaceName] = methodMap
}
return nil
}
func GetMethodName(interfaceName string, code int) (string, error) {
if methods, ok := interfaceMethods[interfaceName]; ok {
if methodName, ok := methods[code]; ok {
return methodName, nil
}
return "", fmt.Errorf("method not found for code %d in interface %s", code, interfaceName)
}
return "", fmt.Errorf("interface %s not found", interfaceName)
}