[BACKEND] Simplify script quote generation (#1409)
Internal script -> string generation used triple-quotes redundantly. Try to use them less to make it more human-readable.
This commit is contained in:
parent
b2056bec5d
commit
6b99c31e45
3 changed files with 46 additions and 6 deletions
|
|
@ -105,6 +105,19 @@ class ScriptUtils:
|
||||||
|
|
||||||
raise UNREACHABLE
|
raise UNREACHABLE
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _get_quote_char(cls, arg: str) -> str:
|
||||||
|
contains_single_quote = "'" in arg
|
||||||
|
contains_double_quote = '"' in arg
|
||||||
|
|
||||||
|
if not contains_single_quote and not contains_double_quote:
|
||||||
|
return '"'
|
||||||
|
if not contains_single_quote and contains_double_quote:
|
||||||
|
return "'"
|
||||||
|
if contains_single_quote and not contains_double_quote:
|
||||||
|
return '"'
|
||||||
|
return "'''"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _to_script_code(cls, arg: Argument, top_level: bool = False) -> str:
|
def _to_script_code(cls, arg: Argument, top_level: bool = False) -> str:
|
||||||
if not top_level and isinstance(arg, (Integer, Boolean, Float)):
|
if not top_level and isinstance(arg, (Integer, Boolean, Float)):
|
||||||
|
|
@ -113,7 +126,10 @@ class ScriptUtils:
|
||||||
if isinstance(arg, String):
|
if isinstance(arg, String):
|
||||||
if arg.native == "":
|
if arg.native == "":
|
||||||
return "" if top_level else "''"
|
return "" if top_level else "''"
|
||||||
return arg.native if top_level else f"'''{arg.native}'''"
|
|
||||||
|
quote = cls._get_quote_char(arg.native)
|
||||||
|
|
||||||
|
return arg.native if top_level else f"{quote}{arg.native}{quote}"
|
||||||
|
|
||||||
if isinstance(arg, Integer):
|
if isinstance(arg, Integer):
|
||||||
out = f"%int({arg.native})"
|
out = f"%int({arg.native})"
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,9 @@ class TestScriptUtils:
|
||||||
"string": "value",
|
"string": "value",
|
||||||
"quotes": "has '' and \"\"",
|
"quotes": "has '' and \"\"",
|
||||||
"triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''",
|
"triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''",
|
||||||
|
"has-double-quotes": 'i got "some double quotes" in here',
|
||||||
|
"has-single-quotes": "i got 'some single quotes' in here",
|
||||||
|
"has-both-quotes": "i got 'both quotes\" in here",
|
||||||
"int": 1,
|
"int": 1,
|
||||||
"bool": True,
|
"bool": True,
|
||||||
"list": [1, 2, 3],
|
"list": [1, 2, 3],
|
||||||
|
|
@ -60,7 +63,15 @@ class TestScriptUtils:
|
||||||
|
|
||||||
def test_to_syntax_tree(self):
|
def test_to_syntax_tree(self):
|
||||||
out = ScriptUtils.to_native_script(
|
out = ScriptUtils.to_native_script(
|
||||||
{"{var_a}": "{var_b}", "static_a": "string with {var_c} in it"}
|
{
|
||||||
|
"{var_a}": "{var_b}",
|
||||||
|
"static_a": "string with {var_c} in it",
|
||||||
|
"quotes": "has '' and \"\"",
|
||||||
|
"triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''",
|
||||||
|
"has-double-quotes": 'i got "some double quotes" in here',
|
||||||
|
"has-single-quotes": "i got 'some single quotes' in here",
|
||||||
|
"has-both-quotes": "i got 'both quotes\" in here",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
assert parse(out) == SyntaxTree(
|
assert parse(out) == SyntaxTree(
|
||||||
ast=[
|
ast=[
|
||||||
|
|
@ -75,6 +86,19 @@ class TestScriptUtils:
|
||||||
BuiltInFunction(name="string", args=[String(value=" in it")]),
|
BuiltInFunction(name="string", args=[String(value=" in it")]),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
String(value="quotes"): String(value="has '' and \"\""),
|
||||||
|
String(value="triple-single-quote"): String(
|
||||||
|
value="right here! '''''''''''''''''''''''''''''' ack '''''''"
|
||||||
|
),
|
||||||
|
String(value="has-double-quotes"): String(
|
||||||
|
value='i got "some double quotes" in here'
|
||||||
|
),
|
||||||
|
String(value="has-single-quotes"): String(
|
||||||
|
value="i got 'some single quotes' in here"
|
||||||
|
),
|
||||||
|
String(value="has-both-quotes"): String(
|
||||||
|
value="i got 'both quotes\" in here"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -115,12 +115,12 @@ class TestUnstructuredDictFormatterValidator(object):
|
||||||
assert len(validator.dict) == 8
|
assert len(validator.dict) == 8
|
||||||
assert all(isinstance(val, expected_formatter_class) for val in validator.dict.values())
|
assert all(isinstance(val, expected_formatter_class) for val in validator.dict.values())
|
||||||
assert validator.dict_with_format_strings == {
|
assert validator.dict_with_format_strings == {
|
||||||
"key1": "{ %concat( %string( '''string with ''' ), %string( variable ) ) }",
|
"key1": '{ %concat( %string( "string with " ), %string( variable ) ) }',
|
||||||
"key2": "no variables",
|
"key2": "no variables",
|
||||||
"key3": "{ %int(3) }",
|
"key3": "{ %int(3) }",
|
||||||
"key4": "{ %float(4.132) }",
|
"key4": "{ %float(4.132) }",
|
||||||
"key5": "{ %bool(True) }",
|
"key5": "{ %bool(True) }",
|
||||||
"key6": "{ { %concat( %string( variable ), %string( '''_key''' ) ): '''value''', '''static_key''': %concat( %string( variable ), %string( '''_value''' ) ) } }",
|
"key6": '{ { %concat( %string( variable ), %string( "_key" ) ): "value", "static_key": %concat( %string( variable ), %string( "_value" ) ) } }',
|
||||||
"key7": "{ [ '''list_1''', %concat( %string( '''list_''' ), %string( variable_2 ) ) ] }",
|
"key7": '{ [ "list_1", %concat( %string( "list_" ), %string( variable_2 ) ) ] }',
|
||||||
"key8": "{ %concat( %string( '''string ''' ), %string( variable1 ), %string( ''' with multiple ''' ), %string( variable2 ) ) }",
|
"key8": '{ %concat( %string( "string " ), %string( variable1 ), %string( " with multiple " ), %string( variable2 ) ) }',
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue