Chromium Code Reviews| Index: tools/json_schema_compiler/model.py |
| diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py |
| index 5ff394af46c471d16ad770a8261580091b1e2ac9..70946f3a0819bae773bb1c7f2cee01ef52a7df4b 100644 |
| --- a/tools/json_schema_compiler/model.py |
| +++ b/tools/json_schema_compiler/model.py |
| @@ -60,13 +60,22 @@ class Type(object): |
| - |name| the type name |
| - |description| the description of the type (if provided) |
| - |properties| a map of property names to their model.Property |
| + - |from_client| indicates that instances of the Type can originate from the |
| + users of generated code, such as top-level types and function results |
| + - |from_json| indicates that instances of the Type can originate from the |
| + JSON (as described by the schema), such as top-level types and function |
| + parameters |
| """ |
| def __init__(self, json): |
| self.name = json['id'] |
| self.description = json.get('description') |
| + self.from_client = True |
| + self.from_json = True |
| self.properties = {} |
| for prop_name, prop_json in json['properties'].items(): |
| - self.properties[prop_name] = Property(prop_name, prop_json) |
| + self.properties[prop_name] = Property(prop_name, prop_json, |
| + from_client=True, |
| + from_json=True) |
| class Callback(object): |
| """A callback parameter to a Function. |
| @@ -81,7 +90,8 @@ class Callback(object): |
| return |
| elif len(params) == 1: |
| param = params[0] |
| - self.params.append(Property(param['name'], param)) |
| + self.params.append(Property(param['name'], param, |
| + from_json=True)) |
| else: |
| raise AssertionError("Callbacks can have at most a single parameter") |
| @@ -106,12 +116,18 @@ class Function(object): |
| assert (not self.callback), self.name + " has more than one callback" |
| self.callback = Callback(param) |
| else: |
| - self.params.append(Property(param['name'], param)) |
| - assert (self.callback), self.name + " does not support callback" |
| + self.params.append(Property(param['name'], param, |
| + from_client=True)) |
| class Property(object): |
| """A property of a type OR a parameter to a function. |
| + Parameters: |
| + from_client: indicates that instances of the Type can originate from the |
| + users of generated code, such as top-level types and function results |
| + from_json: indicates that instances of the Type can originate from the JSON |
| + (as described by the schema), such as top-level types and function parameters |
|
not at google - send to devlin
2012/02/27 06:04:47
need to keep commenting style consistent... elsewh
calamity
2012/02/28 01:01:45
Done.
|
| + |
| Properties: |
| - |name| name of the property as in the json. This shouldn't change since |
| it is the key used to access DictionaryValues |
| @@ -125,9 +141,9 @@ class Property(object): |
| ARRAY |
| - |properties| the properties of an OBJECT parameter |
| """ |
| - def __init__(self, name, json): |
| - if not re.match('^[a-z][a-zA-Z0-9]*$', name): |
| - raise AssertionError('Name %s must be lowerCamelCase' % name) |
| + def __init__(self, name, json, |
| + from_client=False, |
| + from_json=False): |
| self.name = name |
| self._unix_name = _UnixName(self.name) |
| self._unix_name_used = False |
| @@ -154,13 +170,20 @@ class Property(object): |
| elif json_type == 'number': |
| self.type_ = PropertyType.DOUBLE |
| elif json_type == 'array': |
| - self.item_type = Property(name + "Element", json['items']) |
| + self.item_type = Property(name + "Element", json['items'], |
| + from_client, |
| + from_json) |
| self.type_ = PropertyType.ARRAY |
| elif json_type == 'object': |
| - self.properties = {} |
| self.type_ = PropertyType.OBJECT |
| - for key, val in json['properties'].items(): |
| - self.properties[key] = Property(key, val) |
| + # These members are read when this OBJECT Property is used as a Type |
| + self.properties = {} |
| + self.from_client = from_client |
| + self.from_json = from_json |
| + for key, val in json.get('properties', {}).items(): |
| + self.properties[key] = Property(key, val, |
| + from_client, |
| + from_json) |
| else: |
| raise NotImplementedError(json_type) |
| elif 'choices' in json: |
| @@ -168,7 +191,9 @@ class Property(object): |
| self.choices = {} |
| self.type_ = PropertyType.CHOICES |
| for choice_json in json['choices']: |
| - choice = Property(self.name, choice_json) |
| + choice = Property(self.name, choice_json, |
| + from_client, |
| + from_json) |
| # A choice gets its unix_name set in |
| # cpp_type_generator.GetExpandedChoicesInParams |
| choice._unix_name = None |