Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 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 |
| 8 | |
| 9 | // Tag is a name/vlaue pair. |
| 10 | type Tag struct { |
| 11 | Name string |
| 12 | Value string |
| 13 | } |
| 14 | |
| 15 | // NewTagSetFromMap creates a new tag set from a map. |
| 16 | func NewTagSetFromMap(m map[string]string) Set { |
| 17 | var set Set |
| 18 | for k, v := range m { |
| 19 | set = append(set, Tag{Name: k, Value: v}) |
| 20 | } |
| 21 | |
| 22 | return set |
| 23 | } |
| 24 | |
| 25 | // NewTagSetsFromMaps creates new tag sets from maps. |
| 26 | func NewTagSetsFromMaps(maps []map[string]string) []Set { |
| 27 | sets := make([]Set, 0, len(maps)) |
| 28 | for _, m := range maps { |
| 29 | sets = append(sets, NewTagSetFromMap(m)) |
| 30 | } |
| 31 | return sets |
| 32 | } |
| 33 | |
| 34 | // Set is an ordered list of Tags. |
| 35 | type Set []Tag |
| 36 | |
| 37 | // Contains indicates whether the name/value pair exists in the tagset. |
| 38 | func (ts Set) Contains(name, value string) bool { |
| 39 | for _, t := range ts { |
| 40 | if t.Name == name && t.Value == value { |
| 41 | return true |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return false |
| 46 | } |
| 47 | |
| 48 | // ContainsAll indicates whether all the name/value pairs exist in the tagset. |
| 49 | func (ts Set) ContainsAll(other []Tag) bool { |
| 50 | for _, ot := range other { |
| 51 | if !ts.Contains(ot.Name, ot.Value) { |
| 52 | return false |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return true |
| 57 | } |