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

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: More changes 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
« no previous file with comments | « tools/json_schema_compiler/cpp_util.py ('k') | tools/json_schema_compiler/model.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.GetCompiledType(prop, wrap_optional=True),
163 prop.unix_name)) 163 prop.unix_name))
164 .Append() 164 .Append()
165 ) 165 )
166 return c 166 return c
167 167
168 def _GenerateType(self, type_): 168 def _GenerateType(self, type_):
169 """Generates a struct for a type. 169 """Generates a struct for a type.
170 """ 170 """
171 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) 171 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name))
172 c = Code() 172 c = Code()
173 173
174 if type_.functions: 174 if type_.functions:
175 c.Sblock('namespace %(classname)s {') 175 c.Sblock('namespace %(classname)s {')
176 for function in type_.functions.values(): 176 for function in type_.functions.values():
177 (c.Concat(self._GenerateFunction(function)) 177 (c.Concat(self._GenerateFunction(function))
178 .Append() 178 .Append()
179 ) 179 )
180 c.Eblock('}') 180 c.Eblock('}')
181 elif type_.type_ == PropertyType.ARRAY: 181 elif type_.type_ == PropertyType.ARRAY:
182 if type_.description: 182 if type_.description:
183 c.Comment(type_.description) 183 c.Comment(type_.description)
184 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') 184 c.Append('typedef std::vector<%(item_type)s> %(classname)s;')
185 c.Substitute({'classname': classname, 'item_type': 185 c.Substitute({'classname': classname, 'item_type':
186 self._cpp_type_generator.GetType(type_.item_type, 186 self._cpp_type_generator.GetCompiledType(type_.item_type,
187 wrap_optional=True)}) 187 wrap_optional=True)})
188 elif type_.type_ == PropertyType.STRING: 188 elif type_.type_ == PropertyType.STRING:
189 if type_.description: 189 if type_.description:
190 c.Comment(type_.description) 190 c.Comment(type_.description)
191 c.Append('typedef std::string %(classname)s;') 191 c.Append('typedef std::string %(classname)s;')
192 else: 192 else:
193 if type_.description: 193 if type_.description:
194 c.Comment(type_.description) 194 c.Comment(type_.description)
195 (c.Sblock('struct %(classname)s {') 195 (c.Sblock('struct %(classname)s {')
196 .Append('~%(classname)s();') 196 .Append('~%(classname)s();')
197 .Append('%(classname)s();') 197 .Append('%(classname)s();')
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 elif prop.type_ == PropertyType.ARRAY: 287 elif prop.type_ == PropertyType.ARRAY:
288 c.Concat(self._GeneratePropertyStructures([prop.item_type])) 288 c.Concat(self._GeneratePropertyStructures([prop.item_type]))
289 c.Append() 289 c.Append()
290 elif prop.type_ == PropertyType.CHOICES: 290 elif prop.type_ == PropertyType.CHOICES:
291 c.Concat(self._GenerateEnumDeclaration( 291 c.Concat(self._GenerateEnumDeclaration(
292 self._cpp_type_generator.GetChoicesEnumType(prop), 292 self._cpp_type_generator.GetChoicesEnumType(prop),
293 prop, 293 prop,
294 [choice.type_.name for choice in prop.choices.values()])) 294 [choice.type_.name for choice in prop.choices.values()]))
295 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 295 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
296 elif prop.type_ == PropertyType.ENUM: 296 elif prop.type_ == PropertyType.ENUM:
297 enum_name = self._cpp_type_generator.GetType(prop) 297 enum_name = self._cpp_type_generator.GetCompiledType(prop)
298 c.Concat(self._GenerateEnumDeclaration( 298 c.Concat(self._GenerateEnumDeclaration(
299 enum_name, 299 enum_name,
300 prop, 300 prop,
301 prop.enum_values)) 301 prop.enum_values))
302 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % 302 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
303 (enum_name, prop.unix_name)) 303 (enum_name, prop.unix_name))
304 # 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
305 # 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
306 # namespace so we can't have the static keyword. 306 # namespace so we can't have the static keyword.
307 if prop.from_json: 307 if prop.from_json:
(...skipping 24 matching lines...) Expand all
332 params = function.params 332 params = function.params
333 c.Concat(self._GeneratePropertyStructures(params)) 333 c.Concat(self._GeneratePropertyStructures(params))
334 334
335 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) 335 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
336 for param_list in param_lists: 336 for param_list in param_lists:
337 declaration_list = [] 337 declaration_list = []
338 for param in param_list: 338 for param in param_list:
339 if param.description: 339 if param.description:
340 c.Comment(param.description) 340 c.Comment(param.description)
341 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( 341 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration(
342 param, self._cpp_type_generator.GetType(param))) 342 param, self._cpp_type_generator.GetCompiledType(param)))
343 c.Append('scoped_ptr<base::ListValue> Create(%s);' % 343 c.Append('scoped_ptr<base::ListValue> Create(%s);' %
344 ', '.join(declaration_list)) 344 ', '.join(declaration_list))
345 if generate_to_json: 345 if generate_to_json:
346 c.Append('std::string ToJson(%s);' % ', '.join(declaration_list)) 346 c.Append('std::string ToJson(%s);' % ', '.join(declaration_list))
347 return c 347 return c
348 348
349 def _GenerateFunctionResults(self, callback): 349 def _GenerateFunctionResults(self, callback):
350 """Generates namespace for passing a function's result back. 350 """Generates namespace for passing a function's result back.
351 """ 351 """
352 c = Code() 352 c = Code()
353 (c.Sblock('namespace Results {') 353 (c.Sblock('namespace Results {')
354 .Concat(self._GenerateCreateCallbackArguments(callback)) 354 .Concat(self._GenerateCreateCallbackArguments(callback))
355 .Eblock('};') 355 .Eblock('};')
356 ) 356 )
357 return c 357 return c
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cpp_util.py ('k') | tools/json_schema_compiler/model.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698