diff --git a/automation/server/internal/module/common/enum/rule/rule.go b/automation/server/internal/module/common/enum/rule/rule.go index 4199cd10ef4ae040951a9a3e858386ff17c9dd93..93a802bd1d000e6edb4c19d58f294068ddd90573 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 0a5b39d98b4fa286cc7e1bde005ab1c4f6d95079..17a640636020a42af0177110df31530a49d81a12 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 dc4126a191b194965ddde1ba7ac1dffbb4d47b0b..534fcc41401febbe4df93495da461550865d7213 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 bbfb3d0443112b5433233981fe52dffff58634ce..3b39c8460a913dc41f7d09033d9860aadea1f682 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 {