Skip to content

Commit

Permalink
feat: add a __hash__ implementation to AccessEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
spennymac committed May 6, 2020
1 parent df29b7d commit 416bdca
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions google/cloud/bigquery/dataset.py
Expand Up @@ -16,15 +16,16 @@

from __future__ import absolute_import

import six
import copy

import google.cloud._helpers
import six

from google.cloud.bigquery import _helpers
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
from google.cloud.bigquery.model import ModelReference
from google.cloud.bigquery.routine import RoutineReference
from google.cloud.bigquery.table import TableReference
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration


def _get_table_reference(self, table_id):
Expand Down Expand Up @@ -145,18 +146,29 @@ def __init__(self, role, entity_type, entity_id):
"Role must be set for entity " "type %r" % (entity_type,)
)

self.role = role
self.entity_type = entity_type
self.entity_id = entity_id
self._role = role
self._entity_type = entity_type
self._entity_id = entity_id

@property
def role(self):
"""str: The role of the entry."""
return self._role

@property
def entity_type(self):
"""str: The entity_type of the entry."""
return self._entity_type

@property
def entity_id(self):
"""str: The entity_id of the entry."""
return self._entity_id

def __eq__(self, other):
if not isinstance(other, AccessEntry):
return NotImplemented
return (
self.role == other.role
and self.entity_type == other.entity_type
and self.entity_id == other.entity_id
)
return self._key() == other._key()

def __ne__(self, other):
return not self == other
Expand All @@ -168,6 +180,21 @@ def __repr__(self):
self.entity_id,
)

def _key(self):
""" A tuple key that uniquely describes this field.
Used to compute this instance's hashcode and evaluate equality.
Returns:
Tuple: The contents of this :class:`~google.cloud.bigquery.dataset.AccessEntry`.
"""
return (
self._role,
self._entity_type,
self._entity_id
)

def __hash__(self):
return hash(self._key())

def to_api_repr(self):
"""Construct the API resource representation of this access entry
Expand Down

0 comments on commit 416bdca

Please sign in to comment.