Quellcode durchsuchen

调用mcp-测试通过-搜索店铺档案表

qdy vor 1 Monat
Ursprung
Commit
e198533331
5 geänderte Dateien mit 121 neuen und 16 gelöschten Zeilen
  1. 16
    0
      config/config.go
  2. 5
    0
      config/subconfigs/dbs_config.go
  3. 1
    0
      go.mod
  4. 2
    0
      go.sum
  5. 97
    16
      test/test.go

+ 16
- 0
config/config.go Datei anzeigen

@@ -23,6 +23,7 @@ type IConfig interface {
23 23
 	// 多数据库支持
24 24
 	GetDatabaseConfig() *subconfigs.DatabaseConfig               // 获取默认数据库(向后兼容)
25 25
 	GetDbsConfig(name string) (*subconfigs.DatabaseConfig, bool) // 获取指定数据库连接
26
+	GetDbsConfigs() *subconfigs.DbsConfig                        //获取所有配置
26 27
 
27 28
 	//服务支持
28 29
 	GetServiceConfig() *subconfigs.ServiceConfig       //获取默认微服务配置
@@ -70,6 +71,13 @@ func (c *Config) GetDatabaseConfig() *subconfigs.DatabaseConfig {
70 71
 	return nil
71 72
 }
72 73
 
74
+func (c *Config) GetDbsConfigs() *subconfigs.DbsConfig {
75
+	if config := core.GetRegisteredConfig("dbs"); config != nil {
76
+		return config.(*subconfigs.DbsConfig)
77
+	}
78
+	return nil
79
+}
80
+
73 81
 // GetDbsConfig 按名称获取数据库配置
74 82
 func (c *Config) GetDbsConfig(name string) (*subconfigs.DatabaseConfig, bool) {
75 83
 	// 获取 dbs 配置
@@ -239,6 +247,14 @@ func GetDbsConfig(name string) (*subconfigs.DatabaseConfig, bool) {
239 247
 	return cfgInstance.GetDbsConfig(name)
240 248
 }
241 249
 
250
+// GetDbsConfigs 包级便捷函数 - 按名称获取数据库配置
251
+func GetDbsConfigs() *subconfigs.DbsConfig {
252
+	if cfgInstance == nil {
253
+		return nil
254
+	}
255
+	return cfgInstance.GetDbsConfigs()
256
+}
257
+
242 258
 // GetRedis 包级便捷函数 - 获取Redis配置
243 259
 func GetRedisConfig() *subconfigs.RedisConfig {
244 260
 	if cfgInstance == nil {

+ 5
- 0
config/subconfigs/dbs_config.go Datei anzeigen

@@ -70,6 +70,11 @@ func (c *DbsConfig) Default() *DbConfig {
70 70
 	return nil
71 71
 }
72 72
 
73
+func (c *DbsConfig) GetDbsConfig() map[string]*DbConfig {
74
+
75
+	return c.Databases
76
+}
77
+
73 78
 func init() {
74 79
 	core.Register("dbs", &DbsConfig{})
75 80
 }

+ 1
- 0
go.mod Datei anzeigen

@@ -34,6 +34,7 @@ require (
34 34
 	github.com/mitchellh/mapstructure v1.3.3 // indirect
35 35
 	github.com/rogpeppe/go-internal v1.14.1 // indirect
36 36
 	github.com/stretchr/testify v1.11.1 // indirect
37
+	github.com/traefik/yaegi v0.16.1 // indirect
37 38
 	go.opentelemetry.io/otel v1.28.0 // indirect
38 39
 	go.opentelemetry.io/otel/metric v1.28.0 // indirect
39 40
 	go.opentelemetry.io/otel/trace v1.28.0 // indirect

+ 2
- 0
go.sum Datei anzeigen

@@ -134,6 +134,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
134 134
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
135 135
 github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
136 136
 github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
137
+github.com/traefik/yaegi v0.16.1 h1:f1De3DVJqIDKmnasUF6MwmWv1dSEEat0wcpXhD2On3E=
138
+github.com/traefik/yaegi v0.16.1/go.mod h1:4eVhbPb3LnD2VigQjhYbEJ69vDRFdT2HQNrXx8eEwUY=
137 139
 go.mongodb.org/mongo-driver v1.17.6 h1:87JUG1wZfWsr6rIz3ZmpH90rL5tea7O3IHuSwHUpsss=
138 140
 go.mongodb.org/mongo-driver v1.17.6/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
139 141
 go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=

+ 97
- 16
test/test.go Datei anzeigen

@@ -2,27 +2,108 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"strings"
5 6
 
6
-	"git.x2erp.com/qdy/go-base/config"
7
+	"github.com/traefik/yaegi/interp"
8
+	"github.com/traefik/yaegi/stdlib"
7 9
 )
8 10
 
9 11
 func main() {
10
-	// 获取配置实例
11
-	cfg := config.GetConfig()
12
-
13
-	// 使用配置
14
-	if cfg.IsDatabaseConfigured() {
15
-		dbConfig := cfg.GetDatabaseConfig()
16
-		fmt.Printf("Host: %s\n", dbConfig.Host)
17
-		fmt.Printf("Port: %d\n", dbConfig.Port)
18
-		fmt.Printf("Database: %s\n", dbConfig.Database)
12
+
13
+	calculateSimple("(2+3)*5")
14
+	calculateSimple("15/5")
15
+	// 测试多行代码(使用专门的多行函数)
16
+	code := `x := 10
17
+if x > 5 {
18
+    return "大于5"
19
+} else {
20
+    return "小于等于5"
21
+}`
22
+
23
+	calculateMultiLine(code)
24
+	// // 获取配置实例
25
+	// cfg := config.GetConfig()
26
+
27
+	// // 使用配置
28
+	// if cfg.IsDatabaseConfigured() {
29
+	// 	dbConfig := cfg.GetDatabaseConfig()
30
+	// 	fmt.Printf("Host: %s\n", dbConfig.Host)
31
+	// 	fmt.Printf("Port: %d\n", dbConfig.Port)
32
+	// 	fmt.Printf("Database: %s\n", dbConfig.Database)
33
+	// }
34
+
35
+	// //authConfig := cfg.GetAuth()
36
+	// //fmt.Printf("token: %s\n", authConfig.Token)
37
+
38
+	// serviceConfig := cfg.GetServiceConfig()
39
+	// fmt.Printf("ReadTimeout: %d秒\n", serviceConfig.ReadTimeout)
40
+	// fmt.Printf("WriteTimeout: %d秒\n", serviceConfig.WriteTimeout)
41
+	// fmt.Printf("IdleTimeout: %d秒\n", serviceConfig.IdleTimeout)
42
+
43
+}
44
+
45
+func calculateSimple(expression string) (string, error) {
46
+	i := interp.New(interp.Options{})
47
+	i.Use(stdlib.Symbols)
48
+
49
+	// 包装成匿名函数立即调用,既简洁又安全
50
+	code := fmt.Sprintf("(func() interface{} { return %s })()", expression)
51
+
52
+	v, err := i.Eval(code)
53
+	if err != nil {
54
+		fmt.Printf("calculateSimple: err v:%v \n", err)
55
+		return "", err
19 56
 	}
20 57
 
21
-	//authConfig := cfg.GetAuth()
22
-	//fmt.Printf("token: %s\n", authConfig.Token)
58
+	fmt.Printf("calculateSimple: %s v:%v \n", expression, v)
59
+	return fmt.Sprintf("%v", v), nil
60
+}
61
+
62
+// 专门处理多行代码(if/for/变量声明等)
63
+func calculateMultiLine(code string) (string, error) {
64
+	i := interp.New(interp.Options{})
65
+	i.Use(stdlib.Symbols)
23 66
 
24
-	serviceConfig := cfg.GetServiceConfig()
25
-	fmt.Printf("ReadTimeout: %d秒\n", serviceConfig.ReadTimeout)
26
-	fmt.Printf("WriteTimeout: %d秒\n", serviceConfig.WriteTimeout)
27
-	fmt.Printf("IdleTimeout: %d秒\n", serviceConfig.IdleTimeout)
67
+	// 关键:整个代码块作为函数体,最后一行作为返回值
68
+	wrappedCode := fmt.Sprintf(`
69
+(func() interface{} {
70
+	%s
71
+})()`, code)
72
+
73
+	v, err := i.Eval(wrappedCode)
74
+	if err != nil {
75
+		fmt.Printf("calculateMultiLine 错误: %v\n代码: %s\n", err, code)
76
+		return "", err
77
+	}
78
+
79
+	fmt.Printf("calculateMultiLine 成功: %v\n", v)
80
+	return fmt.Sprintf("%v", v), nil
81
+}
82
+
83
+// 更智能的版本:自动判断使用哪种处理方式
84
+func calculateAuto(expression string) (string, error) {
85
+	// 如果是纯数学表达式(只有数字和运算符)
86
+	if isPureMath(expression) {
87
+		return calculateSimple(expression)
88
+	}
89
+
90
+	// 否则当做多行代码处理
91
+	return calculateMultiLine(expression)
92
+}
93
+
94
+func isPureMath(expr string) bool {
95
+	// 移除非空格的空白字符
96
+	cleaned := strings.ReplaceAll(expr, "\n", "")
97
+	cleaned = strings.ReplaceAll(cleaned, "\t", "")
98
+	cleaned = strings.ReplaceAll(cleaned, " ", "")
99
+
100
+	// 检查是否只包含数字、运算符、括号和小数点
101
+	for _, ch := range cleaned {
102
+		if !((ch >= '0' && ch <= '9') ||
103
+			ch == '+' || ch == '-' || ch == '*' || ch == '/' ||
104
+			ch == '(' || ch == ')' || ch == '.') {
105
+			return false
106
+		}
107
+	}
108
+	return len(cleaned) > 0
28 109
 }

Laden…
Abbrechen
Speichern