mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-13 08:30:50 -07:00
[stable-9] Make ready for data tagging (#9991)
Make ready for data tagging (#9833)
* Fix dependent lookup.
* Fix ansible_type plugin utils and adjust documentation of reveal_ansible_type filter and ansible_type test.
* Fix diy callback plugin.
* Adjust to Data Tagging.
* Vendor and use internal code from ansible-core to fix YAML callback.
Ref: https://github.com/ansible/ansible/issues/84781
(cherry picked from commit 04cfce78ea
)
This commit is contained in:
parent
ff7a8f8018
commit
f12f69d2a5
13 changed files with 372 additions and 165 deletions
|
@ -8,17 +8,31 @@ __metaclass__ = type
|
|||
from ansible.errors import AnsibleFilterError
|
||||
from ansible.module_utils.common._collections_compat import Mapping
|
||||
|
||||
try:
|
||||
# Introduced with Data Tagging (https://github.com/ansible/ansible/pull/84621):
|
||||
from ansible.module_utils.datatag import native_type_name as _native_type_name
|
||||
except ImportError:
|
||||
_native_type_name = None
|
||||
|
||||
def _atype(data, alias):
|
||||
|
||||
def _atype(data, alias, *, use_native_type: bool = False):
|
||||
"""
|
||||
Returns the name of the type class.
|
||||
"""
|
||||
|
||||
data_type = type(data).__name__
|
||||
if use_native_type and _native_type_name:
|
||||
data_type = _native_type_name(data)
|
||||
else:
|
||||
data_type = type(data).__name__
|
||||
# The following types were introduced with Data Tagging (https://github.com/ansible/ansible/pull/84621):
|
||||
if data_type == "_AnsibleLazyTemplateDict":
|
||||
data_type = "dict"
|
||||
elif data_type == "_AnsibleLazyTemplateList":
|
||||
data_type = "list"
|
||||
return alias.get(data_type, data_type)
|
||||
|
||||
|
||||
def _ansible_type(data, alias):
|
||||
def _ansible_type(data, alias, *, use_native_type: bool = False):
|
||||
"""
|
||||
Returns the Ansible data type.
|
||||
"""
|
||||
|
@ -30,16 +44,16 @@ def _ansible_type(data, alias):
|
|||
msg = "The argument alias must be a dictionary. %s is %s"
|
||||
raise AnsibleFilterError(msg % (alias, type(alias)))
|
||||
|
||||
data_type = _atype(data, alias)
|
||||
data_type = _atype(data, alias, use_native_type=use_native_type)
|
||||
|
||||
if data_type == 'list' and len(data) > 0:
|
||||
items = [_atype(i, alias) for i in data]
|
||||
items = [_atype(i, alias, use_native_type=use_native_type) for i in data]
|
||||
items_type = '|'.join(sorted(set(items)))
|
||||
return ''.join((data_type, '[', items_type, ']'))
|
||||
|
||||
if data_type == 'dict' and len(data) > 0:
|
||||
keys = [_atype(i, alias) for i in data.keys()]
|
||||
vals = [_atype(i, alias) for i in data.values()]
|
||||
keys = [_atype(i, alias, use_native_type=use_native_type) for i in data.keys()]
|
||||
vals = [_atype(i, alias, use_native_type=use_native_type) for i in data.values()]
|
||||
keys_type = '|'.join(sorted(set(keys)))
|
||||
vals_type = '|'.join(sorted(set(vals)))
|
||||
return ''.join((data_type, '[', keys_type, ', ', vals_type, ']'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue