Index: tools/json_schema_compiler/model.py |
diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py |
index 2351ecd1f999b17bf7077c52d8baaeb00e09cd83..84992b4cdf278289c0cc57b956a66916846ef326 100644 |
--- a/tools/json_schema_compiler/model.py |
+++ b/tools/json_schema_compiler/model.py |
@@ -150,6 +150,8 @@ class Property(object): |
- |optional| a boolean representing whether the property is optional |
- |description| a description of the property (if provided) |
- |type_| the model.PropertyType of this property |
+ - |compiled_type| the model.PropertyType that this property should be |
+ compiled to from the JSON. Defaults to type_. |
- |ref_type| the type that the REF property is referencing. Can be used to |
map to its model.Type |
- |item_type| a model.Property representing the type of each element in an |
@@ -178,9 +180,11 @@ class Property(object): |
_AddProperties(self, json) |
if is_additional_properties: |
self.type_ = PropertyType.ADDITIONAL_PROPERTIES |
+ self.compiled_type = self.type_ |
not at google - send to devlin
2012/07/31 06:37:41
a lot of repetitive assigning to compiled_type. Co
mitchellwrosen
2012/07/31 17:50:25
Okay. I was originally thinking that a compiled_ty
|
elif '$ref' in json: |
self.ref_type = json['$ref'] |
self.type_ = PropertyType.REF |
+ self.compiled_type = self.type_ |
elif 'enum' in json and json.get('type') == 'string': |
# Non-string enums (as in the case of [legalValues=(1,2)]) should fall |
# through to the next elif. |
@@ -188,41 +192,29 @@ class Property(object): |
for value in json['enum']: |
self.enum_values.append(value) |
self.type_ = PropertyType.ENUM |
+ self.compiled_type = self.type_ |
elif 'type' in json: |
- json_type = json['type'] |
- if json_type == 'string': |
- self.type_ = PropertyType.STRING |
- elif json_type == 'any': |
- self.type_ = PropertyType.ANY |
- elif json_type == 'boolean': |
- self.type_ = PropertyType.BOOLEAN |
- elif json_type == 'integer': |
- self.type_ = PropertyType.INTEGER |
- elif json_type == 'number': |
- self.type_ = PropertyType.DOUBLE |
- elif json_type == 'array': |
+ self.type_ = self._JsonTypeToPropertyType(json['type']) |
+ if self.type_ == PropertyType.ARRAY: |
self.item_type = Property(self, name + "Element", json['items'], |
from_json=from_json, |
from_client=from_client) |
- self.type_ = PropertyType.ARRAY |
- elif json_type == 'object': |
- self.type_ = PropertyType.OBJECT |
+ elif self.type_ == PropertyType.OBJECT: |
# These members are read when this OBJECT Property is used as a Type |
type_ = Type(self, self.name, json) |
# self.properties will already have some value from |_AddProperties|. |
self.properties.update(type_.properties) |
self.functions = type_.functions |
- elif json_type == 'function': |
- self.type_ = PropertyType.FUNCTION |
- elif json_type == 'binary': |
- self.type_ = PropertyType.BINARY |
+ if 'compiled_type' in json: |
+ self.compiled_type = self._JsonTypeToPropertyType(json['compiled_type']) |
else: |
- raise ParseException(self, 'type ' + json_type + ' not recognized') |
+ self.compiled_type = self.type_ |
elif 'choices' in json: |
if not json['choices'] or len(json['choices']) == 0: |
raise ParseException(self, 'Choices has no choices') |
self.choices = {} |
self.type_ = PropertyType.CHOICES |
+ self.compiled_type = self.type_ |
for choice_json in json['choices']: |
choice = Property(self, self.name, choice_json, |
from_json=from_json, |
@@ -236,6 +228,7 @@ class Property(object): |
self.value = json['value'] |
if type(self.value) == int: |
self.type_ = PropertyType.INTEGER |
+ self.compiled_type = self.type_ |
else: |
# TODO(kalman): support more types as necessary. |
raise ParseException( |
@@ -244,6 +237,29 @@ class Property(object): |
raise ParseException( |
self, 'Property has no type, $ref, choices, or value') |
+ def _JsonTypeToPropertyType(self, json_type): |
+ if json_type == 'any': |
+ return PropertyType.ANY |
+ if json_type == 'array': |
+ return PropertyType.ARRAY |
+ if json_type == 'binary': |
+ return PropertyType.BINARY |
+ if json_type == 'boolean': |
+ return PropertyType.BOOLEAN |
+ if json_type == 'integer': |
+ return PropertyType.INTEGER |
+ if json_type == 'int64': |
+ return PropertyType.INT_64 |
+ if json_type == 'function': |
+ return PropertyType.FUNCTION |
+ if json_type == 'number': |
+ return PropertyType.DOUBLE |
+ if json_type == 'object': |
+ return PropertyType.OBJECT |
+ if json_type == 'string': |
+ return PropertyType.STRING |
+ raise NotImplementedError('Type %s not recognized' % json_type) |
not at google - send to devlin
2012/07/31 06:37:41
you could do the old
try:
return {
'any': P
mitchellwrosen
2012/07/31 17:50:25
Yes. My never-written-python-before-in-my-life is
|
+ |
def GetUnixName(self): |
"""Gets the property's unix_name. Raises AttributeError if not set. |
""" |
@@ -287,6 +303,7 @@ class PropertyType(object): |
return self.name |
INTEGER = _Info(True, "INTEGER") |
+ INT_64 = _Info(True, "INT_64") |
not at google - send to devlin
2012/07/31 06:37:41
the c++ type is int64 not int_64 so this should be
mitchellwrosen
2012/07/31 17:50:25
Sure, I actually changed it from INT64 to INT_64 b
|
DOUBLE = _Info(True, "DOUBLE") |
BOOLEAN = _Info(True, "BOOLEAN") |
STRING = _Info(True, "STRING") |