Commit a1f77497 authored by kalman@chromium.org's avatar kalman@chromium.org

Make json_schema_compiler remove 'nocompile' nodes from JSON at the JSON level

rather than at the model level. This gives us automatic nocompile of all
properties; previously not all were supported.


TEST=tools/json_schema_compiler/json_schema_test.py, unit_tests --gtest_filter=JsonSchemaCompiler*

Review URL: http://codereview.chromium.org/10108005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132514 0039d316-1c4b-4281-b951-d872f2087c98
parent 18929a9d
......@@ -48,9 +48,28 @@ def StripJSONComments(stream):
return result
def DeleteNocompileNodes(item):
def HasNocompile(thing):
return type(thing) == dict and thing.get('nocompile', False)
if type(item) == dict:
toDelete = []
for key, value in item.items():
if HasNocompile(value):
toDelete.append(key)
else:
DeleteNocompileNodes(value)
for key in toDelete:
del item[key]
elif type(item) == list:
item[:] = [DeleteNocompileNodes(thing)
for thing in item if not HasNocompile(thing)]
return item
def Load(filename):
with open(filename, 'r') as handle:
return json.loads(StripJSONComments(handle.read()))
return DeleteNocompileNodes(json.loads(StripJSONComments(handle.read())))
# A dictionary mapping |filename| to the object resulting from loading the JSON
......
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from json_schema import CachedLoad
import json_schema_test
import unittest
class JsonSchemaUnittest(unittest.TestCase):
def testNocompile(self):
compiled = [
{
"namespace": "compile",
"functions": [],
"types": {}
},
{
"namespace": "functions",
"functions": [
{
"id": "two"
},
{
"id": "four"
}
],
"types": {
"one": { "key": "value" }
}
},
{
"namespace": "types",
"functions": [
{ "id": "one" }
],
"types": {
"two": {
"key": "value"
},
"four": {
"key": "value"
}
}
},
{
"namespace": "nested",
"properties": {
"sync": {
"functions": [
{
"id": "two"
},
{
"id": "four"
}
],
"types": {
"two": {
"key": "value"
},
"four": {
"key": "value"
}
}
}
}
}
]
self.assertEquals(compiled, CachedLoad('test/json_schema_test.json'))
if __name__ == '__main__':
unittest.main()
......@@ -16,12 +16,8 @@ class Model(object):
self.namespaces = {}
def AddNamespace(self, json, source_file):
"""Add a namespace's json to the model if it doesn't have "nocompile"
property set to true. Returns the new namespace or None if a namespace
wasn't added.
"""Add a namespace's json to the model and returns the namespace.
"""
if json.get('nocompile', False):
return None
namespace = Namespace(json, source_file)
self.namespaces[namespace.name] = namespace
return namespace
......@@ -54,8 +50,7 @@ class Namespace(object):
type_ = Type(self, type_json['id'], type_json)
self.types[type_.name] = type_
for function_json in json.get('functions', []):
if not function_json.get('nocompile', False):
self.functions[function_json['name']] = Function(self, function_json)
self.functions[function_json['name']] = Function(self, function_json)
class Type(object):
"""A Type defined in the json.
......@@ -94,8 +89,7 @@ class Type(object):
self.functions = {}
self.parent = parent
for function_json in json.get('functions', []):
if not function_json.get('nocompile', False):
self.functions[function_json['name']] = Function(self, function_json)
self.functions[function_json['name']] = Function(self, function_json)
props = []
for prop_name, prop_json in json.get('properties', {}).items():
# TODO(calamity): support functions (callbacks) as properties. The model
......
......@@ -27,26 +27,10 @@ class ModelTest(unittest.TestCase):
self.assertEquals(3, len(self.model.namespaces))
self.assertTrue(self.permissions)
def testNamespaceNoCompile(self):
self.permissions_json[0]['namespace'] = 'something'
self.permissions_json[0]['nocompile'] = True
self.model.AddNamespace(self.permissions_json[0],
'path/to/something.json')
self.assertEquals(3, len(self.model.namespaces))
def testHasFunctions(self):
self.assertEquals(["contains", "getAll", "remove", "request"],
sorted(self.permissions.functions.keys()))
def testFunctionNoCompile(self):
# tabs.json has 2 functions marked as nocompile (connect, sendRequest)
self.assertEquals(["captureVisibleTab", "create", "detectLanguage",
"executeScript", "get", "getAllInWindow", "getCurrent",
"getSelected", "highlight", "insertCSS", "move", "query", "reload",
"remove", "update"],
sorted(self.tabs.functions.keys())
)
def testHasTypes(self):
self.assertEquals(['Tab'], self.tabs.types.keys())
self.assertEquals(['Permissions'], self.permissions.types.keys())
......
......@@ -190,12 +190,8 @@ updateEverything();
# Get main json file
api_defs = json_schema.Load(json_file_path)
namespace = api_model.AddNamespace(api_defs[0], json_file_path)
if not namespace:
body.Append("<pre>Target file %s is marked nocompile</pre>" %
json_file_path)
return
type_generator = cpp_type_generator.CppTypeGenerator(
'preview::api', namespace, namespace.unix_name)
'previewserver::api', namespace, namespace.unix_name)
# Get json file depedencies
for dependency in api_defs[0].get('dependencies', []):
......
[
{
"namespace": "compile",
"functions": [],
"types": {}
},
{
"namespace": "nocompile",
"nocompile": true,
"functions": [],
"types": {}
},
{
"namespace": "functions",
"functions": [
{
"id": "one",
"nocompile": true
},
{
"id": "two"
},
{
"id": "three",
"nocompile": true
},
{
"id": "four"
}
],
"types": {
"one": { "key": "value" }
}
},
{
"namespace": "types",
"functions": [
{ "id": "one" }
],
"types": {
"one": {
"key": "value",
"nocompile": true
},
"two": {
"key": "value"
},
"three": {
"key": "value",
"nocompile": true
},
"four": {
"key": "value"
}
}
},
{
"namespace": "nested",
"properties": {
"sync": {
"functions": [
{
"id": "one",
"nocompile": true
},
{
"id": "two"
},
{
"id": "three",
"nocompile": true
},
{
"id": "four"
}
],
"types": {
"one": {
"key": "value",
"nocompile": true
},
"two": {
"key": "value"
},
"three": {
"key": "value",
"nocompile": true
},
"four": {
"key": "value"
}
}
}
}
}
]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment