Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions coreapi/codecs/corejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ def _get_content(item, base_url=None):
Return a dictionary of content, for documents, objects and errors.
"""
return {
_unescape_key(key): _primative_to_document(value, base_url)
_unescape_key(key): _primitive_to_document(value, base_url)
for key, value in item.items()
if key not in ('_type', '_meta')
}


def _document_to_primative(node, base_url=None):
def _document_to_primitive(node, base_url=None):
"""
Take a Core API document and return Python primatives
Take a Core API document and return Python primitives
ready to be rendered into the JSON style encoding.
"""
if isinstance(node, Document):
Expand All @@ -156,7 +156,7 @@ def _document_to_primative(node, base_url=None):

# Fill in key-value content.
ret.update([
(_escape_key(key), _document_to_primative(value, base_url=url))
(_escape_key(key), _document_to_primitive(value, base_url=url))
for key, value in node.items()
])
return ret
Expand All @@ -170,7 +170,7 @@ def _document_to_primative(node, base_url=None):

# Fill in key-value content.
ret.update([
(_escape_key(key), _document_to_primative(value, base_url=base_url))
(_escape_key(key), _document_to_primitive(value, base_url=base_url))
for key, value in node.items()
])
return ret
Expand All @@ -193,7 +193,7 @@ def _document_to_primative(node, base_url=None):
ret['description'] = node.description
if node.fields:
ret['fields'] = [
_document_to_primative(field) for field in node.fields
_document_to_primitive(field) for field in node.fields
]
return ret

Expand All @@ -209,19 +209,19 @@ def _document_to_primative(node, base_url=None):

elif isinstance(node, Object):
return OrderedDict([
(_escape_key(key), _document_to_primative(value, base_url=base_url))
(_escape_key(key), _document_to_primitive(value, base_url=base_url))
for key, value in node.items()
])

elif isinstance(node, Array):
return [_document_to_primative(value) for value in node]
return [_document_to_primitive(value) for value in node]

return node


def _primative_to_document(data, base_url=None):
def _primitive_to_document(data, base_url=None):
"""
Take Python primatives as returned from parsing JSON content,
Take Python primitives as returned from parsing JSON content,
and return a Core API document.
"""
if isinstance(data, dict) and data.get('_type') == 'document':
Expand Down Expand Up @@ -278,7 +278,7 @@ def _primative_to_document(data, base_url=None):

elif isinstance(data, list):
# Array
content = [_primative_to_document(item, base_url) for item in data]
content = [_primitive_to_document(item, base_url) for item in data]
return Array(content)

# String, Integer, Number, Boolean, null.
Expand All @@ -303,7 +303,7 @@ def decode(self, bytestring, **options):
except ValueError as exc:
raise ParseError('Malformed JSON. %s' % exc)

doc = _primative_to_document(data, base_url)
doc = _primitive_to_document(data, base_url)

if isinstance(doc, Object):
doc = Document(content=dict(doc))
Expand Down Expand Up @@ -331,5 +331,5 @@ def encode(self, document, **options):
'separators': COMPACT_SEPARATORS
}

data = _document_to_primative(document)
data = _document_to_primitive(document)
return force_bytes(json.dumps(data, **kwargs))
2 changes: 1 addition & 1 deletion coreapi/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _key_sorting(item):
Field.__new__.__defaults__ = (False, '', None, None, None, None)


# The Core API primatives:
# The Core API primitives:

class Document(itypes.Dict):
"""
Expand Down
4 changes: 2 additions & 2 deletions coreapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def _validate_form_field(value, allow_files=False, allow_list=True):
elif allow_files and is_file(value):
return value

msg = 'Must be a primative type.'
msg = 'Must be a primitive type.'
raise exceptions.ParameterError(msg)


Expand All @@ -332,5 +332,5 @@ def _validate_json_data(value):
for item_key, item_val in value.items()
}

msg = 'Must be a JSON primative.'
msg = 'Must be a JSON primitive.'
raise exceptions.ParameterError(msg)
20 changes: 10 additions & 10 deletions tests/test_codecs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
from coreapi.codecs import CoreJSONCodec
from coreapi.codecs.corejson import _document_to_primative, _primative_to_document
from coreapi.codecs.corejson import _document_to_primitive, _primitive_to_document
from coreapi.document import Document, Link, Error, Field
from coreapi.exceptions import ParseError, NoCodecAvailable
from coreapi.utils import negotiate_decoder, negotiate_encoder
Expand All @@ -27,10 +27,10 @@ def doc():
})


# Documents have a mapping to python primatives in JSON style.
# Documents have a mapping to python primitives in JSON style.

def test_document_to_primative(doc):
data = _document_to_primative(doc)
def test_document_to_primitive(doc):
data = _document_to_primitive(doc)
assert data == {
'_type': 'document',
'_meta': {
Expand All @@ -46,7 +46,7 @@ def test_document_to_primative(doc):
}


def test_primative_to_document(doc):
def test_primitive_to_document(doc):
data = {
'_type': 'document',
'_meta': {
Expand All @@ -60,27 +60,27 @@ def test_primative_to_document(doc):
'nested': {'child': {'_type': 'link', 'url': 'http://example.org/123'}},
'__type': 'needs escaping'
}
assert _primative_to_document(data) == doc
assert _primitive_to_document(data) == doc


def test_error_to_primative():
def test_error_to_primitive():
error = Error(title='Failure', content={'messages': ['failed']})
data = {
'_type': 'error',
'_meta': {'title': 'Failure'},
'messages': ['failed']
}
assert _document_to_primative(error) == data
assert _document_to_primitive(error) == data


def test_primative_to_error():
def test_primitive_to_error():
error = Error(title='Failure', content={'messages': ['failed']})
data = {
'_type': 'error',
'_meta': {'title': 'Failure'},
'messages': ['failed']
}
assert _primative_to_document(data) == error
assert _primitive_to_document(data) == error


# Codecs can load a document successfully.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_error_does_not_support_property_assignment():
error.integer = 456


# Children in documents are immutable primatives.
# Children in documents are immutable primitives.

def test_document_dictionaries_coerced_to_objects(doc):
assert isinstance(doc['dict'], Object)
Expand Down