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

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

Issue 1055673002: [Extensions API] Remove inline enums (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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 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 import util_cc_helper 9 import util_cc_helper
10 from cpp_namespace_environment import CppNamespaceEnvironment 10 from cpp_namespace_environment import CppNamespaceEnvironment
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 141
142 return c 142 return c
143 143
144 def _GenerateInitializersAndBody(self, type_): 144 def _GenerateInitializersAndBody(self, type_):
145 items = [] 145 items = []
146 for prop in type_.properties.values(): 146 for prop in type_.properties.values():
147 t = prop.type_ 147 t = prop.type_
148 148
149 real_t = self._type_helper.FollowRef(t) 149 real_t = self._type_helper.FollowRef(t)
150 if real_t.property_type == PropertyType.ENUM: 150 if real_t.property_type == PropertyType.ENUM:
151 items.append('%s(%s)' % ( 151 items.append('%s(%s%s)' % (
152 prop.unix_name, 152 prop.unix_name,
153 ('%s::' % real_t.namespace.unix_name) if
154 real_t.namespace != self._namespace else '',
not at google - send to devlin 2015/04/03 17:04:38 So, the docserver code is all over the place when
Devlin 2015/04/03 19:38:51 Done.
153 self._type_helper.GetEnumNoneValue(t))) 155 self._type_helper.GetEnumNoneValue(t)))
154 elif prop.optional: 156 elif prop.optional:
155 continue 157 continue
156 elif t.property_type == PropertyType.INTEGER: 158 elif t.property_type == PropertyType.INTEGER:
157 items.append('%s(0)' % prop.unix_name) 159 items.append('%s(0)' % prop.unix_name)
158 elif t.property_type == PropertyType.DOUBLE: 160 elif t.property_type == PropertyType.DOUBLE:
159 items.append('%s(0.0)' % prop.unix_name) 161 items.append('%s(0.0)' % prop.unix_name)
160 elif t.property_type == PropertyType.BOOLEAN: 162 elif t.property_type == PropertyType.BOOLEAN:
161 items.append('%s(false)' % prop.unix_name) 163 items.append('%s(false)' % prop.unix_name)
162 elif (t.property_type == PropertyType.ANY or 164 elif (t.property_type == PropertyType.ANY or
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 value_var = prop.unix_name + '_value' 285 value_var = prop.unix_name + '_value'
284 c.Append('const base::Value* %(value_var)s = NULL;') 286 c.Append('const base::Value* %(value_var)s = NULL;')
285 if prop.optional: 287 if prop.optional:
286 (c.Sblock( 288 (c.Sblock(
287 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') 289 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
288 .Concat(self._GeneratePopulatePropertyFromValue( 290 .Concat(self._GeneratePopulatePropertyFromValue(
289 prop, value_var, dst, 'false'))) 291 prop, value_var, dst, 'false')))
290 underlying_type = self._type_helper.FollowRef(prop.type_) 292 underlying_type = self._type_helper.FollowRef(prop.type_)
291 if underlying_type.property_type == PropertyType.ENUM: 293 if underlying_type.property_type == PropertyType.ENUM:
292 (c.Append('} else {') 294 (c.Append('} else {')
293 .Append('%%(dst)s->%%(name)s = %s;' % 295 .Append('%%(dst)s->%%(name)s = %s%s;' %
294 self._type_helper.GetEnumNoneValue(prop.type_))) 296 (('%s::' % underlying_type.namespace.unix_name) if
not at google - send to devlin 2015/04/03 17:04:38 Ditto. This is even harder to dissect.
Devlin 2015/04/03 19:38:51 Done.
297 underlying_type.namespace != self._namespace else '',
298 self._type_helper.GetEnumNoneValue(prop.type_))))
295 c.Eblock('}') 299 c.Eblock('}')
296 else: 300 else:
297 (c.Sblock( 301 (c.Sblock(
298 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {') 302 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
299 .Concat(self._GenerateError('"\'%%(key)s\' is required"')) 303 .Concat(self._GenerateError('"\'%%(key)s\' is required"'))
300 .Append('return false;') 304 .Append('return false;')
301 .Eblock('}') 305 .Eblock('}')
302 .Concat(self._GeneratePopulatePropertyFromValue( 306 .Concat(self._GeneratePopulatePropertyFromValue(
303 prop, value_var, dst, 'false')) 307 prop, value_var, dst, 'false'))
304 ) 308 )
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 """Initialize a model.Property to its default value inside an object. 1024 """Initialize a model.Property to its default value inside an object.
1021 1025
1022 E.g for optional enum "state", generate dst->state = STATE_NONE; 1026 E.g for optional enum "state", generate dst->state = STATE_NONE;
1023 1027
1024 dst: Type* 1028 dst: Type*
1025 """ 1029 """
1026 c = Code() 1030 c = Code()
1027 underlying_type = self._type_helper.FollowRef(prop.type_) 1031 underlying_type = self._type_helper.FollowRef(prop.type_)
1028 if (underlying_type.property_type == PropertyType.ENUM and 1032 if (underlying_type.property_type == PropertyType.ENUM and
1029 prop.optional): 1033 prop.optional):
1030 c.Append('%s->%s = %s;' % ( 1034 c.Append('%s->%s = %s%s;' % (
1031 dst, 1035 dst,
1032 prop.unix_name, 1036 prop.unix_name,
1037 ('%s::' % underlying_type.namespace.unix_name) if
not at google - send to devlin 2015/04/03 17:04:38 Ditto.
Devlin 2015/04/03 19:38:51 Done.
1038 underlying_type.namespace != self._namespace else '',
1033 self._type_helper.GetEnumNoneValue(prop.type_))) 1039 self._type_helper.GetEnumNoneValue(prop.type_)))
1034 return c 1040 return c
1035 1041
1036 def _GenerateError(self, body): 1042 def _GenerateError(self, body):
1037 """Generates an error message pertaining to population failure. 1043 """Generates an error message pertaining to population failure.
1038 1044
1039 E.g 'expected bool, got int' 1045 E.g 'expected bool, got int'
1040 """ 1046 """
1041 c = Code() 1047 c = Code()
1042 if not self._generate_error_messages: 1048 if not self._generate_error_messages:
1043 return c 1049 return c
1044 (c.Append('if (error->length())') 1050 (c.Append('if (error->length())')
1045 .Append(' error->append(UTF8ToUTF16("; "));') 1051 .Append(' error->append(UTF8ToUTF16("; "));')
1046 .Append('error->append(UTF8ToUTF16(%s));' % body)) 1052 .Append('error->append(UTF8ToUTF16(%s));' % body))
1047 return c 1053 return c
1048 1054
1049 def _GenerateParams(self, params): 1055 def _GenerateParams(self, params):
1050 """Builds the parameter list for a function, given an array of parameters. 1056 """Builds the parameter list for a function, given an array of parameters.
1051 """ 1057 """
1052 if self._generate_error_messages: 1058 if self._generate_error_messages:
1053 params = list(params) + ['base::string16* error'] 1059 params = list(params) + ['base::string16* error']
1054 return ', '.join(str(p) for p in params) 1060 return ', '.join(str(p) for p in params)
1055 1061
1056 def _GenerateArgs(self, args): 1062 def _GenerateArgs(self, args):
1057 """Builds the argument list for a function, given an array of arguments. 1063 """Builds the argument list for a function, given an array of arguments.
1058 """ 1064 """
1059 if self._generate_error_messages: 1065 if self._generate_error_messages:
1060 args = list(args) + ['error'] 1066 args = list(args) + ['error']
1061 return ', '.join(str(a) for a in args) 1067 return ', '.join(str(a) for a in args)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698