Use class matches in resource.update()
This was introduced in 3.10, and seems like a much better way to handle matching a type. Signed-off-by: Nic Cope <nicc@rk0n.org>
This commit is contained in:
parent
5fe3b5c9e0
commit
df655cabec
|
@ -36,20 +36,18 @@ def update(r: fnv1.Resource, source: dict | structpb.Struct | pydantic.BaseModel
|
||||||
|
|
||||||
The source can be a dictionary, a protobuf Struct, or a Pydantic model.
|
The source can be a dictionary, a protobuf Struct, or a Pydantic model.
|
||||||
"""
|
"""
|
||||||
# If the resource has a model_dump attribute it's a Pydantic BaseModel.
|
match source:
|
||||||
if hasattr(source, "model_dump"):
|
case pydantic.BaseModel():
|
||||||
r.resource.update(source.model_dump(exclude_defaults=True, warnings=False))
|
r.resource.update(source.model_dump(exclude_defaults=True, warnings=False))
|
||||||
return
|
case structpb.Struct():
|
||||||
|
# TODO(negz): Use struct_to_dict and update to match other semantics?
|
||||||
# If the resource has a get_or_create_struct attribute it's a Struct.
|
r.resource.MergeFrom(source)
|
||||||
if hasattr(source, "get_or_create_struct"):
|
case dict():
|
||||||
# TODO(negz): Use struct_to_dict and update to match other semantics?
|
r.resource.update(source)
|
||||||
r.resource.MergeFrom(source)
|
case _:
|
||||||
return
|
t = type(source)
|
||||||
|
msg = f"Unsupported type: {t}"
|
||||||
# If it has neither, it must be a dictionary.
|
raise TypeError(msg)
|
||||||
r.resource.update(source)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def dict_to_struct(d: dict) -> structpb.Struct:
|
def dict_to_struct(d: dict) -> structpb.Struct:
|
||||||
|
|
Loading…
Reference in New Issue