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

Side by Side Diff: tools/json_schema_compiler/cc_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: 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 any_helper 7 import any_helper
8 import cpp_util 8 import cpp_util
9 import model 9 import model
10 import schema_util 10 import schema_util
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 .Append() 247 .Append()
248 ) 248 )
249 for prop in type_.properties.values(): 249 for prop in type_.properties.values():
250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) 251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name)
252 else: 252 else:
253 if prop.optional: 253 if prop.optional:
254 if prop.type_ == PropertyType.ENUM: 254 if prop.type_ == PropertyType.ENUM:
255 c.Sblock('if (%s != %s)' % 255 c.Sblock('if (%s != %s)' %
256 (prop.unix_name, 256 (prop.unix_name,
257 self._cpp_type_generator.GetEnumNoneValue(prop))) 257 self._cpp_type_generator.GetEnumNoneValue(prop)))
not at google - send to devlin 2012/07/19 01:48:22 nit: these should vertically line up, so unindent
chebert 2012/07/19 21:06:17 Done.
258 elif prop.type_ == PropertyType.CHOICES:
259 c.Sblock('if (%s_type != %s)' %
260 (prop.unix_name,
261 self._cpp_type_generator.GetEnumNoneValue(prop)))
not at google - send to devlin 2012/07/19 01:48:22 ditto oh it was already like this. incremental cl
chebert 2012/07/19 21:06:17 Done.
258 else: 262 else:
259 c.Sblock('if (%s.get())' % prop.unix_name) 263 c.Sblock('if (%s.get())' % prop.unix_name)
260 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( 264 c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
261 prop.name, 265 prop.name,
262 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) 266 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name)))
263 if prop.optional: 267 if prop.optional:
264 c.Eblock(); 268 c.Eblock();
265 (c.Append() 269 (c.Append()
266 .Append('return value.Pass();') 270 .Append('return value.Pass();')
267 .Eblock('}') 271 .Eblock('}')
(...skipping 18 matching lines...) Expand all
286 290
287 # Results::Create function 291 # Results::Create function
288 if function.callback: 292 if function.callback:
289 c.Concat(self._GenerateCreateCallbackArguments( 293 c.Concat(self._GenerateCreateCallbackArguments(
290 "%s::Results" % cpp_namespace, function.callback)) 294 "%s::Results" % cpp_namespace, function.callback))
291 295
292 c.Substitute({'cpp_namespace': cpp_namespace}) 296 c.Substitute({'cpp_namespace': cpp_namespace})
293 297
294 return c 298 return c
295 299
296 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
297 """Generates CreateEnumValue() that returns the |base::StringValue|
298 representation of an enum.
299 """
300 c = Code()
301 c.Append('// static')
302 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue('
303 '%(arg)s) {')
304 c.Sblock('switch (%s) {' % prop.unix_name)
305 if prop.optional:
306 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop))
307 .Append(' return scoped_ptr<base::Value>();')
308 .Append('}')
309 )
310 for enum_value in prop.enum_values:
311 (c.Append('case %s: {' %
312 self._cpp_type_generator.GetEnumValue(prop, enum_value))
313 .Append(' return scoped_ptr<base::Value>('
314 'base::Value::CreateStringValue("%s"));' %
315 enum_value)
316 .Append('}')
317 )
318 (c.Append('default: {')
319 .Append(' return scoped_ptr<base::Value>();')
320 .Append('}')
321 )
322 c.Eblock('}')
323 c.Eblock('}')
324 c.Substitute({
325 'cpp_namespace': cpp_namespace,
326 'arg': cpp_util.GetParameterDeclaration(
327 prop, self._cpp_type_generator.GetType(prop))
328 })
329 return c
330
331 def _CreateValueFromProperty(self, prop, var): 300 def _CreateValueFromProperty(self, prop, var):
332 """Creates a base::Value given a property. Generated code passes ownership 301 """Creates a base::Value given a property. Generated code passes ownership
333 to caller. 302 to caller.
334 303
335 var: variable or variable* 304 var: variable or variable*
336 305
337 E.g for std::string, generate base::Value::CreateStringValue(var) 306 E.g for std::string, generate base::Value::CreateStringValue(var)
338 """ 307 """
339 if prop.type_ == PropertyType.CHOICES: 308 if prop.type_ == PropertyType.CHOICES:
340 # CHOICES conversion not implemented. If needed, write something to 309 return 'GetChoiceValue(%s_type).release()' % var
341 # generate a function that returns a scoped_ptr<base::Value> and put it in 310 elif self._IsObjectOrObjectRef(prop):
342 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue()
343 # for reference.
344 raise NotImplementedError(
345 'Conversion of CHOICES to base::Value not implemented')
346 if self._IsObjectOrObjectRef(prop):
347 if prop.optional: 311 if prop.optional:
348 return '%s->ToValue().release()' % var 312 return '%s->ToValue().release()' % var
349 else: 313 else:
350 return '%s.ToValue().release()' % var 314 return '%s.ToValue().release()' % var
351 elif prop.type_ == PropertyType.ANY: 315 elif prop.type_ == PropertyType.ANY:
352 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) 316 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var)
353 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 317 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
354 return '%s.DeepCopy()' % var 318 return '%s.DeepCopy()' % var
355 elif prop.type_ == PropertyType.ENUM: 319 elif prop.type_ == PropertyType.ENUM:
356 return 'CreateEnumValue(%s).release()' % var 320 return 'CreateEnumValue(%s).release()' % var
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 .Append(' ' + enum_temp + ' = %s;' % ( 572 .Append(' ' + enum_temp + ' = %s;' % (
609 self._cpp_type_generator.GetEnumValue(prop, enum_value))) 573 self._cpp_type_generator.GetEnumValue(prop, enum_value)))
610 ) 574 )
611 (c.Append('else') 575 (c.Append('else')
612 .Append(' return %(failure_value)s;') 576 .Append(' return %(failure_value)s;')
613 ) 577 )
614 578
615 def _GeneratePropertyFunctions(self, param_namespace, params): 579 def _GeneratePropertyFunctions(self, param_namespace, params):
616 """Generate the functions for structures generated by a property such as 580 """Generate the functions for structures generated by a property such as
617 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects. 581 CreateEnumValue for ENUMs and Populate/ToValue for Params/Results objects.
582 If from_function is True if these properties belong to an API Function.
not at google - send to devlin 2012/07/19 01:48:22 revert this comment change
chebert 2012/07/19 21:06:17 Done.
618 """ 583 """
619 c = Code() 584 c = Code()
620 for param in params: 585 for param in params:
621 if param.type_ == PropertyType.OBJECT: 586 if param.type_ == PropertyType.OBJECT:
622 c.Concat(self._GenerateType( 587 c.Concat(self._GenerateType(
623 param_namespace + '::' + cpp_util.Classname(param.name), 588 param_namespace + '::' + cpp_util.Classname(param.name),
624 param)) 589 param))
625 c.Append() 590 c.Append()
626 elif param.type_ == PropertyType.ARRAY: 591 elif param.type_ == PropertyType.ARRAY:
627 c.Concat(self._GeneratePropertyFunctions( 592 c.Concat(self._GeneratePropertyFunctions(
628 param_namespace, [param.item_type])) 593 param_namespace, [param.item_type]))
629 elif param.type_ == PropertyType.CHOICES: 594 elif param.type_ == PropertyType.CHOICES:
630 c.Concat(self._GeneratePropertyFunctions( 595 c.Concat(self._GeneratePropertyFunctions(
631 param_namespace, param.choices.values())) 596 param_namespace, param.choices.values()))
597 if param.from_client and param.from_json:
not at google - send to devlin 2012/07/19 01:48:22 why does it need to be both from the JSON? i would
chebert 2012/07/19 21:06:17 Done.
598 c.Concat(self._GenerateGetChoiceValue(param_namespace, param))
632 elif param.type_ == PropertyType.ENUM: 599 elif param.type_ == PropertyType.ENUM:
633 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) 600 c.Concat(self._GenerateCreateEnumValue(param_namespace, param))
634 c.Append() 601 c.Append()
635 return c 602 return c
636 603
604 def _GenerateGetChoiceValue(self, cpp_namespace, prop):
not at google - send to devlin 2012/07/19 01:48:22 this code is so similar to _GenerateCreateEnumValu
chebert 2012/07/19 21:06:17 I took a shot at this. It is a bit contrived thoug
605 """Generates GetChoiceValue() that returns a scoped_ptr<base::Value>
606 representing the choice value.
607 """
608 c = Code()
609 (c.Sblock('scoped_ptr<base::Value> '
not at google - send to devlin 2012/07/19 01:48:22 static? (const would also be unnecessary)
chebert 2012/07/19 21:06:17 non-static, because we return member data. const,
610 '%(cpp_namespace)s::GetChoiceValue(%(arg)s) const {')
611 .Sblock('switch(%s) {' % prop.unix_name)
612 )
613 for choice in self._cpp_type_generator.ExpandParams([prop]):
614 (c.Append('case %s:' %
615 self._cpp_type_generator.GetEnumValue(prop, choice.type_.name))
616 .Append(' return make_scoped_ptr<base::Value>(%s);' %
617 self._CreateValueFromProperty(choice, choice.unix_name))
618 )
619 (c.Append('default:')
not at google - send to devlin 2012/07/19 01:48:22 we are generating a branch for every enum value, s
chebert 2012/07/19 21:06:17 Done.
620 .Append(' return scoped_ptr<base::Value>();')
621 .Eblock('}')
622 .Eblock('}')
623 .Append()
624 .Substitute({
625 'cpp_namespace': cpp_namespace,
626 'arg': cpp_util.GetParameterDeclaration(
627 prop, self._cpp_type_generator.GetChoicesEnumType(prop))
628 })
629 )
630 return c
631
632 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
633 """Generates CreateEnumValue() that returns the |base::StringValue|
634 representation of an enum.
635 """
636 c = Code()
637 c.Append('// static')
638 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue('
639 '%(arg)s) {')
640 c.Sblock('switch (%s) {' % prop.unix_name)
641 if prop.optional:
642 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop))
643 .Append(' return scoped_ptr<base::Value>();')
644 .Append('}')
645 )
646 for enum_value in prop.enum_values:
647 (c.Append('case %s: {' %
648 self._cpp_type_generator.GetEnumValue(prop, enum_value))
649 .Append(' return scoped_ptr<base::Value>('
650 'base::Value::CreateStringValue("%s"));' %
651 enum_value)
652 .Append('}')
653 )
654 (c.Append('default: {')
655 .Append(' return scoped_ptr<base::Value>();')
656 .Append('}')
not at google - send to devlin 2012/07/19 01:48:22 ditto default thing.
chebert 2012/07/19 21:06:17 Done.
657 )
658 c.Eblock('}')
659 c.Eblock('}')
660 c.Substitute({
661 'cpp_namespace': cpp_namespace,
662 'arg': cpp_util.GetParameterDeclaration(
663 prop, self._cpp_type_generator.GetType(prop))
664 })
665 return c
666
637 def _GenerateCreateCallbackArguments(self, function_scope, callback): 667 def _GenerateCreateCallbackArguments(self, function_scope, callback):
638 """Generate all functions to create Value parameters for a callback. 668 """Generate all functions to create Value parameters for a callback.
639 669
640 E.g for function "Bar", generate Bar::Results::Create 670 E.g for function "Bar", generate Bar::Results::Create
641 E.g for event "Baz", generate Baz::Create 671 E.g for event "Baz", generate Baz::Create
642 672
643 function_scope: the function scope path, e.g. Foo::Bar for the function 673 function_scope: the function scope path, e.g. Foo::Bar for the function
644 Foo::Bar::Baz(). 674 Foo::Bar::Baz().
645 callback: the Function object we are creating callback arguments for. 675 callback: the Function object we are creating callback arguments for.
646 """ 676 """
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 """ 736 """
707 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 737 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
708 PropertyType.ARRAY) 738 PropertyType.ARRAY)
709 739
710 def _IsFundamentalOrFundamentalRef(self, prop): 740 def _IsFundamentalOrFundamentalRef(self, prop):
711 """Determines if this property is a Fundamental type or is a ref to a 741 """Determines if this property is a Fundamental type or is a ref to a
712 Fundamental type. 742 Fundamental type.
713 """ 743 """
714 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 744 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
715 is_fundamental) 745 is_fundamental)
OLDNEW
« no previous file with comments | « no previous file | tools/json_schema_compiler/h_generator.py » ('j') | tools/json_schema_compiler/h_generator.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698