github.com/Jeffail/benthos/v3@v3.65.0/lib/message/metadata/type.go (about)

     1  package metadata
     2  
     3  import (
     4  	"github.com/Jeffail/benthos/v3/lib/types"
     5  )
     6  
     7  //------------------------------------------------------------------------------
     8  
     9  // Type is an implementation of types.Metadata representing the metadata of a
    10  // message part within a batch.
    11  type Type struct {
    12  	m map[string]string
    13  }
    14  
    15  // New creates a new metadata implementation from a map[string]string. It is
    16  // safe to provide a nil map.
    17  func New(m map[string]string) *Type {
    18  	return &Type{
    19  		m: m,
    20  	}
    21  }
    22  
    23  //------------------------------------------------------------------------------
    24  
    25  // Copy returns a copy of the metadata object that can be edited without
    26  // changing the contents of the original.
    27  func (m *Type) Copy() types.Metadata {
    28  	var newMap map[string]string
    29  	if m.m != nil {
    30  		newMap = make(map[string]string, len(m.m))
    31  		for k, v := range m.m {
    32  			newMap[k] = v
    33  		}
    34  	}
    35  	return New(newMap)
    36  }
    37  
    38  // Get returns a metadata value if a key exists, otherwise an empty string.
    39  func (m *Type) Get(key string) string {
    40  	if m.m == nil {
    41  		return ""
    42  	}
    43  	return m.m[key]
    44  }
    45  
    46  // Set sets the value of a metadata key.
    47  func (m *Type) Set(key, value string) types.Metadata {
    48  	if m.m == nil {
    49  		m.m = map[string]string{
    50  			key: value,
    51  		}
    52  		return m
    53  	}
    54  	m.m[key] = value
    55  	return m
    56  }
    57  
    58  // Delete removes the value of a metadata key.
    59  func (m *Type) Delete(key string) types.Metadata {
    60  	if m.m == nil {
    61  		return m
    62  	}
    63  	delete(m.m, key)
    64  	return m
    65  }
    66  
    67  // Iter iterates each metadata key/value pair.
    68  func (m *Type) Iter(f func(k, v string) error) error {
    69  	if m.m == nil {
    70  		return nil
    71  	}
    72  	for ak, av := range m.m {
    73  		if err := f(ak, av); err != nil {
    74  			return err
    75  		}
    76  	}
    77  	return nil
    78  }
    79  
    80  //------------------------------------------------------------------------------