Skip to content

Commit bb59f88

Browse files
committed
Added support for catch all symbols in unions.
1 parent db8d78d commit bb59f88

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

babelapi/babel/tower.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,30 @@ def _create_alias(self, env, item):
120120
def _create_type(self, env, item):
121121
super_type = None
122122
if item.composite_type == 'struct':
123-
composite_type_obj = Struct
124123
if item.extends:
125124
if item.extends not in env:
126125
raise Exception('Data type %r is undefined' % item.extends)
127126
else:
128127
super_type = env.get(item.extends)
128+
api_type_fields = []
129+
for babel_field in item.fields:
130+
api_type_field = self._create_field(env, babel_field)
131+
api_type_fields.append(api_type_field)
132+
api_type = Struct(item.name, item.doc, api_type_fields, super_type)
129133
elif item.composite_type == 'union':
130-
composite_type_obj = Union
134+
catch_all_field = None
135+
api_type_fields = []
136+
for babel_field in item.fields:
137+
if isinstance(babel_field, BabelCatchAllSymbol):
138+
catch_all_field = babel_field.name
139+
else:
140+
api_type_field = self._create_field(env, babel_field)
141+
api_type_fields.append(api_type_field)
142+
api_type = Union(item.name, item.doc, api_type_fields, super_type,
143+
catch_all_field)
131144
else:
132145
raise ValueError('Unknown composite_type %r'
133146
% item.composite_type)
134-
api_type_fields = []
135-
for babel_field in item.fields:
136-
if isinstance(babel_field, BabelCatchAllSymbol):
137-
pass
138-
else:
139-
api_type_field = self._create_field(env, babel_field)
140-
api_type_fields.append(api_type_field)
141-
api_type = composite_type_obj(item.name, item.doc, api_type_fields, super_type)
142147
for example_label, (example_text, example) in item.examples.items():
143148
api_type.add_example(example_label, example_text, dict(example))
144149
env[item.name] = api_type

babelapi/data_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ class Union(CompositeType):
505505

506506
composite_type = 'union'
507507

508-
def __init__(self, name, doc, fields, super_type=None, open_field=None):
509-
super(CompositeType, self).__init__(name, doc, fields, super_type=super_type)
510-
self.open_field = open_field
508+
def __init__(self, name, doc, fields, super_type=None, catch_all_field=None):
509+
super(Union, self).__init__(name, doc, fields, super_type=super_type)
510+
self.catch_all_field = catch_all_field
511511

512512
def check(self, val):
513513
if isinstance(val, dict):

example/api/dbx-core/files.babel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct SubError
1717
union DownloadError
1818
disallowed SubError
1919
no_file SubError
20+
*unknown
2021

2122
route Download (FileTarget, FileInfo, DownloadError)
2223
"Download a file in a user's Dropbox."
@@ -98,7 +99,6 @@ union ConflictReason
9899
"Conflict with a file."
99100
autorename_failed
100101
"Could not autorename."
101-
*unknown
102102

103103
struct ConflictError
104104
reason ConflictReason

example/generator/dropbox-python-sdk/base_files.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -492,31 +492,26 @@ class ConflictReason(object):
492492
Folder = object()
493493
File = object()
494494
AutorenameFailed = object()
495-
Unknown = object()
496495

497496
def __init__(self,
498497
folder=None,
499498
file=None,
500499
autorename_failed=None,
501-
unknown=None,
502500
**kwargs):
503501
"""
504502
Only one argument can be set.
505503
506504
:param bool folder: Conflict with a folder.
507505
:param bool file: Conflict with a file.
508506
:param bool autorename_failed: Could not autorename.
509-
:type unknown: bool
510507
"""
511508
assert_only_one(folder=folder,
512509
file=file,
513510
autorename_failed=autorename_failed,
514-
unknown=unknown,
515511
**kwargs)
516512
self.folder = None
517513
self.file = None
518514
self.autorename_failed = None
519-
self.unknown = None
520515

521516
if folder is not None:
522517
assert isinstance(folder, bool), 'folder must be of type bool'
@@ -533,11 +528,6 @@ def __init__(self,
533528
self.autorename_failed = autorename_failed
534529
self._tag = 'autorename_failed'
535530

536-
if unknown is not None:
537-
assert isinstance(unknown, bool), 'unknown must be of type bool'
538-
self.unknown = unknown
539-
self._tag = 'unknown'
540-
541531
def is_folder(self):
542532
return self._tag == 'folder'
543533

@@ -547,9 +537,6 @@ def is_file(self):
547537
def is_autorename_failed(self):
548538
return self._tag == 'autorename_failed'
549539

550-
def is_unknown(self):
551-
return self._tag == 'unknown'
552-
553540
@classmethod
554541
def from_json(self, obj):
555542
obj = copy.copy(obj)
@@ -560,8 +547,6 @@ def from_json(self, obj):
560547
return obj
561548
if obj == 'autorename_failed':
562549
return obj
563-
if obj == 'unknown':
564-
return obj
565550
return ConflictReason(**obj)
566551

567552
def to_json(self):
@@ -571,8 +556,6 @@ def to_json(self):
571556
return self._tag
572557
if self._tag == 'autorename_failed':
573558
return self._tag
574-
if self._tag == 'unknown':
575-
return self._tag
576559

577560
def __repr__(self):
578561
return 'ConflictReason(%r)' % self._tag
@@ -591,8 +574,6 @@ def __init__(self,
591574
self.reason = ConflictReason(file=True)
592575
if reason == ConflictReason.AutorenameFailed:
593576
self.reason = ConflictReason(autorename_failed=True)
594-
if reason == ConflictReason.Unknown:
595-
self.reason = ConflictReason(unknown=True)
596577

597578
@classmethod
598579
def from_json(cls, obj):

test/test_babel.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
BabelAlias,
66
BabelField,
77
BabelParser,
8+
BabelCatchAllSymbol,
89
BabelSymbol,
910
BabelTypeDef,
1011
)
@@ -165,6 +166,19 @@ def test_union_decl(self):
165166

166167
# TODO: Test a union that includes a struct.
167168

169+
text = """
170+
namespace files
171+
172+
union Error
173+
A
174+
"Variant A"
175+
B
176+
"Variant B"
177+
*UNK
178+
"""
179+
out = self.parser.parse(text)
180+
self.assertTrue(isinstance(out[1].fields[2], BabelCatchAllSymbol))
181+
168182
def test_route_decl(self):
169183
text = """
170184
namespace users

0 commit comments

Comments
 (0)