def resolve_key(obj, key):
    """Resolve key given an object and key."""
    if callable(key):
        return key(obj)
    elif hasattr(obj, 'metadata'):
        return obj.metadata[key]
    raise TypeError("Could not resolve key %r. Key must be callable or %s must"
                    " have `metadata` attribute." % (key,
                                                     obj.__class__.__name__))