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

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

Powered by Google App Engine
This is Rietveld 408576698