diff --git a/compiler/quilt/tools/core.py b/compiler/quilt/tools/core.py index e1868707763..65ef395397b 100644 --- a/compiler/quilt/tools/core.py +++ b/compiler/quilt/tools/core.py @@ -27,6 +27,12 @@ class Node(object): def json_type(cls): raise NotImplementedError + def __init__(self, metadata): + if metadata is None: + metadata = {} + assert isinstance(metadata, dict) + self.metadata = metadata or {} + def __eq__(self, other): if isinstance(other, self.__class__): return self.__dict__ == other.__dict__ @@ -39,7 +45,13 @@ def __hash__(self): return hash(self.__dict__) def __json__(self): - return dict(self.__dict__, type=self.json_type) + val = dict(self.__dict__, type=self.json_type) + if not self.metadata: + # Old clients don't support metadata in groups. If the metadata is empty, + # let's not include it at all to avoid breaking things unnecessarily. + # (Groups with actual metadata will still break old clients, though.) + del val['metadata'] + return val def get_children(self): return {} @@ -62,7 +74,8 @@ def preorder(self, sort=False): class GroupNode(Node): json_type = 'GROUP' - def __init__(self, children): + def __init__(self, children, metadata=None): + super(GroupNode, self).__init__(metadata) assert isinstance(children, dict) self.children = children @@ -76,16 +89,13 @@ class TableNode(Node): json_type = 'TABLE' def __init__(self, hashes, format, metadata=None): - if metadata is None: - metadata = {} + super(TableNode, self).__init__(metadata) assert isinstance(hashes, list) assert isinstance(format, string_types), '%r' % format - assert isinstance(metadata, dict) self.hashes = hashes self.format = PackageFormat(format) - self.metadata = metadata def __json__(self): val = super(TableNode, self).__json__() @@ -96,14 +106,11 @@ class FileNode(Node): json_type = 'FILE' def __init__(self, hashes, metadata=None): - if metadata is None: - metadata = {} + super(FileNode, self).__init__(metadata) assert isinstance(hashes, list) - assert isinstance(metadata, dict) self.hashes = hashes - self.metadata = metadata NODE_TYPE_TO_CLASS = {cls.json_type: cls for cls in [GroupNode, RootNode, TableNode, FileNode]} diff --git a/registry/quilt_server/core.py b/registry/quilt_server/core.py index e1868707763..65ef395397b 100644 --- a/registry/quilt_server/core.py +++ b/registry/quilt_server/core.py @@ -27,6 +27,12 @@ class Node(object): def json_type(cls): raise NotImplementedError + def __init__(self, metadata): + if metadata is None: + metadata = {} + assert isinstance(metadata, dict) + self.metadata = metadata or {} + def __eq__(self, other): if isinstance(other, self.__class__): return self.__dict__ == other.__dict__ @@ -39,7 +45,13 @@ def __hash__(self): return hash(self.__dict__) def __json__(self): - return dict(self.__dict__, type=self.json_type) + val = dict(self.__dict__, type=self.json_type) + if not self.metadata: + # Old clients don't support metadata in groups. If the metadata is empty, + # let's not include it at all to avoid breaking things unnecessarily. + # (Groups with actual metadata will still break old clients, though.) + del val['metadata'] + return val def get_children(self): return {} @@ -62,7 +74,8 @@ def preorder(self, sort=False): class GroupNode(Node): json_type = 'GROUP' - def __init__(self, children): + def __init__(self, children, metadata=None): + super(GroupNode, self).__init__(metadata) assert isinstance(children, dict) self.children = children @@ -76,16 +89,13 @@ class TableNode(Node): json_type = 'TABLE' def __init__(self, hashes, format, metadata=None): - if metadata is None: - metadata = {} + super(TableNode, self).__init__(metadata) assert isinstance(hashes, list) assert isinstance(format, string_types), '%r' % format - assert isinstance(metadata, dict) self.hashes = hashes self.format = PackageFormat(format) - self.metadata = metadata def __json__(self): val = super(TableNode, self).__json__() @@ -96,14 +106,11 @@ class FileNode(Node): json_type = 'FILE' def __init__(self, hashes, metadata=None): - if metadata is None: - metadata = {} + super(FileNode, self).__init__(metadata) assert isinstance(hashes, list) - assert isinstance(metadata, dict) self.hashes = hashes - self.metadata = metadata NODE_TYPE_TO_CLASS = {cls.json_type: cls for cls in [GroupNode, RootNode, TableNode, FileNode]} diff --git a/registry/quilt_server/schemas.py b/registry/quilt_server/schemas.py index d1fbec1a1c6..82427a64705 100644 --- a/registry/quilt_server/schemas.py +++ b/registry/quilt_server/schemas.py @@ -33,6 +33,9 @@ 'type': { 'enum': [RootNode.json_type] }, + 'metadata': { + 'type': 'object' + }, 'children': { 'type': 'object', 'additionalProperties': { @@ -86,6 +89,9 @@ 'type': { 'enum': [GroupNode.json_type] }, + 'metadata': { + 'type': 'object' + }, 'children': { '$ref': '#/properties/contents/properties/children' }