|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2025 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +# |
| 17 | +# AUTOGENERATED FILE: DO NOT EDIT. The source of truth for this file is under |
| 18 | +# the gapic_templates directory, not this one. |
| 19 | + |
| 20 | +import collections.abc |
| 21 | +import proto |
| 22 | + |
| 23 | + |
| 24 | +class OneofMessage(proto.Message): |
| 25 | + def _get_oneof_field_from_key(self, key): |
| 26 | + """Given a field name, return the corresponding oneof associated with it. If it doesn't exist, return None.""" |
| 27 | + |
| 28 | + oneof_type = None |
| 29 | + |
| 30 | + try: |
| 31 | + oneof_type = self._meta.fields[key].oneof |
| 32 | + except KeyError: |
| 33 | + # Underscores may be appended to field names |
| 34 | + # that collide with python or proto-plus keywords. |
| 35 | + # In case a key only exists with a `_` suffix, coerce the key |
| 36 | + # to include the `_` suffix. It's not possible to |
| 37 | + # natively define the same field with a trailing underscore in protobuf. |
| 38 | + # See related issue |
| 39 | + # https://github.com/googleapis/python-api-core/issues/227 |
| 40 | + if f"{key}_" in self._meta.fields: |
| 41 | + key = f"{key}_" |
| 42 | + oneof_type = self._meta.fields[key].oneof |
| 43 | + |
| 44 | + return oneof_type |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + mapping=None, |
| 49 | + *, |
| 50 | + ignore_unknown_fields=False, |
| 51 | + **kwargs, |
| 52 | + ): |
| 53 | + # We accept several things for `mapping`: |
| 54 | + # * An instance of this class. |
| 55 | + # * An instance of the underlying protobuf descriptor class. |
| 56 | + # * A dict |
| 57 | + # * Nothing (keyword arguments only). |
| 58 | + # |
| 59 | + # |
| 60 | + # Check for oneofs collisions in the parameters provided. Extract a set of |
| 61 | + # all fields that are set from the mappings + kwargs combined. |
| 62 | + mapping_fields = set(kwargs.keys()) |
| 63 | + |
| 64 | + if mapping is None: |
| 65 | + pass |
| 66 | + elif isinstance(mapping, collections.abc.Mapping): |
| 67 | + mapping_fields.update(mapping.keys()) |
| 68 | + elif isinstance(mapping, self._meta.pb): |
| 69 | + mapping_fields.update(field.name for field, _ in mapping.ListFields()) |
| 70 | + elif isinstance(mapping, type(self)): |
| 71 | + mapping_fields.update(field.name for field, _ in mapping._pb.ListFields()) |
| 72 | + else: |
| 73 | + # Sanity check: Did we get something not a map? Error if so. |
| 74 | + raise TypeError( |
| 75 | + "Invalid constructor input for %s: %r" |
| 76 | + % ( |
| 77 | + self.__class__.__name__, |
| 78 | + mapping, |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + oneofs = set() |
| 83 | + |
| 84 | + for field in mapping_fields: |
| 85 | + oneof_field = self._get_oneof_field_from_key(field) |
| 86 | + if oneof_field is not None: |
| 87 | + if oneof_field in oneofs: |
| 88 | + raise ValueError( |
| 89 | + "Invalid constructor input for %s: Multiple fields defined for oneof %s" |
| 90 | + % (self.__class__.__name__, oneof_field) |
| 91 | + ) |
| 92 | + else: |
| 93 | + oneofs.add(oneof_field) |
| 94 | + |
| 95 | + super().__init__(mapping, ignore_unknown_fields=ignore_unknown_fields, **kwargs) |
| 96 | + |
| 97 | + def __setattr__(self, key, value): |
| 98 | + # Oneof check: Only set the value of an existing oneof field |
| 99 | + # if the field being overridden is the same as the field already set |
| 100 | + # for the oneof. |
| 101 | + oneof = self._get_oneof_field_from_key(key) |
| 102 | + if ( |
| 103 | + oneof is not None |
| 104 | + and self._pb.HasField(oneof) |
| 105 | + and self._pb.WhichOneof(oneof) != key |
| 106 | + ): |
| 107 | + raise ValueError( |
| 108 | + "Overriding the field set for oneof %s with a different field %s" |
| 109 | + % (oneof, key) |
| 110 | + ) |
| 111 | + super().__setattr__(key, value) |
0 commit comments