mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-07 00:43:35 +08:00
feat: use "GetTags" and "SetTags"
This commit is contained in:
45
utils/json.go
Normal file
45
utils/json.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/oarkflow/json"
|
||||
"github.com/oarkflow/json/jsonparser"
|
||||
)
|
||||
|
||||
func RemoveFromJSONBye(jsonStr json.RawMessage, key ...string) json.RawMessage {
|
||||
return jsonparser.Delete(jsonStr, key...)
|
||||
}
|
||||
|
||||
func RemoveRecursiveFromJSON(jsonStr json.RawMessage, key ...string) json.RawMessage {
|
||||
var data any
|
||||
if err := json.Unmarshal(jsonStr, &data); err != nil {
|
||||
return jsonStr
|
||||
}
|
||||
|
||||
for _, k := range key {
|
||||
data = removeKeyRecursive(data, k)
|
||||
}
|
||||
|
||||
result, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return jsonStr
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func removeKeyRecursive(data any, key string) any {
|
||||
switch v := data.(type) {
|
||||
case map[string]any:
|
||||
delete(v, key)
|
||||
for k, val := range v {
|
||||
v[k] = removeKeyRecursive(val, key)
|
||||
}
|
||||
return v
|
||||
case []any:
|
||||
for i, item := range v {
|
||||
v[i] = removeKeyRecursive(item, key)
|
||||
}
|
||||
return v
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user