Skip to content

Commit

Permalink
Parse unrecognized tags instead of dropping them. Fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
kislyuk committed Mar 30, 2018
1 parent db1b528 commit a97a9c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
30 changes: 28 additions & 2 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
USING_PYTHON2 = True if sys.version_info < (3, 0) else False
USING_PYPY = True if platform.python_implementation() == "PyPy" else False

yaml_with_tags = """
foo: !vault |
$ANSIBLE_VAULT;1.1;AES256
3766343436323632623130303
xyz: !!mytag
foo: bar
baz: 1
xyzzt: !binary
- 1
- 2
- 3
scalar-red: !color FF0000
scalar-orange: !color FFFF00
mapping-red: !color-mapping {r: 255, g: 0, b: 0}
mapping-orange:
!color-mapping
r: 255
g: 255
b: 0
"""

class TestYq(unittest.TestCase):
def run_yq(self, input_data, args, expect_exit_code=os.EX_OK, input_format="yaml"):
stdin, stdout = sys.stdin, sys.stdout
Expand Down Expand Up @@ -79,8 +100,13 @@ def test_datetimes(self):

def test_unrecognized_tags(self):
self.assertEqual(self.run_yq("!!foo bar\n", ["."]), "")
self.assertEqual(self.run_yq("!!foo bar\n", ["-y", "."]), "null\n...\n")
self.assertEqual(self.run_yq("x: !!foo bar\n", ["-y", "."]), "x: null\n")
self.assertEqual(self.run_yq("!!foo bar\n", ["-y", "."]), "bar\n...\n")
self.assertEqual(self.run_yq("x: !foo bar\n", ["-y", "."]), "x: bar\n")
self.assertEqual(self.run_yq("x: !!foo bar\n", ["-y", "."]), "x: bar\n")
with tempfile.TemporaryFile() as tf:
tf.write(yaml_with_tags.encode())
tf.seek(0)
self.assertEqual(self.run_yq("", ["-y", ".xyz.foo", self.fd_path(tf)]), 'bar\n...\n')

@unittest.expectedFailure
def test_times(self):
Expand Down
10 changes: 9 additions & 1 deletion yq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ def decode_docs(jq_output, json_decoder):
jq_output = jq_output[pos + 1:]
yield doc

def parse_unknown_tags(loader, tag_suffix, node):
if isinstance(node, yaml.nodes.ScalarNode):
return loader.construct_scalar(node)
elif isinstance(node, yaml.nodes.SequenceNode):
return loader.construct_sequence(node)
elif isinstance(node, yaml.nodes.MappingNode):
return construct_mapping(loader, node)

OrderedLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)
OrderedLoader.add_multi_constructor('', lambda loader, tag_suffix, node: None) # Ignore all unrecognized tags
OrderedLoader.add_multi_constructor('', parse_unknown_tags)
OrderedDumper.add_representer(OrderedDict, represent_dict_order)

USING_XQ = True if os.path.basename(sys.argv[0]) == "xq" else False
Expand Down

0 comments on commit a97a9c2

Please sign in to comment.