diff options
author | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-17 06:50:31 +0000 |
---|---|---|
committer | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-17 06:50:31 +0000 |
commit | 242d5e7aaf2658a88f1ed4954e34876d7082f1a7 (patch) | |
tree | 677cff28f304753c8e9f1d1e9062a9b2de871a77 /tools/json_schema_compiler/cpp_util.py | |
parent | 3543975945a5667aa3fb48c6d0e1b6312d1ed293 (diff) | |
download | chromium_src-242d5e7aaf2658a88f1ed4954e34876d7082f1a7.zip chromium_src-242d5e7aaf2658a88f1ed4954e34876d7082f1a7.tar.gz chromium_src-242d5e7aaf2658a88f1ed4954e34876d7082f1a7.tar.bz2 |
Overhaul JSON Schema Compiler to support a number of features required to
support the full range of JSON/IDL features, improve the interface of generated
code, and improve generated code formatting, notably:
* Make "choices" generate a struct containing each possible type, rather than
multiple fields with different names plus and enum. This makes it possible
to embed choices inside containers - notably within arrays (as in bug).
It also improves optional choices detection.
* Remove the Any C++ type generated by "any", and model it as a base::Value.
* Make "additionalProperties" generated a std::map with the actual types as
values, rather than just a DictionaryValue forcing callers to extract types.
* Fix enum naming, it was unnecessarily including the namespace as a prefix.
R=yoz@chromium.org
BUG=162044
Review URL: https://codereview.chromium.org/11827026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@177361 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/cpp_util.py')
-rw-r--r-- | tools/json_schema_compiler/cpp_util.py | 58 |
1 files changed, 15 insertions, 43 deletions
diff --git a/tools/json_schema_compiler/cpp_util.py b/tools/json_schema_compiler/cpp_util.py index 96a5cd3..9ce14ca 100644 --- a/tools/json_schema_compiler/cpp_util.py +++ b/tools/json_schema_compiler/cpp_util.py @@ -1,11 +1,12 @@ # Copyright (c) 2012 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. + """Utilies and constants specific to Chromium C++ code. """ from datetime import datetime -from model import PropertyType +from model import Property, PropertyType, Type import os CHROMIUM_LICENSE = ( @@ -31,7 +32,7 @@ def Classname(s): """ return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) -def GetAsFundamentalValue(prop, src, dst): +def GetAsFundamentalValue(type_, src, dst): """Returns the C++ code for retrieving a fundamental type from a Value into a variable. @@ -43,7 +44,7 @@ def GetAsFundamentalValue(prop, src, dst): PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', PropertyType.INTEGER: '%s->GetAsInteger(%s)', PropertyType.DOUBLE: '%s->GetAsDouble(%s)', - }[prop.type_] % (src, dst) + }[type_.property_type] % (src, dst) def GetValueType(type_): """Returns the Value::Type corresponding to the model.PropertyType. @@ -58,15 +59,18 @@ def GetValueType(type_): PropertyType.FUNCTION: 'Value::TYPE_DICTIONARY', PropertyType.ARRAY: 'Value::TYPE_LIST', PropertyType.BINARY: 'Value::TYPE_BINARY', - }[type_] + }[type_.property_type] def GetParameterDeclaration(param, type_): """Gets a parameter declaration of a given model.Property and its C++ type. """ - if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY, - PropertyType.STRING, PropertyType.ANY): - arg = '%(type)s& %(name)s' + if param.type_.property_type in (PropertyType.REF, + PropertyType.OBJECT, + PropertyType.ARRAY, + PropertyType.STRING, + PropertyType.ANY): + arg = 'const %(type)s& %(name)s' else: arg = '%(type)s %(name)s' return arg % { @@ -82,40 +86,8 @@ def GenerateIfndefName(path, filename): return (('%s_%s_H__' % (path, filename)) .upper().replace(os.sep, '_').replace('/', '_')) -def GenerateTypeToCompiledTypeConversion(prop, from_, to): - try: - return _GenerateTypeConversionHelper(prop.type_, prop.compiled_type, from_, - to) - except KeyError: - raise NotImplementedError('Conversion from %s to %s in %s not supported' % - (prop.type_, prop.compiled_type, prop.name)) - -def GenerateCompiledTypeToTypeConversion(prop, from_, to): - try: - return _GenerateTypeConversionHelper(prop.compiled_type, prop.type_, from_, - to) - except KeyError: - raise NotImplementedError('Conversion from %s to %s in %s not supported' % - (prop.compiled_type, prop.type_, prop.name)) - -def _GenerateTypeConversionHelper(from_type, to_type, from_, to): - """Converts from PropertyType from_type to PropertyType to_type. - - from_type: The PropertyType to be converted from. - to_type: The PropertyType to be converted to. - from_: The variable name of the type to be converted from. - to: The variable name of the type to be converted to. +def PadForGenerics(var): + """Appends a space to |var| if it ends with a >, so that it can be compiled + within generic types. """ - # TODO(mwrosen): Add support for more from/to combinations as necessary. - return { - PropertyType.STRING: { - PropertyType.INTEGER: 'base::StringToInt(%(from)s, &%(to)s)', - PropertyType.INT64: 'base::StringToInt64(%(from)s, &%(to)s)', - }, - PropertyType.INTEGER: { - PropertyType.STRING: '%(to)s = base::IntToString(%(from)s)', - }, - PropertyType.INT64: { - PropertyType.STRING: '%(to)s = base::Int64ToString(%(from)s)', - } - }[from_type][to_type] % {'from': from_, 'to': to} + return ('%s ' % var) if var.endswith('>') else var |