...

Source file src/go.mongodb.org/mongo-driver/tag/tag.go

Documentation: go.mongodb.org/mongo-driver/tag

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  // Package tag provides types for filtering replica set members using tags in a read preference.
     8  //
     9  // For more information about read preference tags, see
    10  // https://www.mongodb.com/docs/manual/core/read-preference-tags/
    11  package tag // import "go.mongodb.org/mongo-driver/tag"
    12  
    13  import (
    14  	"bytes"
    15  	"fmt"
    16  )
    17  
    18  // Tag is a name/value pair.
    19  type Tag struct {
    20  	Name  string
    21  	Value string
    22  }
    23  
    24  // String returns a human-readable human-readable description of the tag.
    25  func (tag Tag) String() string {
    26  	return fmt.Sprintf("%s=%s", tag.Name, tag.Value)
    27  }
    28  
    29  // NewTagSetFromMap creates a tag set from a map.
    30  //
    31  // For more information about read preference tags, see
    32  // https://www.mongodb.com/docs/manual/core/read-preference-tags/
    33  func NewTagSetFromMap(m map[string]string) Set {
    34  	var set Set
    35  	for k, v := range m {
    36  		set = append(set, Tag{Name: k, Value: v})
    37  	}
    38  
    39  	return set
    40  }
    41  
    42  // NewTagSetsFromMaps creates a list of tag sets from a slice of maps.
    43  //
    44  // For more information about read preference tags, see
    45  // https://www.mongodb.com/docs/manual/core/read-preference-tags/
    46  func NewTagSetsFromMaps(maps []map[string]string) []Set {
    47  	sets := make([]Set, 0, len(maps))
    48  	for _, m := range maps {
    49  		sets = append(sets, NewTagSetFromMap(m))
    50  	}
    51  	return sets
    52  }
    53  
    54  // Set is an ordered list of Tags.
    55  type Set []Tag
    56  
    57  // Contains indicates whether the name/value pair exists in the tagset.
    58  func (ts Set) Contains(name, value string) bool {
    59  	for _, t := range ts {
    60  		if t.Name == name && t.Value == value {
    61  			return true
    62  		}
    63  	}
    64  
    65  	return false
    66  }
    67  
    68  // ContainsAll indicates whether all the name/value pairs exist in the tagset.
    69  func (ts Set) ContainsAll(other []Tag) bool {
    70  	for _, ot := range other {
    71  		if !ts.Contains(ot.Name, ot.Value) {
    72  			return false
    73  		}
    74  	}
    75  
    76  	return true
    77  }
    78  
    79  // String returns a human-readable human-readable description of the tagset.
    80  func (ts Set) String() string {
    81  	var b bytes.Buffer
    82  	for i, tag := range ts {
    83  		if i > 0 {
    84  			b.WriteString(",")
    85  		}
    86  		b.WriteString(tag.String())
    87  	}
    88  	return b.String()
    89  }
    90  

View as plain text