Files
ai-code-review/utils/utils.go

106 lines
2.2 KiB
Go

package utils
import (
"code-review/services/types"
"fmt"
"strings"
)
// GetString 从 map 中安全获取字符串值
func GetString(m map[string]interface{}, key string) string {
if v, ok := m[key].(string); ok {
return v
}
return ""
}
// GetFloat64 从 map 中安全获取 float64 值
func GetFloat64(m map[string]interface{}, key string) float64 {
if v, ok := m[key].(float64); ok {
return v
}
return 0
}
// GetBool 从 map 中安全获取布尔值
func GetBool(m map[string]interface{}, key string) bool {
if v, ok := m[key].(bool); ok {
return v
}
return false
}
// GetInt 从 map 中安全获取整数值
func GetInt(m map[string]interface{}, key string) int {
if v, ok := m[key].(float64); ok {
return int(v)
}
return 0
}
// GetIntPtr 从 map 中安全获取整数指针值
func GetIntPtr(m map[string]interface{}, key string) *int {
if v, ok := m[key].(float64); ok {
intVal := int(v)
return &intVal
}
return nil
}
// GetIntOk 从 map 中获取 int 值,并返回是否存在
func GetIntOk(m map[string]interface{}, key string) (int, bool) {
if val, ok := m[key]; ok {
switch v := val.(type) {
case float64:
return int(v), true
case int:
return v, true
case int64:
return int(v), true
}
}
return 0, false
}
// FormatReviewResult 将审查结果格式化为 Markdown 格式
func FormatReviewResult(result *types.ReviewResult) string {
var body strings.Builder
// 添加摘要
if result.Summary != "" {
body.WriteString("## 审查摘要\n\n")
body.WriteString(result.Summary)
body.WriteString("\n\n")
}
// 添加详细评论
if len(result.Comments) > 0 {
body.WriteString("## 详细评论\n\n")
for _, comment := range result.Comments {
if comment.Path != "全局" {
body.WriteString(fmt.Sprintf("### %s\n\n", comment.Path))
}
body.WriteString(comment.Content)
body.WriteString("\n\n")
}
}
return body.String()
}
// ParseFileType 将文件变更类型字符串转换为 ChangeType
func ParseFileType(t string) types.ChangeType {
switch t {
case "add", "added":
return types.Added
case "modify", "modified":
return types.Modified
case "delete", "deleted":
return types.Deleted
case "renamed":
return types.Renamed
default:
return types.Modified
}
}