1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Improve the warning message about duplicate yaml dict keys

This commit is contained in:
Toshio Kuratomi 2015-10-27 14:19:39 -07:00
parent 87269599a0
commit e3e2db1119

View file

@ -43,15 +43,19 @@ class AnsibleConstructor(Constructor):
data.ansible_pos = self._node_position_info(node) data.ansible_pos = self._node_position_info(node)
def construct_mapping(self, node, deep=False): def construct_mapping(self, node, deep=False):
# This first part fixes a problem with pyyaml's handling of duplicate # Most of this is from yaml.constructor.SafeConstructor. We replicate
# dict keys. # it here so that we can warn users when they have duplicate dict keys
# from SafeConstructor # (pyyaml silently allows overwriting keys)
if not isinstance(node, MappingNode): if not isinstance(node, MappingNode):
raise ConstructorError(None, None, raise ConstructorError(None, None,
"expected a mapping node, but found %s" % node.id, "expected a mapping node, but found %s" % node.id,
node.start_mark) node.start_mark)
self.flatten_mapping(node) self.flatten_mapping(node)
mapping = AnsibleMapping() mapping = AnsibleMapping()
# Add our extra information to the returned value
mapping.ansible_pos = self._node_position_info(node)
for key_node, value_node in node.value: for key_node, value_node in node.value:
key = self.construct_object(key_node, deep=deep) key = self.construct_object(key_node, deep=deep)
try: try:
@ -61,14 +65,11 @@ class AnsibleConstructor(Constructor):
"found unacceptable key (%s)" % exc, key_node.start_mark) "found unacceptable key (%s)" % exc, key_node.start_mark)
if key in mapping: if key in mapping:
display.warning('While constructing a mapping%s found a duplicate dict key (%s). Using last value only.' % (node.start_mark, key_node.start_mark)) display.warning('While constructing a mapping from {1}, line {2}, column {3}, found a duplicate dict key ({0}). Using last defined value only.'.format(key, *mapping.ansible_pos))
value = self.construct_object(value_node, deep=deep) value = self.construct_object(value_node, deep=deep)
mapping[key] = value mapping[key] = value
# Add our extra information to the returned value
mapping.ansible_pos = self._node_position_info(node)
return mapping return mapping
def construct_yaml_str(self, node): def construct_yaml_str(self, node):