summaryrefslogtreecommitdiff
path: root/examples/json-schema-to-grammar.py
diff options
context:
space:
mode:
authorOlivier Chafik <ochafik@users.noreply.github.com>2024-03-22 13:07:44 +0000
committerGitHub <noreply@github.com>2024-03-22 15:07:44 +0200
commit72114edf068a9b2f54d3b09e9a198f611be397e8 (patch)
tree0614f917e7ecd6a9c86dbcebd43ce4161dea8997 /examples/json-schema-to-grammar.py
parent2f0e81e053b41ca28e73a841e7bdbf9820baaa57 (diff)
json-schema-to-grammar : fix order of props + non-str const/enum (#6232)
* json: ordered json in server/schema converter to respect orig order * json: ws nits * json: support non-string const / enums
Diffstat (limited to 'examples/json-schema-to-grammar.py')
-rwxr-xr-xexamples/json-schema-to-grammar.py7
1 files changed, 3 insertions, 4 deletions
diff --git a/examples/json-schema-to-grammar.py b/examples/json-schema-to-grammar.py
index 9eead557..91dd734c 100755
--- a/examples/json-schema-to-grammar.py
+++ b/examples/json-schema-to-grammar.py
@@ -61,7 +61,7 @@ class SchemaConverter:
def _format_literal(self, literal):
escaped = GRAMMAR_LITERAL_ESCAPE_RE.sub(
- lambda m: GRAMMAR_LITERAL_ESCAPES.get(m.group(0)), json.dumps(literal)
+ lambda m: GRAMMAR_LITERAL_ESCAPES.get(m.group(0)), literal
)
return f'"{escaped}"'
@@ -308,8 +308,7 @@ class SchemaConverter:
return ref_name
def _generate_constant_rule(self, value):
- assert isinstance(value, str), f'Only string constants are supported, got {value}'
- return self._format_literal(value)
+ return self._format_literal(json.dumps(value))
def visit(self, schema, name):
schema_type = schema.get('type')
@@ -428,7 +427,7 @@ class SchemaConverter:
prop_rule_name = self.visit(prop_schema, f'{name}{"-" if name else ""}{prop_name}')
prop_kv_rule_names[prop_name] = self._add_rule(
f'{name}{"-" if name else ""}{prop_name}-kv',
- fr'{self._format_literal(prop_name)} space ":" space {prop_rule_name}'
+ fr'{self._format_literal(json.dumps(prop_name))} space ":" space {prop_rule_name}'
)
required_props = [k for k in sorted_props if k in required]
optional_props = [k for k in sorted_props if k not in required]