@ -117,10 +117,17 @@ func (md MD) Len() int {
|
||||
|
||||
// Copy returns a copy of md.
|
||||
func (md MD) Copy() MD {
|
||||
return Join(md)
|
||||
}
|
||||
|
||||
// Join joins any number of MDs into a single MD.
|
||||
// The order of values for each key is determined by the order in which
|
||||
// the MDs containing those values are presented to Join.
|
||||
func Join(mds ...MD) MD {
|
||||
out := MD{}
|
||||
for _, md := range mds {
|
||||
for k, v := range md {
|
||||
for _, i := range v {
|
||||
out[k] = append(out[k], i)
|
||||
out[k] = append(out[k], v...)
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
@ -107,3 +107,33 @@ func TestPairsMD(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
const key, val = "key", "val"
|
||||
orig := Pairs(key, val)
|
||||
copy := orig.Copy()
|
||||
if !reflect.DeepEqual(orig, copy) {
|
||||
t.Errorf("copied value not equal to the original, got %v, want %v", copy, orig)
|
||||
}
|
||||
orig[key][0] = "foo"
|
||||
if v := copy[key][0]; v != val {
|
||||
t.Errorf("change in original should not affect copy, got %q, want %q", v, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
mds []MD
|
||||
want MD
|
||||
}{
|
||||
{[]MD{}, MD{}},
|
||||
{[]MD{Pairs("foo", "bar")}, Pairs("foo", "bar")},
|
||||
{[]MD{Pairs("foo", "bar"), Pairs("foo", "baz")}, Pairs("foo", "bar", "foo", "baz")},
|
||||
{[]MD{Pairs("foo", "bar"), Pairs("foo", "baz"), Pairs("zip", "zap")}, Pairs("foo", "bar", "foo", "baz", "zip", "zap")},
|
||||
} {
|
||||
md := Join(test.mds...)
|
||||
if !reflect.DeepEqual(md, test.want) {
|
||||
t.Errorf("context's metadata is %v, want %v", md, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user