Files
zerorigin-toolbox/cmd/settings/validate.go
T
zerorigin2019 0a1c1e82ea chore: 初始化仓库(零源工具箱 0.1.0 基线)
- Go 主进程 cmd/zerorigin(托盘常驻 + 触发器/规则/动作引擎)
- Wails v3 设置进程 cmd/settings(独立 module)
- 监督进程 cmd/watchdog(-supervise 异常重启)
- 构建脚本 build.bat / NSIS 安装包 deploy/、便携版 portable/
- 架构与 MVP 计划文档 docs/、阶段 0-4 验收记录 deploy/acceptance-0-4.md
2026-08-05 00:42:45 +08:00

106 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"sort"
"strings"
)
// 触发器与动作的类型清单(供前端下拉选择;校验时也使用)。
var triggerTypes = map[string]struct{}{
"mouse_corner": {},
"mouse_edge_wheel": {},
"hotkey": {},
}
var actionTypes = map[string]struct{}{
"switch_virtual_desktop": {},
"volume_up": {},
"volume_down": {},
"toggle_mute": {},
"show_notification": {},
"delay": {},
"launch_application": {},
"open_url": {},
}
// 触发器类型的中文展示名(前端下拉)。
var triggerLabels = map[string]string{
"mouse_corner": "鼠标进入屏幕角落",
"mouse_edge_wheel": "屏幕边缘滚轮",
"hotkey": "全局快捷键",
}
// 动作类型的中文展示名。
var actionLabels = map[string]string{
"switch_virtual_desktop": "切换虚拟桌面",
"volume_up": "音量增大",
"volume_down": "音量减小",
"toggle_mute": "静音切换",
"show_notification": "显示通知",
"delay": "延迟",
"launch_application": "启动程序",
"open_url": "打开链接",
}
// ValidateConfig 校验配置:规则 ID 唯一、trigger/action 类型已知、
// 动作参数结构合法。返回每条规则的错误描述(不阻止保存,由前端提示)。
func ValidateConfig(cfg *Config) []string {
var errs []string
seen := map[string]bool{}
for i := range cfg.Rules {
r := &cfg.Rules[i]
prefix := fmt.Sprintf("规则 %q", r.Name)
if r.ID == "" {
errs = append(errs, prefix+": 缺少 id")
} else if seen[r.ID] {
errs = append(errs, prefix+": id 重复")
}
seen[r.ID] = true
if _, ok := triggerTypes[r.Trigger.Type]; !ok {
errs = append(errs, prefix+": 未知触发器类型 "+r.Trigger.Type)
}
if len(r.Actions) == 0 {
errs = append(errs, prefix+": 至少需要一个动作")
}
for j := range r.Actions {
if _, ok := actionTypes[r.Actions[j].Type]; !ok {
errs = append(errs, fmt.Sprintf("%s: 未知动作类型 %q", prefix, r.Actions[j].Type))
}
}
if len(r.Conditions) > 0 {
errs = append(errs, prefix+": 条件系统暂不支持")
}
}
sort.Strings(errs)
return errs
}
// normalizeParams 修正前端传来的参数类型(JSON 数字为 float64,
// 但 trigger 阈值等需要整数形式,保持原样由主进程容错)。
func normalizeParams(p map[string]any) map[string]any {
out := make(map[string]any, len(p))
for k, v := range p {
out[k] = v
}
return out
}
// sanitizeRule 规范化前端编辑后的规则:补齐 params 为 map、去空白。
func sanitizeRule(r *Rule) {
if r.Trigger.Params == nil {
r.Trigger.Params = map[string]any{}
}
for k, v := range r.Trigger.Params {
if s, ok := v.(string); ok {
r.Trigger.Params[k] = strings.TrimSpace(s)
}
}
for i := range r.Actions {
if r.Actions[i].Params == nil {
r.Actions[i].Params = map[string]any{}
}
}
}