Commit c79e011e authored by Paul Warren's avatar Paul Warren

Refactor parsing of lists.

parent 89248be4
......@@ -67,25 +67,8 @@ class Parser:
self.eat_whitespace()
if self.get_token(':'):
while True:
self.eat_whitespace()
m = self.get_token('\$\(([-a-z]+)\)')
if m is not None:
c.add_type(m.group(1));
elif self.get_token('\$'):
c.add_type("string");
elif self.get_token('\#'):
c.add_type("number");
elif self.get_token('B'):
c.add_type("boolean");
elif self.get_token('\~'):
c.add_type("null");
elif self.get_token('\{'):
self.parse_object(c)
elif self.get_token('\['):
self.parse_array(c)
else:
break
self.parse_type_definition(c)
elif self.get_token('\.\.\.'):
x.allow_additional_properties()
......@@ -100,21 +83,39 @@ class Parser:
else:
raise ParseError("Unexpected input: " + self.s[self.p:])
def parse_type_definition(self, c):
while True:
self.eat_whitespace()
m = self.get_token('\$\(([-a-z]+)\)')
if m is not None:
c.add_type(m.group(1));
elif self.get_token('\$'):
c.add_type("string");
elif self.get_token('\#'):
c.add_type("number");
elif self.get_token('B'):
c.add_type("boolean");
elif self.get_token('\~'):
c.add_type("null");
elif self.get_token('\{'):
self.parse_object(c)
elif self.get_token('\['):
self.parse_array(c)
else:
break
def parse_array(self, x):
c = SchemaComponent()
x.add_type("array")
while True:
if self.get_token('\s*\#'):
x.add_item_type('number')
continue
if self.get_token('\s*\$'):
x.add_item_type('string')
continue
if self.get_token('\s*\%'):
x.add_item_type('boolean')
continue
if self.get_token('\s*\]'):
return
raise ParseError("Unexpected input: " + self.s[self.p:])
x.set_items(c)
self.parse_type_definition(c)
self.eat_whitespace()
if self.get_token('\]'):
return
raise ParseError("Unexpected input: " + self.s[self.p:])
class SchemaComponent:
......@@ -122,7 +123,7 @@ class SchemaComponent:
self.properties = {}
self.types = []
self.optional = []
self.items = []
self.items = None
self.base_type = None
self.additional_properties = False
......@@ -135,8 +136,8 @@ class SchemaComponent:
def make_optional(self, n):
self.optional.append(n)
def add_item_type(self, t):
self.items.append(t)
def set_items(self, c):
self.items = c
def extends(self, b):
self.base_type = b
......@@ -162,10 +163,8 @@ class SchemaComponent:
if len(required) > 0:
schema["required"] = required
if len(self.items) == 1:
schema["items"] = {"type": self.items[0]}
elif len(self.items) > 1:
schema["items"] = {"type": self.items}
if self.items is not None:
schema["items"] = self.items.serialise()
if "object" in self.types:
schema["additionalProperties"] = self.additional_properties
......
......@@ -93,7 +93,7 @@ def test_short_schema():
}
},
{
"input": "{thing:[%]}",
"input": "{thing:[B]}",
"output": {
"type": "object",
"properties": {
......@@ -106,21 +106,23 @@ def test_short_schema():
{
"input": """
{
thing:[ % ] }""",
thing:[ B$ ] }""",
"output": {
"type": "object",
"properties": {
"thing": { "type": "array", "items": { "type": "boolean" } },
"thing": { "type": "array", "items": { "type": [ "boolean", "string"] } },
},
"required": [ "thing" ],
"additionalProperties": False
},
"valid_instances": [
{ "thing": [ True, False ] },
{ "thing": [ ] },
{ "thing": [ "foo" ] },
{ "thing": [ "bar", False ] },
],
"invalid_instances": [
{ "thing": [ 1 ] },
{ "thing": [ None ] },
{ "thing": { } },
],
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment