1 Star 0 Fork 0

kade / mcube

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
map.go 10.45 KB
一键复制 编辑 原始数据 按行查看 历史
kadegolang 提交于 2023-12-13 17:31 . copy
package smap
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
)
// Event metadata constants. These keys are used within libbeat to identify
// metadata stored in an event.
const (
FieldsKey = "fields"
TagsKey = "tags"
)
var (
// ErrKeyNotFound indicates that the specified key was not found.
ErrKeyNotFound = errors.New("key not found")
)
// StringMap map参数
type StringMap map[string]interface{}
// Scan todo
func (m *StringMap) Scan(value interface{}) error {
switch v := value.(type) {
case []uint8:
return json.Unmarshal(v, m)
case string:
return json.Unmarshal([]byte(v), m)
}
return nil
}
// Value todo
func (m StringMap) Value() (driver.Value, error) {
return json.Marshal(m)
}
// Update copies all the key-value pairs from d to this map. If the key
// already exists then it is overwritten. This method does not merge nested
// maps.
func (m StringMap) Update(d StringMap) {
for k, v := range d {
m[k] = v
}
}
// DeepUpdate recursively copies the key-value pairs from d to this map.
// If the key is present and a map as well, the sub-map will be updated recursively
// via DeepUpdate.
// DeepUpdateNoOverwrite is a version of this function that does not
// overwrite existing values.
func (m StringMap) DeepUpdate(d StringMap) {
m.deepUpdateMap(d, true)
}
// DeepUpdateNoOverwrite recursively copies the key-value pairs from d to this map.
// If a key is already present it will not be overwritten.
// DeepUpdate is a version of this function that overwrites existing values.
func (m StringMap) DeepUpdateNoOverwrite(d StringMap) {
m.deepUpdateMap(d, false)
}
func (m StringMap) deepUpdateMap(d StringMap, overwrite bool) {
for k, v := range d {
switch val := v.(type) {
case map[string]interface{}:
m[k] = deepUpdateValue(m[k], StringMap(val), overwrite)
case StringMap:
m[k] = deepUpdateValue(m[k], val, overwrite)
default:
if overwrite {
m[k] = v
} else if _, exists := m[k]; !exists {
m[k] = v
}
}
}
}
func deepUpdateValue(old interface{}, val StringMap, overwrite bool) interface{} {
switch sub := old.(type) {
case StringMap:
if sub == nil {
return val
}
sub.deepUpdateMap(val, overwrite)
return sub
case map[string]interface{}:
if sub == nil {
return val
}
tmp := StringMap(sub)
tmp.deepUpdateMap(val, overwrite)
return tmp
default:
// We reach the default branch if old is no map or if old == nil.
// In either case we return `val`, such that the old value is completely
// replaced when merging.
return val
}
}
// Delete deletes the given key from the map.
func (m StringMap) Delete(key string) error {
k, d, _, found, err := mapFind(key, m, false)
if err != nil {
return err
}
if !found {
return ErrKeyNotFound
}
delete(d, k)
return nil
}
// CopyFieldsTo copies the field specified by key to the given map. It will
// overwrite the key if it exists. An error is returned if the key does not
// exist in the source map.
func (m StringMap) CopyFieldsTo(to StringMap, key string) error {
v, err := m.Get(key)
if err != nil {
return err
}
_, err = to.Put(key, v)
return err
}
// Clone returns a copy of the MapStr. It recursively makes copies of inner
// maps.
func (m StringMap) Clone() StringMap {
result := StringMap{}
for k, v := range m {
if innerMap, ok := tryToMapStr(v); ok {
v = innerMap.Clone()
}
result[k] = v
}
return result
}
// Put associates the specified value with the specified key. If the map
// previously contained a mapping for the key, the old value is replaced and
// returned. The key can be expressed in dot-notation (e.g. x.y) to put a value
// into a nested map.
//
// If you need insert keys containing dots then you must use bracket notation
// to insert values (e.g. m[key] = value).
func (m StringMap) Put(key string, value interface{}) (interface{}, error) {
// XXX `safemapstr.Put` mimics this implementation, both should be updated to have similar behavior
k, d, old, _, err := mapFind(key, m, true)
if err != nil {
return nil, err
}
d[k] = value
return old, nil
}
// StringToPrint returns the MapStr as pretty JSON.
func (m StringMap) StringToPrint() string {
json, err := json.MarshalIndent(m, "", " ")
if err != nil {
return fmt.Sprintf("Not valid json: %v", err)
}
return string(json)
}
// String returns the MapStr as JSON.
func (m StringMap) String() string {
bytes, err := json.Marshal(m)
if err != nil {
return fmt.Sprintf("Not valid json: %v", err)
}
return string(bytes)
}
// Flatten flattens the given MapStr and returns a flat MapStr.
//
// Example:
// "hello": MapStr{"world": "test" }
//
// This is converted to:
// "hello.world": "test"
//
// This can be useful for testing or logging.
func (m StringMap) Flatten() StringMap {
return flatten("", m, StringMap{})
}
// flatten is a helper for Flatten. See docs for Flatten. For convenience the
// out parameter is returned.
func flatten(prefix string, in, out StringMap) StringMap {
for k, v := range in {
var fullKey string
if prefix == "" {
fullKey = k
} else {
fullKey = prefix + "." + k
}
if m, ok := tryToMapStr(v); ok {
flatten(fullKey, m, out)
} else {
out[fullKey] = v
}
}
return out
}
// MapStrUnion creates a new MapStr containing the union of the
// key-value pairs of the two maps. If the same key is present in
// both, the key-value pairs from dict2 overwrite the ones from dict1.
func MapStrUnion(dict1 StringMap, dict2 StringMap) StringMap {
dict := StringMap{}
for k, v := range dict1 {
dict[k] = v
}
for k, v := range dict2 {
dict[k] = v
}
return dict
}
// MergeFields merges the top-level keys and values in each source map (it does
// not perform a deep merge). If the same key exists in both, the value in
// fields takes precedence. If underRoot is true then the contents of the fields
// MapStr is merged with the value of the 'fields' key in target.
//
// An error is returned if underRoot is true and the value of ms.fields is not a
// MapStr.
func MergeFields(target, from StringMap, underRoot bool) error {
if target == nil || len(from) == 0 {
return nil
}
destMap, err := mergeFieldsGetDestMap(target, from, underRoot)
if err != nil {
return err
}
// Add fields and override.
for k, v := range from {
destMap[k] = v
}
return nil
}
// MergeFieldsDeep recursively merges the keys and values from `from` into `target`, either
// into ms itself (if underRoot == true) or into ms["fields"] (if underRoot == false). If
// the same key exists in `from` and the destination map, the value in fields takes precedence.
//
// An error is returned if underRoot is true and the value of ms["fields"] is not a
// MapStr.
func MergeFieldsDeep(target, from StringMap, underRoot bool) error {
if target == nil || len(from) == 0 {
return nil
}
destMap, err := mergeFieldsGetDestMap(target, from, underRoot)
if err != nil {
return err
}
destMap.DeepUpdate(from)
return nil
}
func mergeFieldsGetDestMap(target, from StringMap, underRoot bool) (StringMap, error) {
destMap := target
if !underRoot {
f, ok := target[FieldsKey]
if !ok {
destMap = make(StringMap, len(from))
target[FieldsKey] = destMap
} else {
// Use existing 'fields' value.
var err error
destMap, err = toMapStr(f)
if err != nil {
return nil, err
}
}
}
return destMap, nil
}
// AddTags appends a tag to the tags field of ms. If the tags field does not
// exist then it will be created. If the tags field exists and is not a []string
// then an error will be returned. It does not deduplicate the list of tags.
func AddTags(ms StringMap, tags []string) error {
return AddTagsWithKey(ms, TagsKey, tags)
}
// AddTagsWithKey appends a tag to the key field of ms. If the field does not
// exist then it will be created. If the field exists and is not a []string
// then an error will be returned. It does not deduplicate the list.
func AddTagsWithKey(ms StringMap, key string, tags []string) error {
if ms == nil || len(tags) == 0 {
return nil
}
k, subMap, oldTags, present, err := mapFind(key, ms, true)
if err != nil {
return err
}
if !present {
subMap[k] = tags
return nil
}
switch arr := oldTags.(type) {
case []string:
subMap[k] = append(arr, tags...)
case []interface{}:
for _, tag := range tags {
arr = append(arr, tag)
}
subMap[k] = arr
default:
return errors.Errorf("expected string array by type is %T", oldTags)
}
return nil
}
// HasKey 获取key
func (m StringMap) HasKey(key string) (bool, error) {
_, _, _, hasKey, err := mapFind(key, m, false)
return hasKey, err
}
// Get 获取值
func (m StringMap) Get(key string) (interface{}, error) {
_, _, v, found, err := mapFind(key, m, false)
if err != nil {
return nil, err
}
if !found {
return nil, ErrKeyNotFound
}
return v, nil
}
// toMapStr performs a type assertion on v and returns a MapStr. v can be either
// a MapStr or a map[string]interface{}. If it's any other type or nil then
// an error is returned.
func toMapStr(v interface{}) (StringMap, error) {
m, ok := tryToMapStr(v)
if !ok {
return nil, errors.Errorf("expected map but type is %T", v)
}
return m, nil
}
func tryToMapStr(v interface{}) (StringMap, bool) {
switch m := v.(type) {
case StringMap:
return m, true
case map[string]interface{}:
return StringMap(m), true
default:
return nil, false
}
}
// mapFind iterates a MapStr based on a the given dotted key, finding the final
// subMap and subKey to operate on.
// An error is returned if some intermediate is no map or the key doesn't exist.
// If createMissing is set to true, intermediate maps are created.
// The final map and un-dotted key to run further operations on are returned in
// subKey and subMap. The subMap already contains a value for subKey, the
// present flag is set to true and the oldValue return will hold
// the original value.
func mapFind(
key string,
data StringMap,
createMissing bool,
) (subKey string, subMap StringMap, oldValue interface{}, present bool, err error) {
// XXX `safemapstr.mapFind` mimics this implementation, both should be updated to have similar behavior
for {
// Fast path, key is present as is.
if v, exists := data[key]; exists {
return key, data, v, true, nil
}
idx := strings.IndexRune(key, '.')
if idx < 0 {
return key, data, nil, false, nil
}
k := key[:idx]
d, exists := data[k]
if !exists {
if createMissing {
d = StringMap{}
data[k] = d
} else {
return "", nil, nil, false, ErrKeyNotFound
}
}
v, err := toMapStr(d)
if err != nil {
return "", nil, nil, false, err
}
// advance to sub-map
key = key[idx+1:]
data = v
}
}
1
https://gitee.com/go-kade/mcube.git
git@gitee.com:go-kade/mcube.git
go-kade
mcube
mcube
1225d9a674f1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891