Skip to content

Commit

Permalink
Add support for group metadata in a somewhat backward-compatible way (#…
Browse files Browse the repository at this point in the history
…566)

Save group metadata if it's not empty.
Old clients will only be able to access packages that don't contain group metadata.
  • Loading branch information
dimaryaz authored Apr 20, 2018
1 parent 574e86d commit 22987b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
27 changes: 17 additions & 10 deletions compiler/quilt/tools/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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 {}
Expand All @@ -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

Expand All @@ -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__()
Expand All @@ -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]}

Expand Down
27 changes: 17 additions & 10 deletions registry/quilt_server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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 {}
Expand All @@ -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

Expand All @@ -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__()
Expand All @@ -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]}

Expand Down
6 changes: 6 additions & 0 deletions registry/quilt_server/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
'type': {
'enum': [RootNode.json_type]
},
'metadata': {
'type': 'object'
},
'children': {
'type': 'object',
'additionalProperties': {
Expand Down Expand Up @@ -86,6 +89,9 @@
'type': {
'enum': [GroupNode.json_type]
},
'metadata': {
'type': 'object'
},
'children': {
'$ref': '#/properties/contents/properties/children'
}
Expand Down

0 comments on commit 22987b9

Please sign in to comment.