Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: tools/json_schema_compiler/model.py

Issue 10825029: Added JSON schema compiler support for serialized types (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import copy 5 import copy
6 import os.path 6 import os.path
7 import re 7 import re
8 8
9 class ParseException(Exception): 9 class ParseException(Exception):
10 """Thrown when data in the model is invalid. 10 """Thrown when data in the model is invalid.
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 class Property(object): 143 class Property(object):
144 """A property of a type OR a parameter to a function. 144 """A property of a type OR a parameter to a function.
145 145
146 Properties: 146 Properties:
147 - |name| name of the property as in the json. This shouldn't change since 147 - |name| name of the property as in the json. This shouldn't change since
148 it is the key used to access DictionaryValues 148 it is the key used to access DictionaryValues
149 - |unix_name| the unix_style_name of the property. Used as variable name 149 - |unix_name| the unix_style_name of the property. Used as variable name
150 - |optional| a boolean representing whether the property is optional 150 - |optional| a boolean representing whether the property is optional
151 - |description| a description of the property (if provided) 151 - |description| a description of the property (if provided)
152 - |type_| the model.PropertyType of this property 152 - |type_| the model.PropertyType of this property
153 - |serialized_type| the model.PropertyType that this property should be
154 serialized to. Will be None if it's missing from the JSON.
153 - |ref_type| the type that the REF property is referencing. Can be used to 155 - |ref_type| the type that the REF property is referencing. Can be used to
154 map to its model.Type 156 map to its model.Type
155 - |item_type| a model.Property representing the type of each element in an 157 - |item_type| a model.Property representing the type of each element in an
156 ARRAY 158 ARRAY
157 - |properties| the properties of an OBJECT parameter 159 - |properties| the properties of an OBJECT parameter
158 - |from_client| indicates that instances of the Type can originate from the 160 - |from_client| indicates that instances of the Type can originate from the
159 users of generated code, such as top-level types and function results 161 users of generated code, such as top-level types and function results
160 - |from_json| indicates that instances of the Type can originate from the 162 - |from_json| indicates that instances of the Type can originate from the
161 JSON (as described by the schema), such as top-level types and function 163 JSON (as described by the schema), such as top-level types and function
162 parameters 164 parameters
163 """ 165 """
164 166
165 def __init__(self, parent, name, json, is_additional_properties=False, 167 def __init__(self, parent, name, json, is_additional_properties=False,
166 from_json=False, from_client=False): 168 from_json=False, from_client=False):
167 self.name = name 169 self.name = name
168 self._unix_name = UnixName(self.name) 170 self._unix_name = UnixName(self.name)
169 self._unix_name_used = False 171 self._unix_name_used = False
170 self.optional = json.get('optional', False) 172 self.optional = json.get('optional', False)
171 self.functions = {} 173 self.functions = {}
172 self.has_value = False 174 self.has_value = False
173 self.description = json.get('description') 175 self.description = json.get('description')
174 self.parent = parent 176 self.parent = parent
175 self.from_json = from_json 177 self.from_json = from_json
176 self.from_client = from_client 178 self.from_client = from_client
177 self.instance_of = json.get('isInstanceOf', None) 179 self.instance_of = json.get('isInstanceOf', None)
180 self.serialized_type = None
178 _AddProperties(self, json) 181 _AddProperties(self, json)
179 if is_additional_properties: 182 if is_additional_properties:
180 self.type_ = PropertyType.ADDITIONAL_PROPERTIES 183 self.type_ = PropertyType.ADDITIONAL_PROPERTIES
181 elif '$ref' in json: 184 elif '$ref' in json:
182 self.ref_type = json['$ref'] 185 self.ref_type = json['$ref']
183 self.type_ = PropertyType.REF 186 self.type_ = PropertyType.REF
184 elif 'enum' in json and json.get('type') == 'string': 187 elif 'enum' in json and json.get('type') == 'string':
185 # Non-string enums (as in the case of [legalValues=(1,2)]) should fall 188 # Non-string enums (as in the case of [legalValues=(1,2)]) should fall
186 # through to the next elif. 189 # through to the next elif.
187 self.enum_values = [] 190 self.enum_values = []
(...skipping 21 matching lines...) Expand all
209 self.type_ = PropertyType.OBJECT 212 self.type_ = PropertyType.OBJECT
210 # These members are read when this OBJECT Property is used as a Type 213 # These members are read when this OBJECT Property is used as a Type
211 type_ = Type(self, self.name, json) 214 type_ = Type(self, self.name, json)
212 # self.properties will already have some value from |_AddProperties|. 215 # self.properties will already have some value from |_AddProperties|.
213 self.properties.update(type_.properties) 216 self.properties.update(type_.properties)
214 self.functions = type_.functions 217 self.functions = type_.functions
215 elif json_type == 'binary': 218 elif json_type == 'binary':
216 self.type_ = PropertyType.BINARY 219 self.type_ = PropertyType.BINARY
217 else: 220 else:
218 raise ParseException(self, 'type ' + json_type + ' not recognized') 221 raise ParseException(self, 'type ' + json_type + ' not recognized')
222
223 if 'serialized_type' in json:
224 json_serialized_type = json['serialized_type']
225 if json_serialized_type == 'integer':
226 self.serialized_type = PropertyType.INTEGER
227 elif json_serialized_type == 'int64':
228 self.serialized_type = PropertyType.INT64
229 else:
not at google - send to devlin 2012/07/26 04:51:42 this might as well be the same code that parses 't
mitchellwrosen 2012/07/26 20:00:27 Hmm. Correct me if I'm wrong but it may not make s
not at google - send to devlin 2012/07/27 04:14:28 Yes, that's true. Hmm. For for the types that onl
mitchellwrosen 2012/07/30 20:52:45 Done.
230 # TODO(mwrosen): Support more types as necessary.
231 raise ParseException(self, 'type ' + json_serialized_type +
232 ' not recognized')
219 elif 'choices' in json: 233 elif 'choices' in json:
220 if not json['choices'] or len(json['choices']) == 0: 234 if not json['choices'] or len(json['choices']) == 0:
221 raise ParseException(self, 'Choices has no choices') 235 raise ParseException(self, 'Choices has no choices')
222 self.choices = {} 236 self.choices = {}
223 self.type_ = PropertyType.CHOICES 237 self.type_ = PropertyType.CHOICES
224 for choice_json in json['choices']: 238 for choice_json in json['choices']:
225 choice = Property(self, self.name, choice_json, 239 choice = Property(self, self.name, choice_json,
226 from_json=from_json, 240 from_json=from_json,
227 from_client=from_client) 241 from_client=from_client)
228 choice.unix_name = UnixName(self.name + choice.type_.name) 242 choice.unix_name = UnixName(self.name + choice.type_.name)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 """ 292 """
279 class _Info(object): 293 class _Info(object):
280 def __init__(self, is_fundamental, name): 294 def __init__(self, is_fundamental, name):
281 self.is_fundamental = is_fundamental 295 self.is_fundamental = is_fundamental
282 self.name = name 296 self.name = name
283 297
284 def __repr__(self): 298 def __repr__(self):
285 return self.name 299 return self.name
286 300
287 INTEGER = _Info(True, "INTEGER") 301 INTEGER = _Info(True, "INTEGER")
302 INT64 = _Info(True, "INT64")
not at google - send to devlin 2012/07/26 04:51:42 ooh maybe we can use this to serialize GURLs direc
mitchellwrosen 2012/07/26 20:00:27 What do you mean?
not at google - send to devlin 2012/07/27 04:14:28 I made this comment first. Just meant what I said
288 DOUBLE = _Info(True, "DOUBLE") 303 DOUBLE = _Info(True, "DOUBLE")
289 BOOLEAN = _Info(True, "BOOLEAN") 304 BOOLEAN = _Info(True, "BOOLEAN")
290 STRING = _Info(True, "STRING") 305 STRING = _Info(True, "STRING")
291 ENUM = _Info(False, "ENUM") 306 ENUM = _Info(False, "ENUM")
292 ARRAY = _Info(False, "ARRAY") 307 ARRAY = _Info(False, "ARRAY")
293 REF = _Info(False, "REF") 308 REF = _Info(False, "REF")
294 CHOICES = _Info(False, "CHOICES") 309 CHOICES = _Info(False, "CHOICES")
295 OBJECT = _Info(False, "OBJECT") 310 OBJECT = _Info(False, "OBJECT")
296 BINARY = _Info(False, "BINARY") 311 BINARY = _Info(False, "BINARY")
297 ANY = _Info(False, "ANY") 312 ANY = _Info(False, "ANY")
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 # handled in pure Javascript on the render process (and .: never reach 374 # handled in pure Javascript on the render process (and .: never reach
360 # C++ let alone the browser). 375 # C++ let alone the browser).
361 if property_json.get('type') == 'function': 376 if property_json.get('type') == 'function':
362 continue 377 continue
363 model.properties[name] = Property( 378 model.properties[name] = Property(
364 model, 379 model,
365 name, 380 name,
366 property_json, 381 property_json,
367 from_json=from_json, 382 from_json=from_json,
368 from_client=from_client) 383 from_client=from_client)
OLDNEW
« tools/json_schema_compiler/cpp_util.py ('K') | « tools/json_schema_compiler/h_generator.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698