bundle2: forbid duplicate parameter keys
authorPierre-Yves David <pierre-yves.david@fb.com>
Thu, 22 May 2014 12:52:09 -0700
changeset 21607 054fa5176fa7
parent 21606 e55888447958
child 21608 3cb96ca90c17
bundle2: forbid duplicate parameter keys No rules were specified about parameter key uniqueness. We document that keys should be unique and document it. This opens the way to a more friendly (read dictionary like) way to access value of parameters in the code.
mercurial/bundle2.py
--- a/mercurial/bundle2.py	Fri May 23 16:46:30 2014 -0700
+++ b/mercurial/bundle2.py	Thu May 22 12:52:09 2014 -0700
@@ -113,6 +113,8 @@
 
             Mandatory parameters comes first, then the advisory ones.
 
+            Each parameter's key MUST be unique within the part.
+
 :payload:
 
     payload is a series of `<chunksize><chunkdata>`.
@@ -570,6 +572,12 @@
         self._data = data
         self._mandatoryparams = list(mandatoryparams)
         self._advisoryparams = list(advisoryparams)
+        # checking for duplicated entries
+        self._seenparams = set()
+        for pname, __ in self._mandatoryparams + self._advisoryparams:
+            if pname in self._seenparams:
+                raise RuntimeError('duplicated params: %s' % pname)
+            self._seenparams.add(pname)
         # status of the part's generation:
         # - None: not started,
         # - False: currently generated,
@@ -598,6 +606,9 @@
     def addparam(self, name, value='', mandatory=True):
         if self._generated is not None:
             raise ReadOnlyPartError('part is being generated')
+        if name in self._seenparams:
+            raise ValueError('duplicated params: %s' % name)
+        self._seenparams.add(name)
         params = self._advisoryparams
         if mandatory:
             params = self._mandatoryparams