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

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

Powered by Google App Engine
This is Rietveld 408576698