Skip to content

Commit

Permalink
Merge pull request #63 from jpmckinney/performance4
Browse files Browse the repository at this point in the history
Use much faster TypeChecker
  • Loading branch information
Bjwebb authored Sep 3, 2021
2 parents f170f09 + 27f6afa commit 35e95f5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
if: matrix.cove == 'bods'
run: |
git clone https://github.com/openownership/cove-bods.git
cd cove-bods
git checkout update-requirements-jsonschema
cd ..
git clone https://github.com/openownership/lib-cove-bods.git
- name: Install
Expand Down
35 changes: 31 additions & 4 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import functools
import json
import logging
import numbers
import os
import re
from collections import OrderedDict
from tempfile import NamedTemporaryFile
from urllib.parse import urljoin, urlparse
from urllib.parse import urljoin, urlparse, urlsplit
from urllib.request import urlopen

import jsonref
import jsonschema.validators
Expand All @@ -18,16 +20,41 @@
from flattentool import unflatten
from jsonschema import FormatChecker, RefResolver
from jsonschema._utils import extras_msg, find_additional_properties, uniq
from jsonschema.compat import urlopen, urlsplit
from jsonschema.exceptions import ValidationError
from jsonschema.exceptions import UndefinedTypeCheck, ValidationError

from .exceptions import cove_spreadsheet_conversion_error
from .tools import decimal_default, get_request


class TypeChecker:
def is_type(self, instance, type):
if type == "string":
return isinstance(instance, str)
if type == "array":
return isinstance(instance, list)
if type == "object":
return isinstance(instance, dict)
if type == "integer":
if isinstance(instance, bool):
return False
return isinstance(instance, int)
if type == "number":
if isinstance(instance, bool):
return False
return isinstance(instance, numbers.Number)
if type == "boolean":
return isinstance(instance, bool)
if type == "null":
return instance is None
raise UndefinedTypeCheck(type)


# Because we will be changing items on this validator, it's important we take a copy!
# Otherwise we could cause conflicts with other software in the same process.
validator = jsonschema.validators.extend(
jsonschema.validators.Draft4Validator, validators={}
jsonschema.validators.Draft4Validator,
validators={},
type_checker=TypeChecker(),
)

uniqueItemsValidator = validator.VALIDATORS.pop("uniqueItems")
Expand Down

0 comments on commit 35e95f5

Please sign in to comment.