1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ from collections import OrderedDict
16+
17+ from opencensus .tags .tag_key import TagKey
18+ from opencensus .tags .tag_value import TagValue
19+
1520
1621class TagMap (object ):
1722 """ A tag map is a map of tags from key to value
@@ -20,21 +25,12 @@ class TagMap(object):
2025 :param tags: a list of tags
2126
2227 """
23- def __init__ (self , tags = None ):
24- self ._map = {}
25- if tags is not None :
26- self .tags = tags
27- for tag in self .tags :
28- for tag_key , tag_value in tag .items ():
29- self ._map [tag_key ] = tag_value
3028
31- else :
32- self . _map = {}
29+ def __init__ ( self , tags = None ) :
30+ self . map = OrderedDict ( tags if tags else [])
3331
34- @property
35- def map (self ):
36- """The current map of tags"""
37- return self ._map
32+ def __iter__ (self ):
33+ return self .map .items ().__iter__ ()
3834
3935 def insert (self , key , value ):
4036 """Inserts a key and value in the map if the map does not already
@@ -48,43 +44,50 @@ def insert(self, key, value):
4844 the value to insert into the tag map
4945
5046 """
51- if key not in self ._map :
52- self ._map [key ] = value
47+ if key in self .map :
48+ return
49+
50+ try :
51+ tag_key = TagKey (key )
52+ tag_val = TagValue (value )
53+ self .map [tag_key ] = tag_val
54+ except ValueError :
55+ raise
5356
5457 def delete (self , key ):
55- """ Deletes a tag from the map if the key is in the map
58+ """Deletes a tag from the map if the key is in the map
5659
57- :type key: str
60+ :type key: :class: '~opencensus.tags.tag_key.TagKey'
5861 :param key: A string representing a possible tag key
5962
6063 :returns: the value of the key in the dictionary if it is in there,
6164 or None if it is not.
6265 """
63- self ._map .pop (key , None )
66+ self .map .pop (key , None )
6467
6568 def update (self , key , value ):
66- """ Updates the map by updating the value of a key
69+ """Updates the map by updating the value of a key
6770
6871 :type key: :class: '~opencensus.tags.tag_key.TagKey'
6972 :param key: A tag key to be updated
7073
71- :type value: str
74+ :type value: :class: '~opencensus.tags.tag_value.TagValue'
7275 :param value: The value to update the key to in the map
7376
7477 """
75- if key in self ._map :
76- self ._map [key ] = value
78+ if key in self .map :
79+ self .map [key ] = value
7780
7881 def tag_key_exists (self , key ):
79- """ Checking if the tag key exists in the map
82+ """Checking if the tag key exists in the map
8083
81- :type key: str
84+ :type key: '~opencensus.tags.tag_key.TagKey'
8285 :param key: A string to check to see if that is a key in the map
8386
8487 :returns: True if the key is in map, False is it is not
8588
8689 """
87- return key in self ._map
90+ return key in self .map
8891
8992 def get_value (self , key ):
9093 """ Gets the value of the key passed in if the key exists in the map
@@ -95,8 +98,7 @@ def get_value(self, key):
9598 :returns: A KeyError if the value is None, else returns the value
9699
97100 """
98- value = self ._map .get (key , None )
99- if value is None :
100- raise KeyError ('Key is not in map.' )
101-
102- return value
101+ try :
102+ return self .map [key ]
103+ except KeyError :
104+ raise KeyError ('key is not in map' )
0 commit comments