From 54d5c3d95aa997eb658376ef57a1f8862e45762d Mon Sep 17 00:00:00 2001 From: zhanghan Date: Wed, 10 Sep 2025 15:27:04 +0800 Subject: [PATCH] Encapsulate the set method of the Redis client --- .../internal/module/common/enum/rule/rule.go | 9 +++++++++ .../module/common/enum/script/script.go | 17 +++++++++++++++++ .../dangerous_rule/model/dangerous_rule.go | 19 +++++++++++++++++++ automation/server/internal/service/redis.go | 9 +++++++++ 4 files changed, 54 insertions(+) diff --git a/automation/server/internal/module/common/enum/rule/rule.go b/automation/server/internal/module/common/enum/rule/rule.go index 4199cd10..93a802bd 100644 --- a/automation/server/internal/module/common/enum/rule/rule.go +++ b/automation/server/internal/module/common/enum/rule/rule.go @@ -18,6 +18,15 @@ func (s ActionType) String() string { return ActionMap.String(int(s)) } +func ParseActionType(s string) ActionType { + for k, v := range ActionMap { + if v == s { + return ActionType(k) + } + } + return 0 +} + func GetActions() []enum.Item { return ActionMap.ToItems() } diff --git a/automation/server/internal/module/common/enum/script/script.go b/automation/server/internal/module/common/enum/script/script.go index 0a5b39d9..17a64063 100644 --- a/automation/server/internal/module/common/enum/script/script.go +++ b/automation/server/internal/module/common/enum/script/script.go @@ -22,6 +22,15 @@ var ScriptTypeMap = enum.EnumMap{ int(SQL): "SQL", } +func ParseScriptType(s string) ScriptType { + for k, v := range ScriptTypeMap { + if v == s { + return ScriptType(k) + } + } + return 0 +} + func GetScriptType() []enum.Item { return ScriptTypeMap.ToItems() } @@ -55,3 +64,11 @@ func (a *ScriptTypeArr) Scan(value interface{}) error { } return nil } + +func NewScriptTypeArr(strs []string) ScriptTypeArr { + res := make(ScriptTypeArr, 0, len(strs)) + for _, s := range strs { + res = append(res, ParseScriptType(s)) + } + return res +} diff --git a/automation/server/internal/module/dangerous_rule/model/dangerous_rule.go b/automation/server/internal/module/dangerous_rule/model/dangerous_rule.go index dc4126a1..534fcc41 100644 --- a/automation/server/internal/module/dangerous_rule/model/dangerous_rule.go +++ b/automation/server/internal/module/dangerous_rule/model/dangerous_rule.go @@ -31,3 +31,22 @@ func (r DangerousRule) MarshalJSON() ([]byte, error) { Alias: (Alias)(r), }) } + +func (r *DangerousRule) UnmarshalJSON(data []byte) error { + type Alias DangerousRule + aux := &struct { + Action string `json:"action"` + ScriptTypes []string `json:"script_types"` + *Alias + }{ + Alias: (*Alias)(r), + } + + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + + r.Action = rule.ParseActionType(aux.Action) + r.ScriptTypes = script.NewScriptTypeArr(aux.ScriptTypes) + return nil +} diff --git a/automation/server/internal/service/redis.go b/automation/server/internal/service/redis.go index bbfb3d04..3b39c846 100644 --- a/automation/server/internal/service/redis.go +++ b/automation/server/internal/service/redis.go @@ -101,6 +101,7 @@ func (r *RedisService) Close() error { // ===============================Redis API============================================= type Redis interface { + Set(key string, value interface{}, expiration time.Duration) error SetNX(key string, value interface{}, expiration time.Duration) error Get(key string, out interface{}) error Delete(keys ...string) error @@ -114,6 +115,14 @@ type Redis interface { } // =============================Redis manager============================================ +func (r *RedisService) Set(key string, value interface{}, expiration time.Duration) error { + data, err := json.Marshal(value) + if err != nil { + return err + } + return r.client.Set(r.ctx, key, data, expiration).Err() +} + func (r *RedisService) SetNX(key string, value interface{}, expiration time.Duration) error { data, err := json.Marshal(value) if err != nil { -- Gitee