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

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

Issue 9309044: Supporting more APIs with json_schema_compiler (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fixed optional properties Created 8 years, 10 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 """Utilies and constants specific to Chromium C++ code. 4 """Utilies and constants specific to Chromium C++ code.
5 """ 5 """
6 6
7 from datetime import datetime 7 from datetime import datetime
8 from model import PropertyType 8 from model import PropertyType
9 import re
9 10
10 CHROMIUM_LICENSE = ( 11 CHROMIUM_LICENSE = (
11 """// Copyright (c) %d The Chromium Authors. All rights reserved. 12 """// Copyright (c) %d The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be 13 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file.""" % datetime.now().year 14 // found in the LICENSE file.""" % datetime.now().year
14 ) 15 )
15 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN 16 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN
16 // %s 17 // %s
17 // DO NOT EDIT. 18 // DO NOT EDIT.
18 """ 19 """
19 20
20 21 def Classname(s):
21 def CppName(s):
22 """Translates a namespace name or function name into something more 22 """Translates a namespace name or function name into something more
23 suited to C++. 23 suited to C++.
24 24
25 eg experimental.downloads -> Experimental_Downloads 25 eg experimental.downloads -> Experimental_Downloads
26 updateAll -> UpdateAll. 26 updateAll -> UpdateAll.
27 """ 27 """
28 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) 28 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')])
29 29
30 def CreateFundamentalValue(prop, var): 30 def CreateFundamentalValue(prop, var):
31 """Returns the C++ code for creating a value of the given property type 31 """Returns the C++ code for creating a value of the given property type
32 using the given variable. 32 using the given variable.
33
34 var: Fundamental or Fundamental*
33 """ 35 """
36 if prop.optional:
37 var = '*' + var
34 return { 38 return {
35 PropertyType.STRING: 'Value::CreateStringValue(%s)', 39 PropertyType.STRING: 'Value::CreateStringValue(%s)',
36 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', 40 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)',
37 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', 41 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)',
38 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', 42 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)',
39 }[prop.type_] % var 43 }[prop.type_] % var
40 44
45 def GetFundamentalValue(prop, src, name, dst):
46 """Returns the C++ code for retrieving a fundamental type from a
47 DictionaryValue into a variable.
41 48
42 def GetFundamentalValue(prop, var): 49 src: DictionaryValue*
43 """Returns the C++ code for retrieving a fundamental type from a Value 50 name: key
44 into a variable. 51 dst: Property*
45 """ 52 """
46 return { 53 return {
47 PropertyType.STRING: 'GetAsString(%s)', 54 PropertyType.STRING: '%s->GetString("%s", %s)',
48 PropertyType.BOOLEAN: 'GetAsBoolean(%s)', 55 PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)',
49 PropertyType.INTEGER: 'GetAsInteger(%s)', 56 PropertyType.INTEGER: '%s->GetInteger("%s", %s)',
50 PropertyType.DOUBLE: 'GetAsDouble(%s)', 57 PropertyType.DOUBLE: '%s->GetDouble("%s", %s)',
51 }[prop.type_] % var 58 }[prop.type_] % (src, name, dst)
59
60 def CreateValueFromSingleProperty(prop, var):
61 """Creates a Value given a single property. Use for everything except
62 PropertyType.ARRAY.
63
64 var: raw value
not at google - send to devlin 2012/02/05 23:42:12 "raw value"?
65 """
66 if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT:
67 return '%s.ToValue()' % var
68 elif prop.type_.is_fundamental:
69 return CreateFundamentalValue(prop, var)
70 else:
71 raise NotImplementedError('Conversion of single %s to Value not implemented' %
not at google - send to devlin 2012/02/05 23:42:12 line wrap
calamity 2012/02/06 11:51:18 Done.
72 repr(prop.type_))
73
74 def GetValueFromList(prop, src, index, dst):
75 """Returns the C++ code for retrieving a fundamental type from a
76 DictionaryValue into a variable.
77
78 src: ListValue&
79 index: int
80 dst: Property*
81 """
82 return {
83 PropertyType.REF: '%s.GetDictionary(%d, %s)',
84 PropertyType.STRING: '%s.GetString(%d, %s)',
85 PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)',
86 PropertyType.INTEGER: '%s.GetInteger(%d, %s)',
87 PropertyType.DOUBLE: '%s.GetDouble(%d, %s)',
88 }[prop.type_] % (src, index, dst)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698