Skip to content

Commit cd69824

Browse files
committed
BaseNode.to_json() now accepts a processing function
1 parent 2e91201 commit cd69824

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

fluent/syntax/ast.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import json
44

55

6-
def to_json(value, **kwargs):
6+
def to_json(value, fn=None):
77
if isinstance(value, BaseNode):
8-
return value.to_json(**kwargs)
8+
return value.to_json(fn)
99
if isinstance(value, list):
10-
return list(to_json(item, **kwargs) for item in value)
10+
return list(to_json(item, fn) for item in value)
1111
if isinstance(value, tuple):
12-
return list(to_json(item, **kwargs) for item in value)
12+
return list(to_json(item, fn) for item in value)
1313
else:
1414
return value
1515

@@ -119,16 +119,15 @@ def equals(self, other, ignored_fields=['span']):
119119

120120
return True
121121

122-
def to_json(self, with_spans=True):
122+
def to_json(self, fn=None):
123123
obj = {
124-
name: to_json(value, with_spans=with_spans)
124+
name: to_json(value, fn)
125125
for name, value in vars(self).items()
126-
if with_spans or name != 'span'
127126
}
128127
obj.update(
129128
{'type': self.__class__.__name__}
130129
)
131-
return obj
130+
return fn(obj) if fn else obj
132131

133132
def __str__(self):
134133
return json.dumps(self.to_json())

tests/syntax/test_reference.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def read_file(path):
2525
class TestReferenceMeta(type):
2626
def __new__(mcs, name, bases, attrs):
2727

28+
def remove_untested(obj):
29+
if obj['type'] == 'Junk':
30+
obj['annotations'] = []
31+
if 'span' in obj:
32+
del obj['span']
33+
return obj
34+
2835
def gen_test(file_name):
2936
def test(self):
3037
ftl_path = os.path.join(fixtures, file_name + '.ftl')
@@ -34,12 +41,8 @@ def test(self):
3441
expected = read_file(ast_path)
3542

3643
ast = parse(source)
37-
for entry in ast.body:
38-
if isinstance(entry, ftl.Junk):
39-
entry.annotations = []
40-
4144
self.assertEqual(
42-
ast.to_json(with_spans=False), json.loads(expected))
45+
ast.to_json(remove_untested), json.loads(expected))
4346

4447
return test
4548

0 commit comments

Comments
 (0)