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

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

Issue 10022005: Let json schema compiler handle using arrays as types (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 8 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 model import PropertyType 5 from model import PropertyType
6 import any_helper 6 import any_helper
7 import code 7 import code
8 import cpp_util 8 import cpp_util
9 import model 9 import model
10 import sys 10 import sys
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 if type_.properties: 91 if type_.properties:
92 raise NotImplementedError('\n'.join(model.GetModelHierarchy(type_)) + 92 raise NotImplementedError('\n'.join(model.GetModelHierarchy(type_)) +
93 '\nCannot generate both functions and properties on a type') 93 '\nCannot generate both functions and properties on a type')
94 for function in type_.functions.values(): 94 for function in type_.functions.values():
95 (c.Concat( 95 (c.Concat(
96 self._GenerateFunction( 96 self._GenerateFunction(
97 cpp_namespace + '::' + cpp_util.Classname(function.name), 97 cpp_namespace + '::' + cpp_util.Classname(function.name),
98 function)) 98 function))
99 .Append() 99 .Append()
100 ) 100 )
101 else: 101 elif type_.type_ == PropertyType.OBJECT:
102 (c.Concat(self._GeneratePropertyFunctions( 102 (c.Concat(self._GeneratePropertyFunctions(
103 cpp_namespace, type_.properties.values())) 103 cpp_namespace, type_.properties.values()))
104 .Sblock('%(namespace)s::%(classname)s()') 104 .Sblock('%(namespace)s::%(classname)s()')
105 .Concat(self._GenerateInitializersAndBody(type_)) 105 .Concat(self._GenerateInitializersAndBody(type_))
106 .Eblock('%(namespace)s::~%(classname)s() {}') 106 .Eblock('%(namespace)s::~%(classname)s() {}')
107 .Append() 107 .Append()
108 ) 108 )
109 if type_.from_json: 109 if type_.from_json:
110 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) 110 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_))
111 .Append() 111 .Append()
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 314
315 E.g for std::string, generate Value::CreateStringValue(var) 315 E.g for std::string, generate Value::CreateStringValue(var)
316 """ 316 """
317 if prop.type_ == PropertyType.CHOICES: 317 if prop.type_ == PropertyType.CHOICES:
318 # CHOICES conversion not implemented. If needed, write something to 318 # CHOICES conversion not implemented. If needed, write something to
319 # generate a function that returns a scoped_ptr<Value> and put it in 319 # generate a function that returns a scoped_ptr<Value> and put it in
320 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue() 320 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue()
321 # for reference. 321 # for reference.
322 raise NotImplementedError( 322 raise NotImplementedError(
323 'Conversion of CHOICES to Value not implemented') 323 'Conversion of CHOICES to Value not implemented')
324 if prop.type_ in (PropertyType.REF, PropertyType.OBJECT): 324 if prop.type_ == PropertyType.OBJECT or \
Yoyo Zhou 2012/04/09 23:48:47 style nits: extra space. Also, the style is to con
cduvall 2012/04/10 03:06:08 That makes it way cleaner. Good idea.
325 (prop.type_ == PropertyType.REF and not prop.is_array):
325 if prop.optional: 326 if prop.optional:
326 return '%s->ToValue().release()' % var 327 return '%s->ToValue().release()' % var
327 else: 328 else:
328 return '%s.ToValue().release()' % var 329 return '%s.ToValue().release()' % var
329 elif prop.type_ == PropertyType.ANY: 330 elif prop.type_ == PropertyType.ANY:
330 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) 331 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var)
331 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 332 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
332 return '%s.DeepCopy()' % var 333 return '%s.DeepCopy()' % var
333 elif prop.type_ == PropertyType.ENUM: 334 elif prop.type_ == PropertyType.ENUM:
334 return 'CreateEnumValue(%s).release()' % var 335 return 'CreateEnumValue(%s).release()' % var
335 elif prop.type_ == PropertyType.ARRAY: 336 elif prop.type_ == PropertyType.ARRAY or \
337 (prop.type_ == PropertyType.REF and prop.is_array):
336 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( 338 return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
337 prop, var) 339 prop, var)
338 elif prop.type_.is_fundamental: 340 elif prop.type_.is_fundamental:
339 if prop.optional: 341 if prop.optional:
340 var = '*' + var 342 var = '*' + var
341 return { 343 return {
342 PropertyType.STRING: 'Value::CreateStringValue(%s)', 344 PropertyType.STRING: 'Value::CreateStringValue(%s)',
343 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', 345 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)',
344 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', 346 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)',
345 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', 347 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)',
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 .Append('if (%s)' % 450 .Append('if (%s)' %
449 cpp_util.GetAsFundamentalValue(prop, value_var, '&temp')) 451 cpp_util.GetAsFundamentalValue(prop, value_var, '&temp'))
450 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') 452 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));')
451 ) 453 )
452 else: 454 else:
453 (c.Append('if (!%s)' % 455 (c.Append('if (!%s)' %
454 cpp_util.GetAsFundamentalValue( 456 cpp_util.GetAsFundamentalValue(
455 prop, value_var, '&%s->%s' % (dst, prop.unix_name))) 457 prop, value_var, '&%s->%s' % (dst, prop.unix_name)))
456 .Append('return %(failure_value)s;') 458 .Append('return %(failure_value)s;')
457 ) 459 )
458 elif prop.type_ in (PropertyType.OBJECT, PropertyType.REF): 460 elif prop.type_ == PropertyType.OBJECT or \
461 (prop.type_ == PropertyType.REF and not prop.is_array):
459 if prop.optional: 462 if prop.optional:
460 (c.Append('DictionaryValue* dictionary = NULL;') 463 (c.Append('DictionaryValue* dictionary = NULL;')
461 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 464 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
462 .Append(' return %(failure_value)s;') 465 .Append(' return %(failure_value)s;')
463 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') 466 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
464 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') 467 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
465 .Append(' return %(failure_value)s;') 468 .Append(' return %(failure_value)s;')
466 .Append('%(dst)s->%(name)s = temp.Pass();') 469 .Append('%(dst)s->%(name)s = temp.Pass();')
467 ) 470 )
468 else: 471 else:
469 (c.Append('DictionaryValue* dictionary = NULL;') 472 (c.Append('DictionaryValue* dictionary = NULL;')
470 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 473 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
471 .Append(' return %(failure_value)s;') 474 .Append(' return %(failure_value)s;')
472 .Append( 475 .Append(
473 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') 476 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
474 .Append(' return %(failure_value)s;') 477 .Append(' return %(failure_value)s;')
475 ) 478 )
476 elif prop.type_ == PropertyType.ANY: 479 elif prop.type_ == PropertyType.ANY:
477 if prop.optional: 480 if prop.optional:
478 c.Append('%(dst)s->%(name)s.reset(new Any());') 481 c.Append('%(dst)s->%(name)s.reset(new Any());')
479 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') 482 c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
480 elif prop.type_ == PropertyType.ARRAY: 483 elif prop.type_ == PropertyType.ARRAY or (prop.type_ == PropertyType.REF
484 and prop.is_array):
481 # util_cc_helper deals with optional and required arrays 485 # util_cc_helper deals with optional and required arrays
482 (c.Append('ListValue* list = NULL;') 486 (c.Append('ListValue* list = NULL;')
483 .Append('if (!%(value_var)s->GetAsList(&list))') 487 .Append('if (!%(value_var)s->GetAsList(&list))')
484 .Append(' return %(failure_value)s;') 488 .Append(' return %(failure_value)s;')
485 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( 489 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
486 prop, 'list', dst + '->' + prop.unix_name)) 490 prop, 'list', dst + '->' + prop.unix_name))
487 .Append(' return %(failure_value)s;') 491 .Append(' return %(failure_value)s;')
488 ) 492 )
489 elif prop.type_ == PropertyType.CHOICES: 493 elif prop.type_ == PropertyType.CHOICES:
490 type_var = '%(dst)s->%(name)s_type' 494 type_var = '%(dst)s->%(name)s_type'
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): 613 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES):
610 if prop.optional: 614 if prop.optional:
611 prop_name = prop.unix_name 615 prop_name = prop.unix_name
612 if prop.type_ == PropertyType.CHOICES: 616 if prop.type_ == PropertyType.CHOICES:
613 prop_name = prop.unix_name + '_type' 617 prop_name = prop.unix_name + '_type'
614 c.Append('%s->%s = %s;' % ( 618 c.Append('%s->%s = %s;' % (
615 dst, 619 dst,
616 prop_name, 620 prop_name,
617 self._cpp_type_generator.GetEnumNoneValue(prop))) 621 self._cpp_type_generator.GetEnumNoneValue(prop)))
618 return c 622 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698