Skip to content

Commit

Permalink
Fix STRUCTURED_JSON compilation when file includes empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
igavriil committed Oct 13, 2022
1 parent 96730ff commit 074ce52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
37 changes: 26 additions & 11 deletions openformats/formats/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import six

from ..exceptions import ParseError
from ..handlers import Handler
from ..strings import OpenString
from ..transcribers import Transcriber
from ..utils.icu import ICUCompiler, ICUParser
from ..utils.json import DumbJson, escape, unescape
from openformats.exceptions import ParseError
from openformats.handlers import Handler
from openformats.strings import OpenString
from openformats.transcribers import Transcriber
from openformats.utils.icu import ICUCompiler, ICUParser
from openformats.utils.json import DumbJson, escape, unescape

try:
from StringIO import StringIO
Expand Down Expand Up @@ -509,7 +509,9 @@ def compile(self, template, translations, **kwargs):
self.transcriber.copy_to_end()
return self.transcriber.get_destination()

def _compile_value(self, value, template_value, value_position):
def _compile_value(self, value, template_value, value_position, skip=False):
value = template_value if skip else value

if value is not None:
if value == '' and template_value is None:
self.transcriber.add(u"null")
Expand All @@ -520,6 +522,7 @@ def _compile_value(self, value, template_value, value_position):
self.transcriber.add(u"{}".format(value))
else:
self.transcriber.add(u"null")

self.transcriber.skip(len(u"{}".format(template_value)))
self.transcriber.copy_until(value_position +
len(u"{}".format(template_value)) +
Expand All @@ -539,7 +542,16 @@ def _compile_recursively(self, current_part):
self.transcriber.copy_until(value_position)
self._compile_recursively(value)
else:
translation = next(self.translations, None)
(value, _) = current_part.find_children(self.STRING_KEY)[0]
if not value.strip():
translation = OpenString(
"", value
)
skip=True
else:
translation = next(self.translations, None)
skip=False

context_added = False
character_limit_added = False
developer_comments_added = False
Expand All @@ -561,19 +573,22 @@ def _compile_recursively(self, current_part):
context = translation.context
self._compile_value(self.escape(context),
value,
value_position)
value_position,
skip=skip)
context_added = True
elif key == self.DEVELOPER_COMMENT_KEY and translation:
developer_comment = translation.developer_comment
self._compile_value(self.escape(developer_comment),
value,
value_position)
value_position,
skip=skip)
developer_comments_added = True
elif key == self.CHARACTER_LIMIT_KEY and translation:
character_limit = translation.character_limit
self._compile_value(character_limit,
value,
value_position)
value_position,
skip=skip)
character_limit_added = True
elif key == self.STRING_KEY and translation:
if translation.pluralized:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def setUp(self):
self.random_string, order=0)
self.random_hash = self.random_openstring.template_replacement

def test_broken(self):
stringset = [
OpenString("b", "foo_tr", order=0)
]
string_hash = stringset[0].template_replacement
template, stringset = self.handler.parse('{"a": {"string":" ", "character_limit": 1, "developer_comment": "A"}, "b": {"string": "foo"}}') # noqa
compiled = self.handler.compile(template, stringset)
self.assertEqual(template, '{"a": {"string":" ", "character_limit": 1, "developer_comment": "A"}, "b": {"string": "%s"}}' % string_hash) # noqa
self.assertEqual(compiled, '{"a": {"string":" ", "character_limit": 1, "developer_comment": "A"}, "b": {"string": "foo"}}') # noqa

def test_simple(self):
template, stringset = self.handler.parse('{"a": {"string":"%s"}}' %
self.random_string)
Expand All @@ -40,12 +50,12 @@ def test_simple(self):
self.random_openstring.__dict__)
self.assertEqual(compiled,
'{"a": {"string":"%s"}}' % self.random_string)

def test_dots_in_key(self):
first_level_key = "a.b"
source = '{"%s": {"c": {"string": "%s"}}}' % (first_level_key, self.random_string)
openstring = OpenString(
"{}.c".format(self.handler._escape_key(first_level_key)),
"{}.c".format(self.handler._escape_key(first_level_key)),
self.random_string, order=0
)
random_hash = openstring.template_replacement
Expand All @@ -63,7 +73,7 @@ def test_escaped_character_in_key(self):
first_level_key = "a\/b"
source = '{"%s": {"c": {"string": "%s"}}}' % (first_level_key, self.random_string)
openstring = OpenString(
"{}.c".format(self.handler._escape_key(first_level_key)),
"{}.c".format(self.handler._escape_key(first_level_key)),
self.random_string, order=0
)
random_hash = openstring.template_replacement
Expand Down Expand Up @@ -460,7 +470,7 @@ def test_template_with_existing_values(self):
source = """
{
"a": {
"character_limit":150,
"character_limit":150,
"string":"%s",
"developer_comment": "i am a developer",
"context": "contexttt"
Expand All @@ -471,7 +481,7 @@ def test_template_with_existing_values(self):
expected_compilation = """
{
"a": {
"character_limit":49,
"character_limit":49,
"string":"%s",
"developer_comment": "comment_changed",
"context": "context_changed"
Expand Down

0 comments on commit 074ce52

Please sign in to comment.