@@ -461,6 +461,31 @@ def condition(self, value: Union["Condition", dict, None]):
461461 @property
462462 def entity_type (self ) -> Optional [str ]:
463463 """The entity_type of the entry."""
464+
465+ # The api_repr for an AccessEntry object is expected to be a dict with
466+ # only a few keys. Two keys that may be present are role and condition.
467+ # Any additional key is going to have one of ~eight different names:
468+ # userByEmail, groupByEmail, domain, dataset, specialGroup, view,
469+ # routine, iamMember
470+
471+ # if self._entity_type is None, see if it needs setting
472+ # i.e. is there a key: value pair that should be associated with
473+ # entity_type and entity_id?
474+ if self ._entity_type is None :
475+ resource = self ._properties .copy ()
476+ # we are empyting the dict to get to the last `key: value`` pair
477+ # so we don't keep these first entries
478+ _ = resource .pop ("role" , None )
479+ _ = resource .pop ("condition" , None )
480+
481+ try :
482+ # we only need entity_type, because entity_id gets set elsewhere.
483+ entity_type , _ = resource .popitem ()
484+ except KeyError :
485+ entity_type = None
486+
487+ self ._entity_type = entity_type
488+
464489 return self ._entity_type
465490
466491 @property
@@ -524,36 +549,10 @@ def from_api_repr(cls, resource: dict) -> "AccessEntry":
524549 Returns:
525550 google.cloud.bigquery.dataset.AccessEntry:
526551 Access entry parsed from ``resource``.
527-
528- Raises:
529- ValueError:
530- If the resource has more keys than ``role`` and one additional
531- key.
532552 """
533553
534- # The api_repr for an AccessEntry object is expected to be a dict with
535- # only a few keys. Two keys that may be present are role and condition.
536- # Any additional key is going to have one of ~eight different names:
537- # userByEmail, groupByEmail, domain, dataset, specialGroup, view,
538- # routine, iamMember
539- #
540- # First we pop role and condition out of the dict, if present.
541- # This should leave only one item in the dict which will be a key: value
542- # pair that will be assigned to entity_type and entity_id respectively.
543-
544- entry = resource .copy ()
545- role = entry .pop ("role" , None )
546- condition = entry .pop ("condition" , None )
547- try :
548- entity_type , entity_id = entry .popitem ()
549- except KeyError :
550- entity_type = None
551- entity_id = None
552-
553- if condition :
554- access_entry = cls (role , entity_type , entity_id , condition = condition )
555- else :
556- access_entry = cls (role , entity_type , entity_id )
554+ access_entry = cls ()
555+ access_entry ._properties = resource .copy ()
557556 return access_entry
558557
559558
0 commit comments