github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_attribute_map.go (about)

     1  // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gmetric
     8  
     9  // AttributeMap contains the attribute key and value as map for easy filtering.
    10  type AttributeMap map[string]any
    11  
    12  // Sets adds given attribute map to current map.
    13  func (m AttributeMap) Sets(attrMap map[string]any) {
    14  	for k, v := range attrMap {
    15  		m[k] = v
    16  	}
    17  }
    18  
    19  // Pick picks and returns attributes by given attribute keys.
    20  func (m AttributeMap) Pick(keys ...string) Attributes {
    21  	var attrs = make(Attributes, 0)
    22  	for _, key := range keys {
    23  		value, ok := m[key]
    24  		if !ok {
    25  			continue
    26  		}
    27  		attrs = append(attrs, NewAttribute(key, value))
    28  	}
    29  	return attrs
    30  }
    31  
    32  // PickEx picks and returns attributes of which the given attribute keys does not in given `keys`.
    33  func (m AttributeMap) PickEx(keys ...string) Attributes {
    34  	var (
    35  		exKeyMap = make(map[string]struct{})
    36  		attrs    = make(Attributes, 0)
    37  	)
    38  	for _, key := range keys {
    39  		exKeyMap[key] = struct{}{}
    40  	}
    41  	for k, v := range m {
    42  		_, ok := exKeyMap[k]
    43  		if ok {
    44  			continue
    45  		}
    46  		attrs = append(attrs, NewAttribute(k, v))
    47  	}
    48  	return attrs
    49  }