Copied mongo repo (to patch it)

This commit is contained in:
2023-06-18 15:50:55 +02:00
parent 21d241f9b1
commit d471d7c396
544 changed files with 142039 additions and 1 deletions

80
mongo/tag/tag.go Normal file
View File

@@ -0,0 +1,80 @@
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Package tag provides a way to define filters for tagged servers.
package tag // import "go.mongodb.org/mongo-driver/tag"
import (
"bytes"
"fmt"
)
// Tag is a name/vlaue pair.
type Tag struct {
Name string
Value string
}
// String returns a human-readable human-readable description of the tag.
func (tag Tag) String() string {
return fmt.Sprintf("%s=%s", tag.Name, tag.Value)
}
// NewTagSetFromMap creates a new tag set from a map.
func NewTagSetFromMap(m map[string]string) Set {
var set Set
for k, v := range m {
set = append(set, Tag{Name: k, Value: v})
}
return set
}
// NewTagSetsFromMaps creates new tag sets from maps.
func NewTagSetsFromMaps(maps []map[string]string) []Set {
sets := make([]Set, 0, len(maps))
for _, m := range maps {
sets = append(sets, NewTagSetFromMap(m))
}
return sets
}
// Set is an ordered list of Tags.
type Set []Tag
// Contains indicates whether the name/value pair exists in the tagset.
func (ts Set) Contains(name, value string) bool {
for _, t := range ts {
if t.Name == name && t.Value == value {
return true
}
}
return false
}
// ContainsAll indicates whether all the name/value pairs exist in the tagset.
func (ts Set) ContainsAll(other []Tag) bool {
for _, ot := range other {
if !ts.Contains(ot.Name, ot.Value) {
return false
}
}
return true
}
// String returns a human-readable human-readable description of the tagset.
func (ts Set) String() string {
var b bytes.Buffer
for i, tag := range ts {
if i > 0 {
b.WriteString(",")
}
b.WriteString(tag.String())
}
return b.String()
}

96
mongo/tag/tag_test.go Normal file
View File

@@ -0,0 +1,96 @@
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package tag
import (
"testing"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
)
func TestTag_String(t *testing.T) {
t.Parallel()
tag := Tag{Name: "a", Value: "1"}
assert.Equal(t, "a=1", tag.String(), `expected "a=1", got %q`, tag.String())
}
func TestTagSets_NewTagSet(t *testing.T) {
t.Parallel()
ts := Set{Tag{Name: "a", Value: "1"}}
require.True(t, ts.Contains("a", "1"))
require.False(t, ts.Contains("1", "a"))
require.False(t, ts.Contains("A", "1"))
require.False(t, ts.Contains("a", "10"))
}
func TestTagSets_NewTagSetFromMap(t *testing.T) {
t.Parallel()
ts := NewTagSetFromMap(map[string]string{"a": "1"})
require.True(t, ts.Contains("a", "1"))
require.False(t, ts.Contains("1", "a"))
require.False(t, ts.Contains("A", "1"))
require.False(t, ts.Contains("a", "10"))
}
func TestTagSets_NewTagSetsFromMaps(t *testing.T) {
t.Parallel()
tss := NewTagSetsFromMaps([]map[string]string{{"a": "1"}, {"b": "1"}})
require.Len(t, tss, 2)
ts := tss[0]
require.True(t, ts.Contains("a", "1"))
require.False(t, ts.Contains("1", "a"))
require.False(t, ts.Contains("A", "1"))
require.False(t, ts.Contains("a", "10"))
ts = tss[1]
require.True(t, ts.Contains("b", "1"))
require.False(t, ts.Contains("1", "b"))
require.False(t, ts.Contains("B", "1"))
require.False(t, ts.Contains("b", "10"))
}
func TestTagSets_ContainsAll(t *testing.T) {
t.Parallel()
ts := Set{
Tag{Name: "a", Value: "1"},
Tag{Name: "b", Value: "2"},
}
test := Set{Tag{Name: "a", Value: "1"}}
require.True(t, ts.ContainsAll(test))
test = Set{Tag{Name: "a", Value: "1"}, Tag{Name: "b", Value: "2"}}
require.True(t, ts.ContainsAll(test))
test = Set{Tag{Name: "a", Value: "1"}, Tag{Name: "b", Value: "2"}}
require.True(t, ts.ContainsAll(test))
test = Set{Tag{Name: "a", Value: "2"}, Tag{Name: "b", Value: "1"}}
require.False(t, ts.ContainsAll(test))
test = Set{Tag{Name: "a", Value: "1"}, Tag{Name: "b", Value: "1"}}
require.False(t, ts.ContainsAll(test))
test = Set{Tag{Name: "a", Value: "2"}, Tag{Name: "b", Value: "2"}}
require.False(t, ts.ContainsAll(test))
}
func TestTagSets_String(t *testing.T) {
t.Parallel()
ts := Set{
Tag{Name: "a", Value: "1"},
Tag{Name: "b", Value: "2"},
}
assert.Equal(t, "a=1,b=2", ts.String(), `expected "a=1,b=2", got %q`, ts.String())
}