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

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

Issue 10790040: JSON Schema Compiler now supports conversion from Choice to base::Value. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Trying a refactor of GetChoiceValue and CreateEnumValue 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 17 matching lines...) Expand all
28 28
29 ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir, 29 ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir,
30 self._target_namespace) 30 self._target_namespace)
31 (c.Append('#ifndef %s' % ifndef_name) 31 (c.Append('#ifndef %s' % ifndef_name)
32 .Append('#define %s' % ifndef_name) 32 .Append('#define %s' % ifndef_name)
33 .Append() 33 .Append()
34 .Append('#include <string>') 34 .Append('#include <string>')
35 .Append('#include <vector>') 35 .Append('#include <vector>')
36 .Append() 36 .Append()
37 .Append('#include "base/basictypes.h"') 37 .Append('#include "base/basictypes.h"')
38 .Append('#include "base/logging.h"')
38 .Append('#include "base/memory/linked_ptr.h"') 39 .Append('#include "base/memory/linked_ptr.h"')
39 .Append('#include "base/memory/scoped_ptr.h"') 40 .Append('#include "base/memory/scoped_ptr.h"')
40 .Append('#include "base/values.h"') 41 .Append('#include "base/values.h"')
41 .Append('#include "tools/json_schema_compiler/any.h"') 42 .Append('#include "tools/json_schema_compiler/any.h"')
42 .Append() 43 .Append()
43 ) 44 )
44 45
45 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) 46 c.Concat(self._cpp_type_generator.GetRootNamespaceStart())
46 # TODO(calamity): These forward declarations should be #includes to allow 47 # TODO(calamity): These forward declarations should be #includes to allow
47 # $ref types from other files to be used as required params. This requires 48 # $ref types from other files to be used as required params. This requires
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 208
208 if type_.from_client: 209 if type_.from_client:
209 (c.Comment('Returns a new base::DictionaryValue representing the' 210 (c.Comment('Returns a new base::DictionaryValue representing the'
210 ' serialized form of this %s object. Passes ' 211 ' serialized form of this %s object. Passes '
211 'ownership to caller.' % classname) 212 'ownership to caller.' % classname)
212 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;') 213 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;')
213 ) 214 )
214 215
215 (c.Eblock() 216 (c.Eblock()
216 .Sblock(' private:') 217 .Sblock(' private:')
218 .Concat(self._GeneratePrivatePropertyStructures(
219 type_.properties.values()))
220 .Append()
217 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 221 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
218 .Eblock('};') 222 .Eblock('};')
219 ) 223 )
220 c.Substitute({'classname': classname}) 224 c.Substitute({'classname': classname})
221 return c 225 return c
222 226
223 def _GenerateEvent(self, event): 227 def _GenerateEvent(self, event):
224 """Generates the namespaces for an event. 228 """Generates the namespaces for an event.
225 """ 229 """
226 c = Code() 230 c = Code()
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % 300 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
297 (enum_name, prop.unix_name)) 301 (enum_name, prop.unix_name))
298 # If the property is from the UI then we're in a struct so this function 302 # If the property is from the UI then we're in a struct so this function
299 # should be static. If it's from the client, then we're just in a 303 # should be static. If it's from the client, then we're just in a
300 # namespace so we can't have the static keyword. 304 # namespace so we can't have the static keyword.
301 if prop.from_json: 305 if prop.from_json:
302 create_enum_value = 'static ' + create_enum_value 306 create_enum_value = 'static ' + create_enum_value
303 c.Append(create_enum_value) 307 c.Append(create_enum_value)
304 return c 308 return c
305 309
310 def _GeneratePrivatePropertyStructures(self, props):
311 """Generate the private structures required by a property such as OBJECT
312 classes and enums.
313 """
314 c = Code()
315 for prop in props:
316 if prop.type_ == PropertyType.ARRAY:
317 c.Concat(self._GeneratePrivatePropertyStructures([prop.item_type]))
318 c.Append()
319 elif prop.type_ == PropertyType.CHOICES:
320 # We only need GetChoiceValue() if there is a ToValue() method.
321 if prop.from_client:
322 c.Append('scoped_ptr<base::Value> GetChoiceValue(%s) const;' %
not at google - send to devlin 2012/07/19 23:59:53 static?
323 cpp_util.GetParameterDeclaration(prop,
324 self._cpp_type_generator.GetChoicesEnumType(prop)))
325 return c
326
306 def _GenerateCreateCallbackArguments(self, function): 327 def _GenerateCreateCallbackArguments(self, function):
307 """Generates functions for passing paramaters to a callback. 328 """Generates functions for passing paramaters to a callback.
308 """ 329 """
309 c = Code() 330 c = Code()
310 params = function.params 331 params = function.params
311 c.Concat(self._GeneratePropertyStructures(params)) 332 c.Concat(self._GeneratePropertyStructures(params))
312 333
313 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) 334 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
314 for param_list in param_lists: 335 for param_list in param_lists:
315 declaration_list = [] 336 declaration_list = []
316 for param in param_list: 337 for param in param_list:
317 if param.description: 338 if param.description:
318 c.Comment(param.description) 339 c.Comment(param.description)
319 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( 340 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration(
320 param, self._cpp_type_generator.GetType(param))) 341 param, self._cpp_type_generator.GetType(param)))
321 c.Append('scoped_ptr<base::ListValue> Create(%s);' % 342 c.Append('scoped_ptr<base::ListValue> Create(%s);' %
322 ', '.join(declaration_list)) 343 ', '.join(declaration_list))
323 return c 344 return c
324 345
325 def _GenerateFunctionResults(self, callback): 346 def _GenerateFunctionResults(self, callback):
326 """Generates namespace for passing a function's result back. 347 """Generates namespace for passing a function's result back.
327 """ 348 """
328 c = Code() 349 c = Code()
329 (c.Sblock('namespace Results {') 350 (c.Sblock('namespace Results {')
330 .Concat(self._GenerateCreateCallbackArguments(callback)) 351 .Concat(self._GenerateCreateCallbackArguments(callback))
331 .Eblock('};') 352 .Eblock('};')
332 ) 353 )
333 return c 354 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698