aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfranczc <fchin@biamp.com>2019-11-11 16:31:22 +1000
committerMichael Hirsch, Ph.D <10931741+scivision@users.noreply.github.com>2019-11-12 09:21:10 -0500
commitadb4e071e688534fd7c163719f005239cd2f7c25 (patch)
treee0c3a863adc14a28eb815d637ddd302d02ad9a97
parent51d0e02292f571a0408c8a6a932b352fc6fd48e4 (diff)
downloadmeson-adb4e071e688534fd7c163719f005239cd2f7c25.zip
meson-adb4e071e688534fd7c163719f005239cd2f7c25.tar.gz
meson-adb4e071e688534fd7c163719f005239cd2f7c25.tar.bz2
Adding dictionary entry using string variable as key.
-rw-r--r--docs/markdown/snippets/add_dictionary_variable_key.md17
-rw-r--r--mesonbuild/interpreterbase.py13
-rw-r--r--mesonbuild/mparser.py20
-rw-r--r--test cases/common/228 add dict variable key/meson.build12
4 files changed, 51 insertions, 11 deletions
diff --git a/docs/markdown/snippets/add_dictionary_variable_key.md b/docs/markdown/snippets/add_dictionary_variable_key.md
new file mode 100644
index 0000000..373ce04
--- /dev/null
+++ b/docs/markdown/snippets/add_dictionary_variable_key.md
@@ -0,0 +1,17 @@
+## Adding dictionary entry using string variable as key
+
+New dictionary entry can now be added using string variable as key,
+in addition to using string literal as key.
+
+```meson
+dict = {}
+
+# A variable to be used as a key
+key = 'myKey'
+
+# Add new entry using the variable
+dict += {key : 'myValue'}
+
+# Test that the stored value is correct
+assert(dict[key] == 'myValue', 'Incorrect value retrieved from dictionary')
+```
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py
index 0bea05b..a04ff38 100644
--- a/mesonbuild/interpreterbase.py
+++ b/mesonbuild/interpreterbase.py
@@ -731,7 +731,16 @@ The result of this is undefined and will become a hard error in a future Meson r
elif isinstance(old_variable, dict):
if not isinstance(addition, dict):
raise InvalidArguments('The += operator requires a dict on the right hand side if the variable on the left is a dict')
- new_value = {**old_variable, **addition}
+ new_addition = {}
+ for (key, value) in addition.items():
+ if isinstance(key, str):
+ new_addition[key] = value
+ elif isinstance(key, mparser.IdNode):
+ FeatureNew('Adding dictionary entry using string variable as key', '0.53.0').use(self.subproject)
+ new_addition[self.get_variable(key.value)] = value
+ else:
+ raise InvalidArguments('Dictionary key must be a string or string variable')
+ new_value = {**old_variable, **new_addition}
# Add other data types here.
else:
raise InvalidArguments('The += operator currently only works with arrays, dicts, strings or ints ')
@@ -1021,8 +1030,6 @@ The result of this is undefined and will become a hard error in a future Meson r
reduced_pos = [self.evaluate_statement(arg) for arg in args.arguments]
reduced_kw = {}
for key in args.kwargs.keys():
- if not isinstance(key, str):
- raise InvalidArguments('Keyword argument name is not a string.')
a = args.kwargs[key]
reduced_kw[key] = self.evaluate_statement(a)
self.argument_depth -= 1
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
index b4fb032..2c547d8 100644
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -676,14 +676,18 @@ class Parser:
while not isinstance(s, EmptyNode):
potential = self.current
if self.accept('colon'):
- if not isinstance(s, StringNode):
- raise ParseException('Key must be a string.',
- self.getline(), s.lineno, s.colno)
- if s.value in a.kwargs:
- # + 1 to colno to point to the actual string, not the opening quote
- raise ParseException('Duplicate dictionary key: {}'.format(s.value),
- self.getline(), s.lineno, s.colno + 1)
- a.set_kwarg(s.value, self.statement())
+ if isinstance(s, StringNode):
+ if s.value in a.kwargs:
+ # + 1 to colno to point to the actual string, not the opening quote
+ raise ParseException('Duplicate dictionary key: {}'.format(s.value), self.getline(), s.lineno, s.colno + 1)
+ a.set_kwarg(s.value, self.statement())
+ elif isinstance(s, IdNode) and isinstance(s.value, str):
+ for key in a.kwargs:
+ if s.value == key.value:
+ raise ParseException('Duplicate dictionary variable key: {}'.format(s.value), self.getline(), s.lineno, s.colno)
+ a.set_kwarg(s, self.statement())
+ else:
+ raise ParseException('Key must be a string or string variable', self.getline(), s.lineno, s.colno)
potential = self.current
if not self.accept('comma'):
return a
diff --git a/test cases/common/228 add dict variable key/meson.build b/test cases/common/228 add dict variable key/meson.build
new file mode 100644
index 0000000..b39c17e
--- /dev/null
+++ b/test cases/common/228 add dict variable key/meson.build
@@ -0,0 +1,12 @@
+project('add dictionary entry using string variable as key', meson_version: '>=0.52')
+
+dict = {}
+
+# A variable to be used as a key
+key = 'myKey'
+
+# Add new entry using the variable
+dict += {key : 'myValue'}
+
+# Test that the stored value is correct
+assert(dict[key] == 'myValue', 'Incorrect value retrieved from dictionary')