metadata: add Merge func
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
c7bafecce3
commit
202a942eef
@ -35,15 +35,5 @@ func MergeContext(ctx context.Context, pmd Metadata, overwrite bool) context.Con
|
||||
if !ok {
|
||||
return context.WithValue(ctx, metadataKey{}, Copy(pmd))
|
||||
}
|
||||
nmd := Copy(md)
|
||||
for key, val := range pmd {
|
||||
if _, ok := nmd[key]; ok && !overwrite {
|
||||
// skip
|
||||
} else if val != "" {
|
||||
nmd.Set(key, val)
|
||||
} else {
|
||||
nmd.Del(key)
|
||||
}
|
||||
}
|
||||
return context.WithValue(ctx, metadataKey{}, nmd)
|
||||
return context.WithValue(ctx, metadataKey{}, Merge(md, pmd, overwrite))
|
||||
}
|
||||
|
@ -120,3 +120,18 @@ func New(size int) Metadata {
|
||||
}
|
||||
return make(Metadata, size)
|
||||
}
|
||||
|
||||
// Merge merges metadata to existing metadata, overwriting if specified
|
||||
func Merge(omd Metadata, mmd Metadata, overwrite bool) Metadata {
|
||||
nmd := Copy(omd)
|
||||
for key, val := range mmd {
|
||||
if _, ok := nmd[key]; ok && !overwrite {
|
||||
// skip
|
||||
} else if val != "" {
|
||||
nmd.Set(key, val)
|
||||
} else {
|
||||
nmd.Del(key)
|
||||
}
|
||||
}
|
||||
return nmd
|
||||
}
|
||||
|
@ -7,6 +7,20 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
omd := Metadata{
|
||||
"key1": "val1",
|
||||
}
|
||||
mmd := Metadata{
|
||||
"key2": "val2",
|
||||
}
|
||||
|
||||
nmd := Merge(omd, mmd, true)
|
||||
if len(nmd) != 2 {
|
||||
t.Fatalf("merge failed: %v", nmd)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
md := Metadata{
|
||||
"1Last": "last",
|
||||
|
Loading…
x
Reference in New Issue
Block a user