...
 
Commits (2)
......@@ -28,7 +28,7 @@ class Parser:
return schema
def eat_whitespace(self):
self.get_token(r'\s*')
self.get_token(r'(?m)\s*(--[^\r\n]*$)?\s*')
def parse_object(self, x):
x.add_type("object")
......@@ -51,8 +51,8 @@ class Parser:
c.extends(b.group(1))
self.eat_whitespace()
if self.get_token('\+'):
x.add_required(name)
if self.get_token('\?'):
x.make_optional(name)
self.eat_whitespace()
......@@ -64,10 +64,10 @@ class Parser:
c.add_type("string");
elif self.get_token('\#'):
c.add_type("number");
elif self.get_token('\%'):
c.add_type("boolean");
elif self.get_token('\^'):
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('\['):
......@@ -109,7 +109,7 @@ class SchemaComponent:
def __init__(self):
self.properties = {}
self.types = []
self.required = []
self.optional = []
self.items = []
self.base_type = None
self.additional_properties = False
......@@ -120,8 +120,8 @@ class SchemaComponent:
def add_property(self, n, c):
self.properties[n] = c
def add_required(self, n):
self.required.append(n)
def make_optional(self, n):
self.optional.append(n)
def add_item_type(self, t):
self.items.append(t)
......@@ -138,13 +138,18 @@ class SchemaComponent:
schema["type"] = self.types[0]
elif len(self.types) > 1:
schema["type"] = self.types
if len(self.required) > 0:
schema["required"] = self.required
required = []
for n, c in self.properties.items():
if n[0] != '/':
schema.setdefault("properties", {})[n] = c.serialise()
if n not in self.optional:
required.append(n)
else:
schema.setdefault("patternProperties", {})[n[1:-1]] = c.serialise()
if len(required) > 0:
schema["required"] = required
if len(self.items) == 1:
schema["items"] = {"type": self.items[0]}
elif len(self.items) > 1:
......
......@@ -6,7 +6,7 @@ def test_short_schema():
tests = [
# Single required property, whitespace everywhere
{
"input": " { requiredString+ : $ } ",
"input": " { requiredString : $ } ",
"output": {
"type": "object",
"properties": {
......@@ -32,9 +32,9 @@ def test_short_schema():
{
"input": """
{
thing:{
subthing1:$,
subthing2+:$,
thing?:{
subthing1?:$, -- with comment
subthing2:$,
}
}
""",
......@@ -66,7 +66,7 @@ def test_short_schema():
},
# Additional properties allowed
{
"input": "{thing:$#, ...}",
"input": "{thing?:$#, ...}",
"output": {
"type": "object",
"properties": {
......@@ -83,7 +83,7 @@ def test_short_schema():
],
},
{
"input": "{thing:[$#]}",
"input": "{thing?:[$#]}",
"output": {
"type": "object",
"properties": {
......@@ -93,7 +93,7 @@ def test_short_schema():
}
},
{
"input": "{thing+:[%]}",
"input": "{thing:[%]}",
"output": {
"type": "object",
"properties": {
......@@ -106,7 +106,7 @@ def test_short_schema():
{
"input": """
{
thing+:[ % ] }""",
thing:[ % ] }""",
"output": {
"type": "object",
"properties": {
......@@ -126,7 +126,7 @@ def test_short_schema():
},
# Both string and object
{
"input": "{thing:${otherThing:$}}",
"input": "{thing?:${otherThing?:$}}",
"output": {
"type": "object",
"properties": {
......@@ -148,7 +148,7 @@ def test_short_schema():
},
# Empty object definitions are OK
{
"input": "{thing:{}}",
"input": "{thing?:{}}",
"output": {
"type": "object",
"properties": {
......@@ -170,8 +170,8 @@ def test_short_schema():
{
"input": """
{
thing(BaseType),
otherThing(BaseType)
thing(BaseType)?,
otherThing(BaseType)?
}
{
BaseType:#
......