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

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

Issue 10824002: JSON Schema Compiler supports functions as PropertyTypes. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: bool function; becomes bool has_function; Created 8 years, 4 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 elif t == PropertyType.BOOLEAN: 145 elif t == PropertyType.BOOLEAN:
146 items.append('%s(false)' % prop.unix_name) 146 items.append('%s(false)' % prop.unix_name)
147 elif t == PropertyType.BINARY: 147 elif t == PropertyType.BINARY:
148 items.append('%s(NULL)' % prop.unix_name) 148 items.append('%s(NULL)' % prop.unix_name)
149 elif (t == PropertyType.ADDITIONAL_PROPERTIES or 149 elif (t == PropertyType.ADDITIONAL_PROPERTIES or
150 t == PropertyType.ANY or 150 t == PropertyType.ANY or
151 t == PropertyType.ARRAY or 151 t == PropertyType.ARRAY or
152 t == PropertyType.CHOICES or 152 t == PropertyType.CHOICES or
153 t == PropertyType.ENUM or 153 t == PropertyType.ENUM or
154 t == PropertyType.OBJECT or 154 t == PropertyType.OBJECT or
155 t == PropertyType.FUNCTION or
155 t == PropertyType.REF or 156 t == PropertyType.REF or
156 t == PropertyType.STRING): 157 t == PropertyType.STRING):
157 # TODO(miket): It would be nice to initialize CHOICES and ENUM, but we 158 # TODO(miket): It would be nice to initialize CHOICES and ENUM, but we
158 # don't presently have the semantics to indicate which one of a set 159 # don't presently have the semantics to indicate which one of a set
159 # should be the default. 160 # should be the default.
160 continue 161 continue
161 else: 162 else:
162 sys.exit("Unhandled PropertyType: %s" % t) 163 sys.exit("Unhandled PropertyType: %s" % t)
163 164
164 if items: 165 if items:
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 def _GenerateTypePopulateProperty(self, prop, src, dst): 209 def _GenerateTypePopulateProperty(self, prop, src, dst):
209 """Generate the code to populate a single property in a type. 210 """Generate the code to populate a single property in a type.
210 211
211 src: base::DictionaryValue* 212 src: base::DictionaryValue*
212 dst: Type* 213 dst: Type*
213 """ 214 """
214 c = Code() 215 c = Code()
215 value_var = prop.unix_name + '_value' 216 value_var = prop.unix_name + '_value'
216 c.Append('base::Value* %(value_var)s = NULL;') 217 c.Append('base::Value* %(value_var)s = NULL;')
217 if prop.optional: 218 if prop.optional:
218 (c.Sblock( 219 if prop.type_ == PropertyType.FUNCTION:
not at google - send to devlin 2012/07/25 01:32:33 Comment why you're only generating for optional.
chebert 2012/07/25 18:38:30 the empty dict solved a lot of problems, so a lot
219 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' 220 # For functions, we only want to check the presence, we are not actually
221 # getting a value
not at google - send to devlin 2012/07/25 01:32:33 nit: fullstop
chebert 2012/07/25 18:38:30 Done.
222 (c.Sblock('%s->has_%s = '
223 '%s->GetWithoutPathExpansion("%s", &%s);' %
not at google - send to devlin 2012/07/25 01:32:33 just use HasKey, don't need to Get it too.
224 (dst, prop.unix_name, src, prop.name, value_var))
220 ) 225 )
221 .Concat(self._GeneratePopulatePropertyFromValue( 226 else:
222 prop, value_var, dst, 'false')) 227 (c.Sblock(
223 .Eblock('}') 228 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {'
224 ) 229 )
230 .Concat(self._GeneratePopulatePropertyFromValue(
231 prop, value_var, dst, 'false'))
232 .Eblock('}')
233 )
225 else: 234 else:
226 (c.Append( 235 (c.Append(
227 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') 236 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))')
228 .Append(' return false;') 237 .Append(' return false;')
229 .Concat(self._GeneratePopulatePropertyFromValue( 238 .Concat(self._GeneratePopulatePropertyFromValue(
230 prop, value_var, dst, 'false')) 239 prop, value_var, dst, 'false'))
231 ) 240 )
232 c.Append() 241 c.Append()
233 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src}) 242 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src})
234 return c 243 return c
235 244
236 def _GenerateTypeToValue(self, cpp_namespace, type_): 245 def _GenerateTypeToValue(self, cpp_namespace, type_):
237 """Generates a function that serializes the type into a 246 """Generates a function that serializes the type into a
238 |base::DictionaryValue|. 247 |base::DictionaryValue|.
239 248
240 E.g. for type "Foo" generates Foo::ToValue() 249 E.g. for type "Foo" generates Foo::ToValue()
241 """ 250 """
242 c = Code() 251 c = Code()
243 (c.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' % 252 (c.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' %
244 cpp_namespace) 253 cpp_namespace)
245 .Append('scoped_ptr<base::DictionaryValue> value(' 254 .Append('scoped_ptr<base::DictionaryValue> value('
246 'new base::DictionaryValue());') 255 'new base::DictionaryValue());')
247 .Append() 256 .Append()
248 ) 257 )
249 for prop in type_.properties.values(): 258 for prop in type_.properties.values():
250 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 259 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
251 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) 260 c.Append('value->MergeDictionary(&%s);' % prop.unix_name)
252 else: 261 else:
253 if prop.optional: 262 if prop.optional and not prop.type_ == PropertyType.FUNCTION:
not at google - send to devlin 2012/07/25 01:32:33 nit: assign this condition to a local variable and
chebert 2012/07/25 18:38:30 Done.
254 if prop.type_ == PropertyType.ENUM: 263 if prop.type_ == PropertyType.ENUM:
255 c.Sblock('if (%s != %s)' % 264 c.Sblock('if (%s != %s)' %
256 (prop.unix_name, 265 (prop.unix_name,
257 self._cpp_type_generator.GetEnumNoneValue(prop))) 266 self._cpp_type_generator.GetEnumNoneValue(prop)))
258 elif prop.type_ == PropertyType.CHOICES: 267 elif prop.type_ == PropertyType.CHOICES:
259 c.Sblock('if (%s_type != %s)' % 268 c.Sblock('if (%s_type != %s)' %
260 (prop.unix_name, 269 (prop.unix_name,
261 self._cpp_type_generator.GetEnumNoneValue(prop))) 270 self._cpp_type_generator.GetEnumNoneValue(prop)))
262 else: 271 else:
263 c.Sblock('if (%s.get())' % prop.unix_name) 272 c.Sblock('if (%s.get())' % prop.unix_name)
264 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( 273 c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
265 prop.name, 274 prop.name,
266 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name))) 275 self._CreateValueFromProperty(prop, 'this->' + prop.unix_name)))
267 if prop.optional: 276 if prop.optional and not prop.type_ == PropertyType.FUNCTION:
268 c.Eblock(); 277 c.Eblock();
269 (c.Append() 278 (c.Append()
270 .Append('return value.Pass();') 279 .Append('return value.Pass();')
271 .Eblock('}') 280 .Eblock('}')
272 ) 281 )
273 return c 282 return c
274 283
275 def _GenerateFunction(self, cpp_namespace, function): 284 def _GenerateFunction(self, cpp_namespace, function):
276 """Generates the definitions for function structs. 285 """Generates the definitions for function structs.
277 """ 286 """
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 if prop.optional: 320 if prop.optional:
312 return '%s->ToValue().release()' % var 321 return '%s->ToValue().release()' % var
313 else: 322 else:
314 return '%s.ToValue().release()' % var 323 return '%s.ToValue().release()' % var
315 elif prop.type_ == PropertyType.ANY: 324 elif prop.type_ == PropertyType.ANY:
316 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) 325 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var)
317 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: 326 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES:
318 return '%s.DeepCopy()' % var 327 return '%s.DeepCopy()' % var
319 elif prop.type_ == PropertyType.ENUM: 328 elif prop.type_ == PropertyType.ENUM:
320 return 'CreateEnumValue(%s).release()' % var 329 return 'CreateEnumValue(%s).release()' % var
330 elif prop.type_ == PropertyType.FUNCTION:
not at google - send to devlin 2012/07/25 01:32:33 Hm, well, for consistency with the way that functi
chebert 2012/07/25 18:38:30 Done.
331 return 'base::Value::CreateBooleanValue(this->has_%s)' % prop.unix_name
321 elif prop.type_ == PropertyType.BINARY: 332 elif prop.type_ == PropertyType.BINARY:
322 if prop.optional: 333 if prop.optional:
323 vardot = var + '->' 334 vardot = var + '->'
324 else: 335 else:
325 vardot = var + '.' 336 vardot = var + '.'
326 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' % 337 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' %
327 (vardot, vardot)) 338 (vardot, vardot))
328 elif self._IsArrayOrArrayRef(prop): 339 elif self._IsArrayOrArrayRef(prop):
329 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( 340 return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
330 self._cpp_type_generator.GetReferencedProperty(prop), var, 341 self._cpp_type_generator.GetReferencedProperty(prop), var,
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 .Append('%(dst)s->%(name)s = temp.Pass();') 473 .Append('%(dst)s->%(name)s = temp.Pass();')
463 ) 474 )
464 else: 475 else:
465 (c.Append('base::DictionaryValue* dictionary = NULL;') 476 (c.Append('base::DictionaryValue* dictionary = NULL;')
466 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') 477 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
467 .Append(' return %(failure_value)s;') 478 .Append(' return %(failure_value)s;')
468 .Append( 479 .Append(
469 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') 480 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
470 .Append(' return %(failure_value)s;') 481 .Append(' return %(failure_value)s;')
471 ) 482 )
483 elif prop.type_ == PropertyType.FUNCTION:
484 c.Append('%s->has_%s = true;' % (dst, prop.unix_name))
not at google - send to devlin 2012/07/25 01:32:33 comment plz, the stuff about the renderer I said i
chebert 2012/07/25 18:38:30 Done.
472 elif prop.type_ == PropertyType.ANY: 485 elif prop.type_ == PropertyType.ANY:
473 if prop.optional: 486 if prop.optional:
474 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') 487 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());')
475 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') 488 c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
476 elif self._IsArrayOrArrayRef(prop): 489 elif self._IsArrayOrArrayRef(prop):
477 # util_cc_helper deals with optional and required arrays 490 # util_cc_helper deals with optional and required arrays
478 (c.Append('base::ListValue* list = NULL;') 491 (c.Append('base::ListValue* list = NULL;')
479 .Append('if (!%(value_var)s->GetAsList(&list))') 492 .Append('if (!%(value_var)s->GetAsList(&list))')
480 .Append(' return %(failure_value)s;')) 493 .Append(' return %(failure_value)s;'))
481 if prop.item_type.type_ == PropertyType.ENUM: 494 if prop.item_type.type_ == PropertyType.ENUM:
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 """ 764 """
752 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == 765 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ ==
753 PropertyType.ARRAY) 766 PropertyType.ARRAY)
754 767
755 def _IsFundamentalOrFundamentalRef(self, prop): 768 def _IsFundamentalOrFundamentalRef(self, prop):
756 """Determines if this property is a Fundamental type or is a ref to a 769 """Determines if this property is a Fundamental type or is a ref to a
757 Fundamental type. 770 Fundamental type.
758 """ 771 """
759 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. 772 return (self._cpp_type_generator.GetReferencedProperty(prop).type_.
760 is_fundamental) 773 is_fundamental)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698