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

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

Issue 10907151: Extensions Docs Server: Enum values do not show up if enum is a type (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ToString and FromString Created 8 years, 3 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 | Annotate | Revision Log
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 for type_ in self._namespace.types.values(): 128 for type_ in self._namespace.types.values():
129 ExpandType([], type_) 129 ExpandType([], type_)
130 return dependency_order 130 return dependency_order
131 131
132 def _GenerateEnumDeclaration(self, enum_name, prop, values): 132 def _GenerateEnumDeclaration(self, enum_name, prop, values):
133 """Generate the declaration of a C++ enum for the given property and 133 """Generate the declaration of a C++ enum for the given property and
134 values. 134 values.
135 """ 135 """
136 c = Code() 136 c = Code()
137 c.Sblock('enum %s {' % enum_name) 137 c.Sblock('enum %s {' % enum_name)
138 if prop.optional: 138 c.Append(self._cpp_type_generator.GetEnumNoneValue(prop) + ',')
139 c.Append(self._cpp_type_generator.GetEnumNoneValue(prop) + ',')
140 for value in values: 139 for value in values:
141 c.Append(self._cpp_type_generator.GetEnumValue(prop, value) + ',') 140 c.Append(self._cpp_type_generator.GetEnumValue(prop, value) + ',')
142 (c.Eblock('};') 141 (c.Eblock('};')
143 .Append() 142 .Append()
144 ) 143 )
145 return c 144 return c
146 145
147 def _GenerateFields(self, props): 146 def _GenerateFields(self, props):
148 """Generates the field declarations when declaring a type. 147 """Generates the field declarations when declaring a type.
149 """ 148 """
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 self._cpp_type_generator.GetCompiledType(type_.item_type, 185 self._cpp_type_generator.GetCompiledType(type_.item_type,
187 wrap_optional=True)}) 186 wrap_optional=True)})
188 elif type_.type_ == PropertyType.STRING: 187 elif type_.type_ == PropertyType.STRING:
189 if type_.description: 188 if type_.description:
190 c.Comment(type_.description) 189 c.Comment(type_.description)
191 c.Append('typedef std::string %(classname)s;') 190 c.Append('typedef std::string %(classname)s;')
192 elif type_.type_ == PropertyType.ENUM: 191 elif type_.type_ == PropertyType.ENUM:
193 if type_.description: 192 if type_.description:
194 c.Comment(type_.description) 193 c.Comment(type_.description)
195 c.Sblock('enum %(classname)s {') 194 c.Sblock('enum %(classname)s {')
195 c.Append('%s,' % self._cpp_type_generator.GetEnumNoneValue(type_))
196 for value in type_.enum_values: 196 for value in type_.enum_values:
197 c.Append('%s_%s,' % (classname.upper(), value.upper())) 197 c.Append('%s,' % self._cpp_type_generator.GetEnumValue(type_, value))
198 (c.Eblock('};') 198 (c.Eblock('};')
199 .Append() 199 .Append()
200 .Append('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % 200 .Append('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
201 (classname, classname.lower())) 201 (classname, classname.lower()))
202 .Append('std::string ToString(%s enum_param);' % classname)
203 .Append('%s From%sString(const std::string& enum_string);' %
204 (classname, classname))
202 ) 205 )
203 else: 206 else:
204 if type_.description: 207 if type_.description:
205 c.Comment(type_.description) 208 c.Comment(type_.description)
206 (c.Sblock('struct %(classname)s {') 209 (c.Sblock('struct %(classname)s {')
207 .Append('~%(classname)s();') 210 .Append('~%(classname)s();')
208 .Append('%(classname)s();') 211 .Append('%(classname)s();')
209 .Append() 212 .Append()
210 .Concat(self._GeneratePropertyStructures(type_.properties.values())) 213 .Concat(self._GeneratePropertyStructures(type_.properties.values()))
211 .Concat(self._GenerateFields(type_.properties.values())) 214 .Concat(self._GenerateFields(type_.properties.values()))
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 [choice.type_.name for choice in prop.choices.values()])) 308 [choice.type_.name for choice in prop.choices.values()]))
306 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 309 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
307 elif prop.type_ == PropertyType.ENUM: 310 elif prop.type_ == PropertyType.ENUM:
308 enum_name = self._cpp_type_generator.GetCompiledType(prop) 311 enum_name = self._cpp_type_generator.GetCompiledType(prop)
309 c.Concat(self._GenerateEnumDeclaration( 312 c.Concat(self._GenerateEnumDeclaration(
310 enum_name, 313 enum_name,
311 prop, 314 prop,
312 prop.enum_values)) 315 prop.enum_values))
313 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' % 316 create_enum_value = ('scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
314 (enum_name, prop.unix_name)) 317 (enum_name, prop.unix_name))
318 enum_to_string = 'std::string ToString(%s enum_param);' % enum_name
319 enum_from_string = ('%s From%sString(const std::string& enum_string);' %
320 (enum_name, enum_name))
315 # If the property is from the UI then we're in a struct so this function 321 # If the property is from the UI then we're in a struct so this function
316 # should be static. If it's from the client, then we're just in a 322 # should be static. If it's from the client, then we're just in a
317 # namespace so we can't have the static keyword. 323 # namespace so we can't have the static keyword.
318 if prop.from_json: 324 if prop.from_json:
319 create_enum_value = 'static ' + create_enum_value 325 create_enum_value = 'static ' + create_enum_value
320 c.Append(create_enum_value) 326 enum_to_string = 'static ' + enum_to_string
327 enum_from_string = 'static ' + enum_from_string
not at google - send to devlin 2012/09/18 03:55:20 change to use 'static %s' %...
cduvall 2012/09/21 00:39:28 Done.
328 (c.Append(create_enum_value)
329 .Append(enum_to_string)
330 .Append(enum_from_string))
321 return c 331 return c
322 332
323 def _GeneratePrivatePropertyStructures(self, props): 333 def _GeneratePrivatePropertyStructures(self, props):
324 """Generate the private structures required by a property such as OBJECT 334 """Generate the private structures required by a property such as OBJECT
325 classes and enums. 335 classes and enums.
326 """ 336 """
327 c = Code() 337 c = Code()
328 for prop in props: 338 for prop in props:
329 if prop.type_ == PropertyType.ARRAY: 339 if prop.type_ == PropertyType.ARRAY:
330 c.Concat(self._GeneratePrivatePropertyStructures([prop.item_type])) 340 c.Concat(self._GeneratePrivatePropertyStructures([prop.item_type]))
(...skipping 28 matching lines...) Expand all
359 369
360 def _GenerateFunctionResults(self, callback): 370 def _GenerateFunctionResults(self, callback):
361 """Generates namespace for passing a function's result back. 371 """Generates namespace for passing a function's result back.
362 """ 372 """
363 c = Code() 373 c = Code()
364 (c.Sblock('namespace Results {') 374 (c.Sblock('namespace Results {')
365 .Concat(self._GenerateCreateCallbackArguments(callback)) 375 .Concat(self._GenerateCreateCallbackArguments(callback))
366 .Eblock('};') 376 .Eblock('};')
367 ) 377 )
368 return c 378 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698