Skip to content

Commit

Permalink
Merge pull request #70 from cps-org/fix-definitions-schema
Browse files Browse the repository at this point in the history
Fix type of "definitions" attribute
  • Loading branch information
mwoehlke authored Dec 11, 2024
2 parents fbc7924 + d1365ee commit 8fcd328
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
25 changes: 16 additions & 9 deletions _extensions/cps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,22 @@ def make_list(self, values, nodetype, classes):

# -------------------------------------------------------------------------
def parse_type(self, typedesc):
if '|' in typedesc:
types = typedesc.split('|')
content = self.parse_type(types[0])
for t in types[1:]:
content += [
nodes.Text(' '),
nodes.inline('or', 'or', classes=['separator']),
nodes.Text(' '),
] + self.parse_type(t)
types = jsb.split_typedesc(typedesc)
if len(types) > 1:
if len(types) == 2 and 'null' in types:
types.remove('null')
content = [
nodes.Text('(nullable) '),
] + self.parse_type(types[0])

else:
content = self.parse_type(types[0])
for t in types[1:]:
content += [
nodes.Text(' '),
nodes.inline('or', 'or', classes=['separator']),
nodes.Text(' '),
] + self.parse_type(t)

return content

Expand Down
26 changes: 24 additions & 2 deletions _packages/jsb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ def decompose_typedesc(typedesc):
m = re.match(r'^(list|map)[(](.*)[)]$', typedesc)
return m.groups() if m else (None, typedesc)

# =============================================================================
def split_typedesc(typedesc):
assert typedesc.count('(') == typedesc.count(')')

types = []
start = 0
depth = 0
for n in range(len(typedesc)):
if typedesc[n] == '(':
depth += 1
elif typedesc[n] == ')':
assert depth > 0
depth -= 1
elif depth == 0 and typedesc[n] == '|':
types.append(typedesc[start:n])
start = n + 1

types.append(typedesc[start:])

print(f'split typedesc {typedesc!r} => {types!r}')
return types

# =============================================================================
class JsonSchema:
# -------------------------------------------------------------------------
Expand All @@ -29,8 +51,8 @@ def add_type(self, typedesc):
# Type already defined; nothing to do
return

if '|' in typedesc:
types = typedesc.split('|')
types = split_typedesc(typedesc)
if len(types) > 1:
for t in types:
self.add_type(t)

Expand Down
2 changes: 1 addition & 1 deletion schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Attribute names are case sensitive.

.. ----------------------------------------------------------------------------
.. cps:attribute:: definitions
:type: map(map(string))
:type: map(map(string|null))
:context: component configuration

Specifies a collection of compile definitions that must be defined
Expand Down

0 comments on commit 8fcd328

Please sign in to comment.