OLD | NEW |
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 API_UTIL_NAMESPACE = 'json_schema_compiler::util' | 5 API_UTIL_NAMESPACE = 'json_schema_compiler::util' |
6 | 6 |
7 class UtilCCHelper(object): | 7 class UtilCCHelper(object): |
8 """A util class that generates code that uses | 8 """A util class that generates code that uses |
9 tools/json_schema_compiler/util.cc. | 9 tools/json_schema_compiler/util.cc. |
10 """ | 10 """ |
11 def __init__(self, type_manager): | 11 def __init__(self, type_manager): |
12 self._type_manager = type_manager | 12 self._type_manager = type_manager |
13 | 13 |
14 def GetArray(self, array_prop, src, name, dst): | 14 def PopulateArrayFromDictionary(self, array_prop, src, name, dst): |
15 """Generates code to get an array from a src.name into dst. | 15 """Generates code to get an array from a src.name into dst. |
16 | 16 |
17 src: DictionaryValue* | 17 src: DictionaryValue* |
18 dst: std::vector or scoped_ptr<std::vector> | 18 dst: std::vector or scoped_ptr<std::vector> |
19 """ | 19 """ |
20 prop = array_prop.item_type | 20 prop = array_prop.item_type |
21 sub = { | 21 sub = { |
22 'namespace': API_UTIL_NAMESPACE, | 22 'namespace': API_UTIL_NAMESPACE, |
23 'name': name, | 23 'name': name, |
24 'src': src, | 24 'src': src, |
25 'dst': dst, | 25 'dst': dst, |
26 } | 26 } |
27 | 27 |
28 sub['type'] = self._type_manager.GetType(prop) | 28 sub['type'] = self._type_manager.GetType(prop) |
29 if array_prop.optional: | 29 if array_prop.optional: |
30 val = ('%(namespace)s::PopulateOptionalArrayFromDictionary' | 30 val = ('%(namespace)s::PopulateOptionalArrayFromDictionary' |
31 '(*%(src)s, "%(name)s", &%(dst)s)') | 31 '(*%(src)s, "%(name)s", &%(dst)s)') |
32 else: | 32 else: |
33 val = ('%(namespace)s::PopulateArrayFromDictionary' | 33 val = ('%(namespace)s::PopulateArrayFromDictionary' |
34 '(*%(src)s, "%(name)s", &%(dst)s)') | 34 '(*%(src)s, "%(name)s", &%(dst)s)') |
35 | 35 |
36 return val % sub | 36 return val % sub |
37 | 37 |
38 def GetArrayFromList(self, array_prop, src, dst): | 38 def PopulateArrayFromList(self, array_prop, src, dst): |
39 """Generates code to get an array from src into dst. | 39 """Generates code to get an array from src into dst. |
40 | 40 |
41 src: ListValue* | 41 src: ListValue* |
42 dst: std::vector or scoped_ptr<std::vector> | 42 dst: std::vector or scoped_ptr<std::vector> |
43 """ | 43 """ |
44 prop = array_prop.item_type | 44 prop = array_prop.item_type |
45 sub = { | 45 sub = { |
46 'namespace': API_UTIL_NAMESPACE, | 46 'namespace': API_UTIL_NAMESPACE, |
47 'src': src, | 47 'src': src, |
48 'dst': dst, | 48 'dst': dst, |
49 'type': self._type_manager.GetType(prop), | 49 'type': self._type_manager.GetType(prop), |
50 } | 50 } |
51 | 51 |
52 if array_prop.optional: | 52 if array_prop.optional: |
53 val = '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)' | 53 val = '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)' |
54 else: | 54 else: |
55 val = '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)' | 55 val = '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)' |
56 | 56 |
57 return val % sub | 57 return val % sub |
58 | 58 |
59 def SetArray(self, array_prop, src, name, dst): | 59 def PopulateDictionaryFromArray(self, array_prop, src, name, dst): |
60 """Generates code to set dst.name to the array at src | 60 """Generates code to set dst.name to the array at src |
61 | 61 |
62 src: std::vector or scoped_ptr<std::vector> | 62 src: std::vector or scoped_ptr<std::vector> |
63 dst: scoped_ptr<DictionaryValue> | 63 dst: scoped_ptr<DictionaryValue> |
64 """ | 64 """ |
65 prop = array_prop.item_type | 65 prop = array_prop.item_type |
66 sub = { | 66 sub = { |
67 'namespace': API_UTIL_NAMESPACE, | 67 'namespace': API_UTIL_NAMESPACE, |
68 'src': src, | 68 'src': src, |
69 'name': name, | 69 'name': name, |
70 'dst': dst, | 70 'dst': dst, |
71 'type': self._type_manager.GetType(prop), | 71 'type': self._type_manager.GetType(prop), |
72 } | 72 } |
73 | 73 |
74 if array_prop.optional: | 74 if array_prop.optional: |
75 val = ('%(namespace)s::PopulateDictionaryFromOptionalArray' | 75 val = ('%(namespace)s::PopulateDictionaryFromOptionalArray' |
76 '(%(src)s, "%(name)s", %(dst)s.get())') | 76 '(%(src)s, "%(name)s", %(dst)s.get())') |
77 else: | 77 else: |
78 val = ('%(namespace)s::PopulateDictionaryFromArray(%(src)s, ' | 78 val = ('%(namespace)s::PopulateDictionaryFromArray' |
79 '"%(name)s", %(dst)s.get())') | 79 '(%(src)s, "%(name)s", %(dst)s.get())') |
80 | 80 |
81 return val % sub | 81 return val % sub |
82 | 82 |
83 def SetArrayToList(self, array_prop, src, dst): | 83 def PopulateListFromArray(self, array_prop, src, dst): |
84 """Generates code to set dst to the array at src | 84 """Generates code to set dst to the array at src |
85 | 85 |
86 src: std::vector or scoped_ptr<std::vector> | 86 src: std::vector or scoped_ptr<std::vector> |
87 dst: ListValue* | 87 dst: ListValue* |
88 """ | 88 """ |
89 prop = array_prop.item_type | 89 prop = array_prop.item_type |
90 sub = { | 90 sub = { |
91 'namespace': API_UTIL_NAMESPACE, | 91 'namespace': API_UTIL_NAMESPACE, |
92 'src': src, | 92 'src': src, |
93 'dst': dst, | 93 'dst': dst, |
94 'type': self._type_manager.GetType(prop), | 94 'type': self._type_manager.GetType(prop), |
95 } | 95 } |
96 | 96 |
97 if array_prop.optional: | 97 if array_prop.optional: |
98 val = '%(namespace)s::PopulateListFromOptionalArray(%(src)s, %(dst)s)' | 98 val = '%(namespace)s::PopulateListFromOptionalArray(%(src)s, %(dst)s)' |
99 else: | 99 else: |
100 val = '%(namespace)s::PopulateListFromArray(%(src)s, %(dst)s)' | 100 val = '%(namespace)s::PopulateListFromArray(%(src)s, %(dst)s)' |
101 | 101 |
102 return val % sub | 102 return val % sub |
103 | 103 |
104 def GetIncludePath(self): | 104 def GetIncludePath(self): |
105 return '#include "tools/json_schema_compiler/util.h"' | 105 return '#include "tools/json_schema_compiler/util.h"' |
OLD | NEW |