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

Side by Side Diff: tools/json_schema_compiler/h_generator.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 from code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import cpp_util 7 import cpp_util
8 import schema_util 8 import schema_util
9 9
10 class HGenerator(object): 10 class HGenerator(object):
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 for prop in props: 152 for prop in props:
153 if prop.type_ == PropertyType.CHOICES: 153 if prop.type_ == PropertyType.CHOICES:
154 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) 154 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop)
155 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) 155 c.Append('%s %s_type;' % (enum_name, prop.unix_name))
156 c.Append() 156 c.Append()
157 157
158 for prop in self._cpp_type_generator.ExpandParams(props): 158 for prop in self._cpp_type_generator.ExpandParams(props):
159 if prop.description: 159 if prop.description:
160 c.Comment(prop.description) 160 c.Comment(prop.description)
161 (c.Append('%s %s;' % ( 161 (c.Append('%s %s;' % (
162 self._cpp_type_generator.GetType(prop, wrap_optional=True), 162 self._cpp_type_generator.GetType(prop, wrap_optional=True,
163 prop.unix_name)) 163 convert_type=True),
164 prop.unix_name))
164 .Append() 165 .Append()
165 ) 166 )
166 return c 167 return c
167 168
168 def _GenerateType(self, type_): 169 def _GenerateType(self, type_):
169 """Generates a struct for a type. 170 """Generates a struct for a type.
170 """ 171 """
171 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) 172 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name))
172 c = Code() 173 c = Code()
173 174
174 if type_.functions: 175 if type_.functions:
175 c.Sblock('namespace %(classname)s {') 176 c.Sblock('namespace %(classname)s {')
176 for function in type_.functions.values(): 177 for function in type_.functions.values():
177 (c.Concat(self._GenerateFunction(function)) 178 (c.Concat(self._GenerateFunction(function))
178 .Append() 179 .Append()
179 ) 180 )
180 c.Eblock('}') 181 c.Eblock('}')
181 elif type_.type_ == PropertyType.ARRAY: 182 elif type_.type_ == PropertyType.ARRAY:
182 if type_.description: 183 if type_.description:
183 c.Comment(type_.description) 184 c.Comment(type_.description)
184 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') 185 c.Append('typedef std::vector<%(item_type)s> %(classname)s;')
185 c.Substitute({'classname': classname, 'item_type': 186 c.Substitute({'classname': classname, 'item_type':
186 self._cpp_type_generator.GetType(type_.item_type, 187 self._cpp_type_generator.GetType(type_.item_type,
187 wrap_optional=True)}) 188 wrap_optional=True,
189 convert_type=True)})
188 elif type_.type_ == PropertyType.STRING: 190 elif type_.type_ == PropertyType.STRING:
189 if type_.description: 191 if type_.description:
190 c.Comment(type_.description) 192 c.Comment(type_.description)
191 c.Append('typedef std::string %(classname)s;') 193 c.Append('typedef std::string %(classname)s;')
192 else: 194 else:
193 if type_.description: 195 if type_.description:
194 c.Comment(type_.description) 196 c.Comment(type_.description)
195 (c.Sblock('struct %(classname)s {') 197 (c.Sblock('struct %(classname)s {')
196 .Append('~%(classname)s();') 198 .Append('~%(classname)s();')
197 .Append('%(classname)s();') 199 .Append('%(classname)s();')
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 elif prop.type_ == PropertyType.ARRAY: 288 elif prop.type_ == PropertyType.ARRAY:
287 c.Concat(self._GeneratePropertyStructures([prop.item_type])) 289 c.Concat(self._GeneratePropertyStructures([prop.item_type]))
288 c.Append() 290 c.Append()
289 elif prop.type_ == PropertyType.CHOICES: 291 elif prop.type_ == PropertyType.CHOICES:
290 c.Concat(self._GenerateEnumDeclaration( 292 c.Concat(self._GenerateEnumDeclaration(
291 self._cpp_type_generator.GetChoicesEnumType(prop), 293 self._cpp_type_generator.GetChoicesEnumType(prop),
292 prop, 294 prop,
293 [choice.type_.name for choice in prop.choices.values()])) 295 [choice.type_.name for choice in prop.choices.values()]))
294 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 296 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
295 elif prop.type_ == PropertyType.ENUM: 297 elif prop.type_ == PropertyType.ENUM:
296 enum_name = self._cpp_type_generator.GetType(prop) 298 enum_name = self._cpp_type_generator.GetType(prop, convert_type=True)
297 c.Concat(self._GenerateEnumDeclaration( 299 c.Concat(self._GenerateEnumDeclaration(
298 enum_name, 300 enum_name,
299 prop, 301 prop,
300 prop.enum_values)) 302 prop.enum_values))
301 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % 303 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
302 (enum_name, prop.unix_name)) 304 (enum_name, prop.unix_name))
303 # If the property is from the UI then we're in a struct so this function 305 # If the property is from the UI then we're in a struct so this function
304 # should be static. If it's from the client, then we're just in a 306 # should be static. If it's from the client, then we're just in a
305 # namespace so we can't have the static keyword. 307 # namespace so we can't have the static keyword.
306 if prop.from_json: 308 if prop.from_json:
(...skipping 24 matching lines...) Expand all
331 params = function.params 333 params = function.params
332 c.Concat(self._GeneratePropertyStructures(params)) 334 c.Concat(self._GeneratePropertyStructures(params))
333 335
334 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) 336 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
335 for param_list in param_lists: 337 for param_list in param_lists:
336 declaration_list = [] 338 declaration_list = []
337 for param in param_list: 339 for param in param_list:
338 if param.description: 340 if param.description:
339 c.Comment(param.description) 341 c.Comment(param.description)
340 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( 342 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration(
341 param, self._cpp_type_generator.GetType(param))) 343 param, self._cpp_type_generator.GetType(param, convert_type=True)))
342 c.Append('scoped_ptr<base::ListValue> Create(%s);' % 344 c.Append('scoped_ptr<base::ListValue> Create(%s);' %
343 ', '.join(declaration_list)) 345 ', '.join(declaration_list))
344 return c 346 return c
345 347
346 def _GenerateFunctionResults(self, callback): 348 def _GenerateFunctionResults(self, callback):
347 """Generates namespace for passing a function's result back. 349 """Generates namespace for passing a function's result back.
348 """ 350 """
349 c = Code() 351 c = Code()
350 (c.Sblock('namespace Results {') 352 (c.Sblock('namespace Results {')
351 .Concat(self._GenerateCreateCallbackArguments(callback)) 353 .Concat(self._GenerateCreateCallbackArguments(callback))
352 .Eblock('};') 354 .Eblock('};')
353 ) 355 )
354 return c 356 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698